Class: RigidWorkflow::WorkflowRunner
- Inherits:
-
Object
- Object
- RigidWorkflow::WorkflowRunner
- Defined in:
- lib/rigid_workflow/workflow_runner.rb
Overview
Executes workflow definitions. Handles step execution, signal waiting, loops, parallel and race blocks.
Instance Method Summary collapse
-
#initialize(workflow_run) ⇒ WorkflowRunner
constructor
A new instance of WorkflowRunner.
- #loop(name, collection, &block) ⇒ Object
- #memo(name) ⇒ Object
- #parallel(name) ⇒ Object
- #race(name) ⇒ Object
- #run(&block) ⇒ Object
- #step(name, activity_class, **options) ⇒ Object
- #wait(name, timeout: nil, **options) ⇒ Object
Constructor Details
#initialize(workflow_run) ⇒ WorkflowRunner
Returns a new instance of WorkflowRunner.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 7 def initialize(workflow_run) @workflow_run = workflow_run @params = workflow_run.params.with_indifferent_access @activity_cache = workflow_run.steps.reload.index_by(&:step_name) @signal_cache = workflow_run.signals.reload.index_by(&:name) @activity_names_seen = Set.new @parallel_mode = false @race_mode = false @loop_stack = [] @loop_index_stack = [] @suspensions = [] @parallel_results = nil @race_winner = nil end |
Instance Method Details
#loop(name, collection, &block) ⇒ Object
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 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 109 def loop(name, collection, &block) @memory ||= (@workflow_run.memory || {}).to_h.with_indifferent_access full_name = name.to_s full_name = "#{@loop_stack.last}_" + full_name if @loop_stack.any? memory_key = "loop_#{full_name}" index = @memory[memory_key] || 0 return if index >= collection.size @loop_stack.push(name) @loop_index_stack.push(index) @workflow_run.memory = @memory collection.each_with_index do |item, current_index| next if current_index < index @memory[memory_key] = current_index @loop_index_stack[-1] = current_index yield(item, current_index) end @memory.delete(memory_key) ensure @loop_stack.pop @loop_index_stack.pop end |
#memo(name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 97 def memo(name) full_name = "memo_#{name}" @memory ||= (@workflow_run.memory || {}).to_h.with_indifferent_access return @memory[full_name] if @memory.key?(full_name) value = yield @memory[full_name] = value @workflow_run.memory = @memory value end |
#parallel(name) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 185 def parallel(name) if @parallel_mode || @race_mode raise NestedBlockError, "Nesting parallel or race blocks is not supported" end @parallel_results = {} old_mode = @parallel_mode @parallel_mode = true yield @parallel_mode = old_mode if @suspensions.any? throw :suspend else @parallel_results.dup.tap { @parallel_results = nil } end ensure @parallel_mode = old_mode end |
#race(name) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 206 def race(name) if @parallel_mode || @race_mode raise NestedBlockError, "Nesting parallel or race blocks is not supported" end @race_winner = nil old_mode = @race_mode @race_mode = true yield @race_mode = old_mode if @race_winner @race_winner.dup.tap { @race_winner = nil } elsif @suspensions.any? throw :suspend end ensure @race_mode = old_mode end |
#run(&block) ⇒ Object
22 23 24 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 22 def run(&block) instance_eval(&block) end |
#step(name, activity_class, **options) ⇒ Object
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 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 26 def step(name, activity_class, **) full_name = build_step_name(name) if @activity_names_seen.include?(full_name) raise DuplicateNameError.new( "Activity '#{full_name}' cannot be called more than once" ) end @activity_names_seen << full_name step = @activity_cache[full_name] if step&.completed? result = StepResult.new(step.status, step.current_attempt&.output) handle_completed(name, result) return result end if step&.failed? unless step.attempt_count < step.max_attempts raise ActivityFailedError.new(step) end schedule_retry(step) handle_suspension end if step&.pending? schedule_step(step.id, ) handle_suspension end force_async = activity_class.respond_to?(:force_async?) && activity_class.force_async? should_async = force_async || [:async] || [:wait] || [:wait_until] if !@parallel_mode && !@race_mode && !should_async && step.nil? step = create_workflow_step(full_name, activity_class, ) step.start! activity = activity_class.new(step) input_hash = step.input.deep_symbolize_keys begin result = activity.perform(**input_hash) step.complete!(result) rescue StandardError => error handle_activity_error(step, error) end @activity_cache[full_name] = step handle_completed( name, StepResult.new(step.status, step.current_attempt&.output) ) return StepResult.new(step.status, step.current_attempt&.output) end if step.nil? step = create_workflow_step(full_name, activity_class, ) schedule_step(step.id, ) if should_async && !@parallel_mode && !@race_mode @suspensions << :suspend throw :suspend end end handle_suspension end |
#wait(name, timeout: nil, **options) ⇒ Object
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 |
# File 'lib/rigid_workflow/workflow_runner.rb', line 137 def wait(name, timeout: nil, **) full_name = name.to_s full_name = "#{@loop_stack.last}_#{@loop_index_stack.last}_" + full_name if @loop_stack.any? signal = @signal_cache[full_name] if signal&.received? @signal_cache[full_name] = signal result = signal.payload handle_completed(name, result) return result end if signal&.expired? signal.update!(received_at: Time.current) unless signal.received? @signal_cache[full_name] = signal handle_completed(name, nil) return nil end if signal.nil? is_timer = timeout.is_a?(ActiveSupport::Duration) || timeout.is_a?(Numeric) if is_timer signal = @workflow_run.signals.create!( name: full_name, expires_at: Time.current + timeout, payload: [:payload] || {} ) schedule_timer(signal.id, wait: timeout) else signal = @workflow_run.signals.create!(name: full_name) end end @signal_cache[full_name] = signal return handle_suspension if @race_mode throw :suspend unless @parallel_mode handle_suspension nil end |