Class: Zillabyte::Runner::ComponentRunner

Inherits:
Command::Base show all
Includes:
Helpers
Defined in:
lib/zillabyte/runner/component_runner.rb

Overview

HIDDEN:

Constant Summary

Constants inherited from Command::Base

Command::Base::META_COLUMNS

Instance Attribute Summary

Attributes inherited from Command::Base

#args, #options

Instance Method Summary collapse

Methods included from Helpers

#add_git_remote, #app, #ask, #create_git_remote, #error, #extract_app_from_git_config, #extract_app_in_dir, #format_with_bang, #get_flow_name, #git, #handle_downloading_manifest, #has_git?, #longest, #read_multiline, #with_tty

Methods inherited from Command::Base

alias_command, #api, extract_banner, extract_description, extract_help, extract_help_from_caller, extract_options, extract_summary, inherited, #initialize, method_added, namespace

Constructor Details

This class inherits a constructor from Zillabyte::Command::Base

Instance Method Details

#cdisplay(name, message) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/zillabyte/runner/component_runner.rb', line 191

def cdisplay(name, message)
   color = @colors[name] || :default
    if message == ""
      display ""
    else
      display "#{name} - #{message}".colorize(color)
    end
end

#describe_component(meta) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/zillabyte/runner/component_runner.rb', line 205

def describe_component(meta)
  colors ||= [:green, :yellow, :magenta, :cyan, :white, :blue, :light_yellow, :light_blue, :red, :light_magenta, :light_cyan]
  rjust = 20

  display "#{'component name'.rjust(rjust)}: #{meta['name']}"
  display "#{'component language'.rjust(rjust)}: #{meta['language']}"
  meta['nodes'].each_with_index do |node, index|
    color = @colors[node['name']] ||= colors[index % colors.length]
    display (("="*rjust + "  operation ##{index}").colorize(color))
    display "#{"name".rjust(rjust)}: #{node['name'].to_s.colorize(color)}"

    # Convert metadata typing to that of components
    if node['type'] == "source"
      type = "input"
      display "#{"type".rjust(rjust)}: #{type.to_s.colorize(color)}"
      display "#{"fields".rjust(rjust)}: #{node['fields'].to_s.colorize(color)}"
      display "#{"matches".rjust(rjust)}: #{JSON.pretty_generate(node['matches']).indent(rjust+2).lstrip.colorize(color)}" if node['matches']
    elsif node['type'] == "sink"
      type = "output"
      display "#{"type".rjust(rjust)}: #{type.to_s.colorize(color)}"
      display "#{"columns".rjust(rjust)}: #{node['columns'].to_s.colorize(color)}"

    else
      type = node['type']
      display "#{"type".rjust(rjust)}: #{type.to_s.colorize(color)}"
    end
  end
end

#display(message, newline = true) ⇒ Object



201
202
203
# File 'lib/zillabyte/runner/component_runner.rb', line 201

def display(message, newline = true)
  @session.display(message, newline)
end

#run(meta, dir = Dir.pwd, session = nil, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/zillabyte/runner/component_runner.rb', line 11

def run (meta, dir = Dir.pwd, session = nil, options = {})

  if meta.nil? or session.nil?
    return
  end

  @session = session
  @colors = {}

  # Get options
  input = options[:input]
  output = options[:output]
  otype = options[:output_type] 

  # Show the user what we know about their app... 
  display "inferring your app details..."
  describe_component(meta)

  # Setup streams
  @nodes = meta["nodes"]

  # Index stream consummers and emitters by stream name
  @arcs = meta["arcs"]

  # Organize component pipes
  @operations = {}
  @operation_pipes = {}


  # Start component
  begin

    # Setup operation pipes
    @nodes.each do |n|

      name = n["name"]
      type = n["type"]
      if n["type"] ==  "source"
        fields = n["fields"]
      end
      
      # Create two new pipes in the parent.
      rd_child, wr_parent = IO.pipe()
      rd_parent, wr_child = IO.pipe()

      @operation_pipes[name] = {
        :node => n,
        :rd_child => rd_child,      
        :wr_child => wr_child,
        :rd_parent => rd_parent,        
        :wr_parent => wr_parent
      }
    end

    # Maps origin => {stream => [destinations]}
    @arc_map = {}
    @arcs.each do |a|
      origin = a["origin"]
      name = a["name"]
      dest = a["dest"]
      @arc_map[origin] ||= {}
      @arc_map[origin][name] ||= []
      @arc_map[origin][name] << a["dest"]
    end


    # Spawn component threads
    @nodes.each do |n|

      name = n["name"]
      type = n["type"]
      emits = n["emits"]

      pipes = @operation_pipes[name] 
      rd_child = pipes[:rd_child]
      wr_child = pipes[:wr_child]
      rd_parent = pipes[:rd_parent]        
      wr_parent = pipes[:wr_parent]

      # Fork.
      pid = fork()
      if pid # In parent
        # Close the reading end of the child so we can write to the child.
        rd_child.close()
        # Close the writing end of the child so we can read from the child.
        wr_child.close()

      else # in child
        # Close the writing end of the parent so we can read from the parent.
        wr_parent.close()
        # Close the reading end of the parent so we can write to the parent.
        rd_parent.close()
        begin

          # Setup reading and writing pipes for communicating with consumee component
          in_pipe = {:rd_child => @operation_pipes[name][:rd_child], :wr_child => @operation_pipes[name][:wr_child]}

          # Index consumer pipes by consumer_name
          out_pipes = {}

          if type != "sink"
            @arc_map[name].each_pair do |stream, destinations|
              out_pipes[stream] ||= {}
              destinations.each do |dest|
                out_pipes[stream][dest] = {:wr_parent => @operation_pipes[dest][:wr_parent], :rd_parent => @operation_pipes[dest][:rd_parent] }
              end
            end
          end

          # Run the child process 
          Zillabyte::Runner::ComponentOperation.run(n, dir, in_pipe, out_pipes, self, meta, options)

       rescue => e
        display e

        ensure
          # Close the reading end of the child
          rd_child.close()
          # Close the writing end of the child
          wr_child.close()
          exit!(-1)
        end

      end #end child
    end 


    # If no input file, read from STDIN
    # TODO handle inputs
    if input.nil?

      source = ""
      @nodes.each do |n|
        name = n["name"]
        type = n["type"]

        if type == "source"
          if source == ""
            source = name
          else
            display "Cannot run component with multiple input sources without input files"
            return
          end
        end
      end
        
      display ""
      display "To view results:  Enter 'end'    "
      display "To exit the test: Enter 'Ctrl-C' "
      display ""

      while true

        # TODO this doesnt handle multiple sources
        #source = @operation_pipes["source_1"][:node]

        display "Enter an input tuple in JSON format i.e.{ \"url\" : \"foo.com\", \"html\" : \"bar.html\" }"
        msg = ask
        # Send tuple to source
        @operation_pipes[source][:wr_parent].puts msg
      end
    end
 
  ensure
    Process.waitall()
    @operation_pipes.each do |name, pipes|
      #Close the  writing end of the parent
      pipes[:wr_parent].close()
      # Close the reading end of the parent
      pipes[:rd_parent].close()
    end
  end

end

#session ⇒ Object



186
187
188
# File 'lib/zillabyte/runner/component_runner.rb', line 186

def session
  @session
end