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.



489
490
491
492
# File 'lib/active_saga/stores/active_record.rb', line 489

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

Instance Attribute Details

#executionObject (readonly)

Returns the value of attribute execution.



487
488
489
# File 'lib/active_saga/stores/active_record.rb', line 487

def execution
  @execution
end

#storeObject (readonly)

Returns the value of attribute store.



487
488
489
# File 'lib/active_saga/stores/active_record.rb', line 487

def store
  @store
end

Instance Method Details

#process!Object



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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/active_saga/stores/active_record.rb', line 494

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