Class: Mushy::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mushy/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner = nil) ⇒ Runner

Returns a new instance of Runner.



7
8
9
# File 'lib/mushy/runner.rb', line 7

def initialize runner = nil
  self.runner = runner || self
end

Instance Attribute Details

#runnerObject

Returns the value of attribute runner.



5
6
7
# File 'lib/mushy/runner.rb', line 5

def runner
  @runner
end

Instance Method Details

#build_event(event_data, flow_id, run_id, flux_id) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/mushy/runner.rb', line 58

def build_event event_data, flow_id, run_id, flux_id
  event = Mushy::Event.new
  event.id = SecureRandom.uuid
  event.run_id = run_id
  event.flow_id = flow_id
  event.flux_id = flux_id
  event.data = event_data
  event
end

#find_run(flux, flow) ⇒ Object



51
52
53
54
55
56
# File 'lib/mushy/runner.rb', line 51

def find_run flux, flow
  run = Mushy::Run.new
  run.id = SecureRandom.uuid
  run.flow_id = flow.id
  run
end

#run_event_in_flow(event, flow) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mushy/runner.rb', line 30

def run_event_in_flow(event, flow)
  fluxes = flow.fluxs_for(event)

  [fluxes.map do |flux|
    events = runner.run_event_with_flux event, flux, flow

    return [flux.stop(events[0]), false] if flux.respond_to?(:stop)

    events
  end.flatten, true]
end

#run_event_with_flux(event, flux, flow) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/mushy/runner.rb', line 42

def run_event_with_flux event, flux, flow
  data = event.data
  data = flow.adjust_data data
  [flux.execute(data)]
    .flatten
    .reject { |x| x.nil? }
    .map { |x| x.is_a?(Hash) ? build_event(x, event.flow_id, event.run_id, flux.id) : x }
end

#start(event_data, flux, flow) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mushy/runner.rb', line 11

def start event_data, flux, flow
  run = find_run flux, flow
  starting_event = build_event event_data, flow.id, run.id, flux.id

  events = run_event_with_flux starting_event, flux, flow

  while events.any?
    events = events.map do |event|
                          result = runner.run_event_in_flow(event, flow)

                          return result[0].flatten unless result[1]

                          result[0]
                        end.flatten
  end

  run
end