Class: Rockflow::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/rockflow/flow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ Flow

Returns a new instance of Flow.



6
7
8
9
10
# File 'lib/rockflow/flow.rb', line 6

def initialize(payload = {})
  @steps = []
  @payload = payload
  setup
end

Instance Attribute Details

#payloadObject

Returns the value of attribute payload.



4
5
6
# File 'lib/rockflow/flow.rb', line 4

def payload
  @payload
end

#stepsObject

Returns the value of attribute steps.



4
5
6
# File 'lib/rockflow/flow.rb', line 4

def steps
  @steps
end

Instance Method Details

#concertTrueClass, FalseClass

Returns true or false depending if the flow raised any errors or violated any conditions.

Returns:

  • (TrueClass, FalseClass)

    returns true if no exception is raised else false.



21
22
23
24
25
# File 'lib/rockflow/flow.rb', line 21

def concert
  execute_steps.inject(true) do |result, elem|
    result && !elem.errors.any?
  end
end

#concert!Array

Returns all steps if successfull. If any error is raised inside the step it is raised.

Returns:

  • (Array)

    array of all defined steps in the flow.



29
30
31
32
33
34
35
36
# File 'lib/rockflow/flow.rb', line 29

def concert!
  execute_steps
  @steps.map do |step|
    step.errors
  end.flatten.each do |error|
    raise error
  end
end

#execute_stepsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rockflow/flow.rb', line 51

def execute_steps
  while !steps_finished?
    break if next_free_steps.blank?
    ::Parallel.each(next_free_steps, threads_or_processes.to_sym => Rockflow.configuration.thread_or_processes) do |step|
      begin
        step.execute_pre_conditions
        step.it_up unless step.failed?
        step.execute_post_conditions
        step.finish! unless step.failed?
      rescue => e
        step.fail!
        step.errors << e
        raise Parallel::Break, e.message
      end
    end
  end
  @steps
end

#next_free_stepsObject



38
39
40
41
42
# File 'lib/rockflow/flow.rb', line 38

def next_free_steps
  @steps.select do |step|
    step.after_dependencies_finished? && !step.finished? && !step.failed?
  end
end

#rock(klazz, opts = {}) ⇒ Object



15
16
17
# File 'lib/rockflow/flow.rb', line 15

def rock(klazz, opts = {})
  @steps << klazz.new(self, opts)
end

#setupObject



12
13
# File 'lib/rockflow/flow.rb', line 12

def setup
end

#steps_finished?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/rockflow/flow.rb', line 44

def steps_finished?
  @steps.inject(true) do |result, elem|
    result = result && (elem.finished? || elem.failed?)
    result
  end
end