Class: SolidFlow::Replay

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_flow/replay.rb

Defined Under Namespace

Classes: AwaitingState, Event, ExecutionState, State, StepState, TaskState, TimerState

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow_class:, events:, execution_record:) ⇒ Replay

Returns a new instance of Replay.



112
113
114
115
116
# File 'lib/solid_flow/replay.rb', line 112

def initialize(workflow_class:, events:, execution_record:)
  @workflow_class = workflow_class
  @events = Array(events)
  @execution_record = execution_record
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



110
111
112
# File 'lib/solid_flow/replay.rb', line 110

def events
  @events
end

#execution_recordObject (readonly)

Returns the value of attribute execution_record.



110
111
112
# File 'lib/solid_flow/replay.rb', line 110

def execution_record
  @execution_record
end

#workflow_classObject (readonly)

Returns the value of attribute workflow_class.



110
111
112
# File 'lib/solid_flow/replay.rb', line 110

def workflow_class
  @workflow_class
end

Instance Method Details

#callObject



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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/solid_flow/replay.rb', line 118

def call
  execution_state = ExecutionState.new(
    id: execution_record.fetch(:id),
    workflow: execution_record.fetch(:workflow),
    state: execution_record.fetch(:state),
    cursor_step: execution_record[:cursor_step]&.to_sym,
    cursor_index: execution_record[:cursor_index] || 0,
    graph_signature: execution_record[:graph_signature],
    metadata: execution_record[:metadata] || {}
  )

  ctx = (execution_record[:ctx] || {}).with_indifferent_access

  step_states = {}
  workflow_class.steps.each do |step|
    step_states[step.name] = StepState.new(
      name: step.name,
      status: :idle,
      attempt: 0,
      last_result: nil,
      last_error: nil,
      idempotency_key: nil,
      wait_instructions: []
    )
  end

  task_states = {}
  timer_states = {}
  signal_buffer = Signals::Buffer.new
  awaiting = nil

  events.each do |event|
    type = event.type.to_sym
    raw_payload = event.payload || {}
    payload = raw_payload.is_a?(Hash) ? raw_payload.deep_symbolize_keys : raw_payload

    case type
    when :workflow_started
      ctx.merge!(payload[:input] || {})
    when :step_waiting
      step = payload.fetch(:step).to_sym
      instructions = Array(payload[:instructions]).map do |inst|
        inst.deep_symbolize_keys
      end
      awaiting = AwaitingState.new(step:, instructions:)
      if step_states[step]
        step_states[step].status = :waiting
        step_states[step].wait_instructions = instructions
      end
    when :step_completed
      step = payload.fetch(:step).to_sym
      step_state = step_states[step]
      if step_state
        step_state.status = :completed
        step_state.wait_instructions = []
        step_state.last_result = payload[:result]
        step_state.last_error = nil
        step_state.attempt = payload[:attempt] if payload.key?(:attempt)
        step_state.idempotency_key = payload[:idempotency_key]
      end
      ctx.merge!(payload[:ctx_snapshot] || {})
      awaiting = nil if awaiting&.step == step
    when :task_scheduled
      step = payload.fetch(:step).to_sym
      task_states[step] = TaskState.new(
        step:,
        status: :scheduled,
        attempt: payload[:attempt] || 1,
        idempotency_key: payload[:idempotency_key],
        last_result: nil,
        last_error: nil
      )
      if step_states[step]
        step_states[step].status = :task_scheduled
        step_states[step].attempt = payload[:attempt] || 1
        step_states[step].idempotency_key = payload[:idempotency_key]
      end
    when :task_completed
      step = payload.fetch(:step).to_sym
      task_state = task_states[step] ||= TaskState.new(step:, status: :scheduled, attempt: 0, idempotency_key: nil, last_result: nil, last_error: nil)
      task_state.status = :completed
      task_state.attempt = payload[:attempt] || task_state.attempt
      task_state.last_result = payload[:result]
      task_state.last_error = nil
      if step_states[step]
        step_states[step].status = :completed
        step_states[step].last_result = payload[:result]
        step_states[step].last_error = nil
        step_states[step].attempt = task_state.attempt
      end
      ctx.merge!(payload[:ctx_snapshot] || {})
    when :task_failed
      step = payload.fetch(:step).to_sym
      task_state = task_states[step] ||= TaskState.new(step:, status: :scheduled, attempt: 0, idempotency_key: nil, last_result: nil, last_error: nil)
      task_state.status = :failed
      task_state.attempt = payload[:attempt] || task_state.attempt
      task_state.last_error = payload[:error]
      if step_states[step]
        step_states[step].status = :failed
        step_states[step].last_error = payload[:error]
        step_states[step].attempt = task_state.attempt
      end
    when :timer_scheduled
      timer_states[payload.fetch(:timer_id)] = TimerState.new(
        id: payload.fetch(:timer_id),
        step: payload[:step]&.to_sym,
        run_at: payload[:run_at],
        status: :scheduled,
        metadata: payload[:metadata]
      )
    when :timer_fired
      timer = timer_states[payload.fetch(:timer_id)]
      timer.status = :fired if timer
    when :signal_received
      signal_buffer.push(Signals::Message.new(
                           name: payload.fetch(:signal).to_sym,
                           payload: payload[:payload],
                           metadata: payload[:metadata],
                           received_at: payload[:received_at],
                           consumed: false
                         ))
    when :signal_consumed
      signal_buffer.consume(payload.fetch(:signal))
    when :workflow_completed, :workflow_failed, :workflow_cancelled
      execution_state.state = type.to_s.sub("workflow_", "")
    end
  end

  State.new(
    execution_state:,
    ctx:,
    history: events,
    step_states:,
    awaiting:,
    task_states:,
    timer_states:,
    signal_buffer:
  )
end