Class: ActiveSaga::Stores::ActiveRecord::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/active_saga/stores/active_record.rb

Overview

Coordinates execution progression within a DB transaction.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, execution) ⇒ Processor

Returns a new instance of Processor.



443
444
445
446
# File 'lib/active_saga/stores/active_record.rb', line 443

def initialize(store, execution)
  @store = store
  @execution = execution
end

Instance Attribute Details

#executionObject (readonly)

Returns the value of attribute execution.



441
442
443
# File 'lib/active_saga/stores/active_record.rb', line 441

def execution
  @execution
end

#storeObject (readonly)

Returns the value of attribute store.



441
442
443
# File 'lib/active_saga/stores/active_record.rb', line 441

def store
  @store
end

Instance Method Details

#process!Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/active_saga/stores/active_record.rb', line 448

def process!
  return if TERMINAL_STATES.include?(execution.state)

  step = nil
  definition = nil
  workflow = nil

  check_timeouts!
  step = next_step
  return if step.nil?

  workflow_class = execution.workflow_class.constantize
  definition = workflow_class.step_definition(step.name)
  ctx = store.context_from(execution)
  workflow = workflow_class.new(context: ctx, execution_id: execution.id)

  if skip_step?(workflow, definition)
    mark_skipped(step, definition)
    store.persist_context(execution, workflow.ctx)
    ActiveSupport::Notifications.instrument("active_saga.step.skipped",
      execution_id: execution.id, step: definition.name, workflow: workflow_class.name)
    process! # continue to next
    return
  end

  attempt = step.attempts.to_i + 1
  now = store.clock.call
  step.update!(state: "running", attempts: attempt, started_at: now, scheduled_at: nil)
  execution.update!(state: "running", cursor_step: definition.name)

  ActiveSupport::Notifications.instrument("active_saga.step.started",
    execution_id: execution.id,
    workflow: workflow_class.name,
    step: definition.name,
    attempt:) do
    result = execute_step(workflow, definition, step)

    case result
    when :waiting
      # Wait steps or async initialization indicated waiting state.
      store.persist_context(execution, workflow.ctx)
    when :complete
      store.persist_context(execution, workflow.ctx)
      step.update!(state: "completed", completed_at: store.clock.call)
      ActiveSupport::Notifications.instrument("active_saga.step.completed",
        execution_id: execution.id, workflow: workflow_class.name, step: definition.name)
      new_state = next_state_after(step)
      execution.update!(state: new_state, cursor_step: next_step_name(step))
      ActiveSupport::Notifications.instrument("active_saga.execution.completed",
        execution_id: execution.id,
        workflow: execution.workflow_class) if new_state == "completed"
      process!
    else
      # Synchronous result returned.
      store.persist_context(execution, workflow.ctx)
      step.update!(state: "completed", completed_at: store.clock.call)
      ActiveSupport::Notifications.instrument("active_saga.step.completed",
        execution_id: execution.id, workflow: workflow_class.name, step: definition.name, result: result)
      new_state = next_state_after(step)
      execution.update!(state: new_state, cursor_step: next_step_name(step))
      ActiveSupport::Notifications.instrument("active_saga.execution.completed",
        execution_id: execution.id,
        workflow: execution.workflow_class) if new_state == "completed"
      process!
    end
  end
rescue => error
  raise if step.nil? || definition.nil? || workflow.nil?

  handle_error(step, definition, workflow, error)
end