Class: RigidWorkflow::Orchestrator

Inherits:
Object
  • Object
show all
Defined in:
lib/rigid_workflow/orchestrator.rb

Overview

Orchestrates workflow execution. Manages workflow starting, scheduling, and state advancement.

Class Method Summary collapse

Class Method Details

.advance!(run) ⇒ Object



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
# File 'lib/rigid_workflow/orchestrator.rb', line 32

def advance!(run)
  run.increment(:iterations)
  return if run.finished?

  raise "Cannot advance a compensating workflow" if run.compensating?

  run.start! if run.pending?

  # brakeman:ignore UnsafeReflection
  # This is intentional - workflow_class is a validated string from the workflow definition
  workflow_class = run.workflow_class.constantize
  unless workflow_class && workflow_class < RigidWorkflow::Workflow
    raise ArgumentError, "#{run.workflow_class} is not a valid Workflow"
  end

  workflow = workflow_class.new(run)

  catch(:suspend) do
    if run.running?
      workflow.run
      run.finish!
    end
  end

  run.save! if run.changed?
rescue ActivityFailedError => error
  # A step failed after exhausting all retries
  if run.running? && !run.compensating?
    failed_step = run.steps.where(status: :failed).last
    if failed_step && step_exhausted_retries?(failed_step)
      # Attempt compensation - if it raises, let it propagate
      run.compensate!
      # If compensate! succeeds, don't re-raise ActivityFailedError
      return
    end
  end

  # If we get here, no compensation was performed
  run.fail! unless run.compensating?
  raise error
rescue => error
  # Handle non-ActivityFailedError exceptions
  run.fail! unless run.compensating?
  raise error
end

.schedule(run, options = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/rigid_workflow/orchestrator.rb', line 24

def schedule(run, options = {})
  RigidWorkflow::WorkflowJob.set(
    wait: options[:wait],
    wait_until: options[:wait_until],
    priority: options[:priority]
  ).perform_later(run.id)
end

.start(workflow_class, params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rigid_workflow/orchestrator.rb', line 8

def start(workflow_class, params = {})
  RigidWorkflow.instrument(
    "workflow.start",
    workflow_class: workflow_class.name
  ) do
    run =
      RigidWorkflow::Run.create!(
        workflow_class: workflow_class.name,
        version: workflow_class.workflow_version,
        params: params
      )
    schedule(run)
    run
  end
end

.step_exhausted_retries?(step) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rigid_workflow/orchestrator.rb', line 78

def step_exhausted_retries?(step)
  step.attempts.count >= step.max_attempts
end