Class: SolidFlow::Stores::ActiveRecord
- Defined in:
- lib/solid_flow/stores/active_record.rb
Constant Summary collapse
- THREAD_EXECUTION =
:__solidflow_current_execution__
Instance Attribute Summary
Attributes inherited from Base
#event_serializer, #logger, #time_provider
Instance Method Summary collapse
- #append_event(execution_id:, type:, payload:, idempotency_key: nil) ⇒ Object
- #enqueue_execution(execution_id:, reason:) ⇒ Object
- #load_history(execution_id) ⇒ Object
- #mark_timer_fired(timer_id:) ⇒ Object
- #persist_context(execution_id:, ctx:) ⇒ Object
- #persist_signal_consumed(execution_id:, signal_name:) ⇒ Object
- #query_execution(execution_id:, workflow_class:) {|state| ... } ⇒ Object
- #record_compensation_failure(execution_id:, step:, compensation_task:, error:) ⇒ Object
- #record_compensation_result(execution_id:, step:, compensation_task:, result:) ⇒ Object
- #record_task_failure(execution_id:, workflow_class:, step:, attempt:, error:, retryable:) ⇒ Object
- #record_task_result(execution_id:, workflow_class:, step:, result:, attempt:, idempotency_key:) ⇒ Object
- #schedule_compensation(execution_id:, workflow_class:, step:, compensation_task:, context:) ⇒ Object
- #schedule_task(execution_id:, step:, task:, arguments:, headers:, run_at: nil) ⇒ Object
- #schedule_timer(execution_id:, step:, run_at:, instruction:, metadata:) ⇒ Object
- #signal_execution(execution_id:, workflow_class:, signal_name:, payload:) ⇒ Object
- #start_execution(workflow_class:, input:, graph_signature:) ⇒ Object
- #update_execution(execution_id:, attributes:) ⇒ Object
- #with_execution(execution_id, lock: true) ⇒ Object
Methods inherited from Base
Constructor Details
This class inherits a constructor from SolidFlow::Stores::Base
Instance Method Details
#append_event(execution_id:, type:, payload:, idempotency_key: nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/solid_flow/stores/active_record.rb', line 113 def append_event(execution_id:, type:, payload:, idempotency_key: nil) execution = execution_for(execution_id) next_sequence = (execution.events.maximum(:sequence) || 0) + 1 execution.events.create!( sequence: next_sequence, event_type: type.to_s, payload: payload, recorded_at: time_provider.call, idempotency_key: ) end |
#enqueue_execution(execution_id:, reason:) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/solid_flow/stores/active_record.rb', line 137 def enqueue_execution(execution_id:, reason:) SolidFlow.instrument( "solidflow.execution.enqueued", execution_id:, reason: ) SolidFlow::Jobs::RunExecutionJob.perform_later(execution_id) end |
#load_history(execution_id) ⇒ Object
107 108 109 110 111 |
# File 'lib/solid_flow/stores/active_record.rb', line 107 def load_history(execution_id) SolidFlow::Event.where(execution_id:) .ordered .map(&:to_replay_event) end |
#mark_timer_fired(timer_id:) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/solid_flow/stores/active_record.rb', line 288 def mark_timer_fired(timer_id:) timer = SolidFlow::Timer.lock.find(timer_id) return if timer.fired? timer.update!(status: "fired", fired_at: time_provider.call) append_event( execution_id: timer.execution_id, type: :timer_fired, payload: { timer_id: timer.id, step: timer.step } ) enqueue_execution( execution_id: timer.execution_id, reason: :timer_fired ) end |
#persist_context(execution_id:, ctx:) ⇒ Object
132 133 134 135 |
# File 'lib/solid_flow/stores/active_record.rb', line 132 def persist_context(execution_id:, ctx:) execution = execution_for(execution_id) execution.update!(ctx: ctx.deep_stringify_keys) end |
#persist_signal_consumed(execution_id:, signal_name:) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/solid_flow/stores/active_record.rb', line 309 def persist_signal_consumed(execution_id:, signal_name:) = SolidFlow::SignalMessage .where(execution_id:, signal_name: signal_name.to_s, status: "pending") .order(:received_at) .first return unless .update!( status: "consumed", consumed_at: time_provider.call ) append_event( execution_id:, type: :signal_consumed, payload: { signal: signal_name } ) SolidFlow.instrument( "solidflow.signal.consumed", execution_id:, signal: signal_name ) end |
#query_execution(execution_id:, workflow_class:) {|state| ... } ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/solid_flow/stores/active_record.rb', line 79 def query_execution(execution_id:, workflow_class:) execution = SolidFlow::Execution.find(execution_id) events = load_history(execution_id) state = Replay.new( workflow_class:, events:, execution_record: serialize_execution(execution) ).call yield(state) end |
#record_compensation_failure(execution_id:, step:, compensation_task:, error:) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/solid_flow/stores/active_record.rb', line 402 def record_compensation_failure(execution_id:, step:, compensation_task:, error:) append_event( execution_id:, type: :compensation_failed, payload: { step: step, task: compensation_task, error: error } ) SolidFlow.instrument( "solidflow.compensation.failed", execution_id:, step: step, task: compensation_task, error: error ) end |
#record_compensation_result(execution_id:, step:, compensation_task:, result:) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/solid_flow/stores/active_record.rb', line 382 def record_compensation_result(execution_id:, step:, compensation_task:, result:) append_event( execution_id:, type: :compensation_completed, payload: { step: step, task: compensation_task, result: result } ) SolidFlow.instrument( "solidflow.compensation.completed", execution_id:, step: step, task: compensation_task, result: result ) end |
#record_task_failure(execution_id:, workflow_class:, step:, attempt:, error:, retryable:) ⇒ Object
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 |
# File 'lib/solid_flow/stores/active_record.rb', line 214 def record_task_failure(execution_id:, workflow_class:, step:, attempt:, error:, retryable:) execution = execution_for(execution_id) execution.update!( last_error: error, state: retryable ? "running" : "failed" ) append_event( execution_id:, type: :task_failed, payload: { step: step, attempt: attempt, error: error, retryable: retryable } ) if retryable enqueue_execution( execution_id:, reason: :task_failed ) else append_event( execution_id:, type: :workflow_failed, payload: { step: step, error: error } ) SolidFlow.instrument( "solidflow.execution.failed", execution_id:, step: step, error: error ) end end |
#record_task_result(execution_id:, workflow_class:, step:, result:, attempt:, idempotency_key:) ⇒ Object
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 |
# File 'lib/solid_flow/stores/active_record.rb', line 155 def record_task_result(execution_id:, workflow_class:, step:, result:, attempt:, idempotency_key:) execution = execution_for(execution_id) existing = execution.events.where( event_type: "task_completed", idempotency_key: ).first return if existing ctx = execution.ctx_hash ctx[step.to_s] = result steps = workflow_class.steps next_index = execution.cursor_index + 1 next_step = steps[next_index]&.name new_state = next_step ? "running" : "completed" execution.assign_attributes( ctx: ctx.deep_stringify_keys, cursor_index: next_index, cursor_step: next_step&.to_s, state: new_state, last_error: nil ) execution.save! append_event( execution_id:, type: :task_completed, payload: { step: step, result: result, attempt: attempt, ctx_snapshot: ctx, idempotency_key: }, idempotency_key: idempotency_key ) if new_state == "completed" append_event( execution_id:, type: :workflow_completed, payload: {} ) SolidFlow.instrument( "solidflow.execution.completed", execution_id:, workflow: workflow_class.workflow_name ) else enqueue_execution( execution_id:, reason: :task_completed ) end end |
#schedule_compensation(execution_id:, workflow_class:, step:, compensation_task:, context:) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/solid_flow/stores/active_record.rb', line 337 def schedule_compensation(execution_id:, workflow_class:, step:, compensation_task:, context:) execution = execution_for(execution_id) already_scheduled = execution.events.where(event_type: "compensation_scheduled") .where("payload ->> 'step' = ?", step.to_s) .where("payload ->> 'task' = ?", compensation_task.to_s) .exists? return if already_scheduled append_event( execution_id:, type: :compensation_scheduled, payload: { step: step, task: compensation_task } ) headers = { execution_id: execution_id, step_name: step, attempt: 1, idempotency_key: Idempotency.digest(execution_id, step, "compensation"), workflow_name: workflow_class.workflow_name, metadata: execution., compensation: true, compensation_task: compensation_task } SolidFlow::Jobs::RunTaskJob.perform_later( execution_id, step, compensation_task, context, headers ) SolidFlow.instrument( "solidflow.compensation.scheduled", execution_id:, step: step, task: compensation_task ) end |
#schedule_task(execution_id:, step:, task:, arguments:, headers:, run_at: nil) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/solid_flow/stores/active_record.rb', line 146 def schedule_task(execution_id:, step:, task:, arguments:, headers:, run_at: nil) job = SolidFlow::Jobs::RunTaskJob if run_at job.set(wait_until: run_at).perform_later(execution_id, step, task, arguments, headers) else job.perform_later(execution_id, step, task, arguments, headers) end end |
#schedule_timer(execution_id:, step:, run_at:, instruction:, metadata:) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/solid_flow/stores/active_record.rb', line 257 def schedule_timer(execution_id:, step:, run_at:, instruction:, metadata:) execution = execution_for(execution_id) timer = execution.timers.create!( step: step.to_s, run_at: run_at, status: "scheduled", instruction: instruction, metadata: ) append_event( execution_id:, type: :timer_scheduled, payload: { step: step, timer_id: timer.id, run_at: run_at, metadata: } ) SolidFlow.instrument( "solidflow.timer.scheduled", execution_id: execution.id, timer_id: timer.id, step: step, run_at: run_at ) end |
#signal_execution(execution_id:, workflow_class:, signal_name:, payload:) ⇒ Object
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 |
# File 'lib/solid_flow/stores/active_record.rb', line 43 def signal_execution(execution_id:, workflow_class:, signal_name:, payload:) execution = SolidFlow::Execution.find(execution_id) = execution..create!( signal_name: signal_name.to_s, payload: payload, metadata: {}, status: "pending", received_at: time_provider.call ) append_event( execution_id: execution.id, type: :signal_received, payload: { signal: signal_name, payload: payload, metadata: ., received_at: .received_at } ) enqueue_execution( execution_id: execution.id, reason: :signal ) SolidFlow.instrument( "solidflow.signal.received", execution_id: execution.id, workflow: workflow_class.workflow_name, signal: signal_name, payload: ) end |
#start_execution(workflow_class:, input:, graph_signature:) ⇒ Object
10 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 |
# File 'lib/solid_flow/stores/active_record.rb', line 10 def start_execution(workflow_class:, input:, graph_signature:) execution_id = SolidFlow.configuration.id_generator.call now = time_provider.call first_step = workflow_class.steps.first&.name input_payload = input.respond_to?(:deep_stringify_keys) ? input.deep_stringify_keys : input execution = SolidFlow::Execution.create!( id: execution_id, workflow: workflow_class.workflow_name, state: "running", ctx: input_payload, cursor_step: first_step&.to_s, cursor_index: 0, graph_signature: graph_signature, metadata: {}, started_at: now, updated_at: now ) append_event( execution_id: execution.id, type: :workflow_started, payload: { input: input } ) enqueue_execution( execution_id: execution.id, reason: :start ) build_handle(execution) end |
#update_execution(execution_id:, attributes:) ⇒ Object
127 128 129 130 |
# File 'lib/solid_flow/stores/active_record.rb', line 127 def update_execution(execution_id:, attributes:) execution = execution_for(execution_id) execution.update!(normalize_attributes(attributes)) end |
#with_execution(execution_id, lock: true) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/solid_flow/stores/active_record.rb', line 91 def with_execution(execution_id, lock: true) SolidFlow::Execution.transaction do relation = SolidFlow::Execution.where(id: execution_id) relation = relation.lock("FOR UPDATE SKIP LOCKED") if lock execution = relation.first raise Errors::ExecutionNotFound, execution_id unless execution previous = Thread.current[THREAD_EXECUTION] Thread.current[THREAD_EXECUTION] = execution yield serialize_execution(execution) ensure Thread.current[THREAD_EXECUTION] = previous end end |