Class: Zillabyte::Runner::AppRunner

Inherits:
Command::Base show all
Includes:
Helpers
Defined in:
lib/zillabyte/runner/app_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



177
178
179
180
181
182
183
184
185
# File 'lib/zillabyte/runner/app_runner.rb', line 177

def cdisplay(name, message)

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

#describe_app(meta) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/zillabyte/runner/app_runner.rb', line 197

def describe_app(meta)
  colors ||= [:green, :yellow, :magenta, :cyan, :white, :blue, :light_yellow, :light_blue, :red, :light_magenta, :light_cyan]
  rjust = 20
  display "#{'app name'.rjust(rjust)}: #{meta['name']}"
  display "#{'app language'.rjust(rjust)}: #{meta['language']}"
  meta['nodes'].each_with_index do |node, index|
    @colors[node['name']] ||= colors[index % colors.length]
    color = @colors[node['name']]
    display (("="*rjust + "  operation ##{index}").colorize(color))
    display "#{"name".rjust(rjust)}: #{node['name'].to_s.colorize(color)}"
    display "#{"type".rjust(rjust)}: #{node['type'].to_s.colorize(color)}"
    display "#{"matches".rjust(rjust)}: #{JSON.pretty_generate(node['matches']).indent(rjust+2).lstrip.colorize(color)}" if node['matches']
    display "#{"consumes".rjust(rjust)}: #{node['consumes'].to_s.colorize(color)}" if node['consumes']
    display "#{"emits".rjust(rjust)}: #{JSON.pretty_generate(node['emits']).indent(rjust+2).lstrip.colorize(color)}" if node['emits']
  end
end

#display(message, newline = true) ⇒ Object



192
193
194
# File 'lib/zillabyte/runner/app_runner.rb', line 192

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

#query_agnostic(query) ⇒ Object



188
189
190
# File 'lib/zillabyte/runner/app_runner.rb', line 188

def query_agnostic(query)
  @session.api.query.agnostic(query)
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
# File 'lib/zillabyte/runner/app_runner.rb', line 11

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

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

  @session = session

  @colors = {}
  output = options[:output]
  otype = options[:output_type] 
  interactive = options[:interactive]
  cycles = (options[:cycles] || "1").to_i

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

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

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

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

  # On each cycle, setup and tear down a test harness
  (1..cycles).each do |cycle|

    display "starting cycle #{cycle}" unless  cycles == 1
    
    begin

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

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

        @operation_pipes[name] = {
          :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 stream name, 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::MultilangOperation.run(n, dir, in_pipe, out_pipes, self, meta, options)
  
          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 interactive
        display "To view results:  Enter 'end'    "
        display "To exit the test: Enter 'Ctrl-C' "
        display ""


        while true
          display "Enter an input tuple in JSON format i.e.{ \"url\" : \"foo.com\", \"html\" : \"bar.html\" }"
          msg = ask

          begin
            JSON.parse(msg)
          rescue JSON::ParserError
            display "Received invalid JSON object"
            next
          end
          # Send tuple to source
         @operation_pipes["source_1"][: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 
end

#session ⇒ Object



172
173
174
# File 'lib/zillabyte/runner/app_runner.rb', line 172

def session
  @session
end