Class: DurableFlow::Workflow

Inherits:
ActiveJob::Base
  • Object
show all
Includes:
ActiveJob::Continuable
Defined in:
lib/durable_flow/workflow.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#workflow_runObject (readonly)

Returns the value of attribute workflow_run.



11
12
13
# File 'lib/durable_flow/workflow.rb', line 11

def workflow_run
  @workflow_run
end

Class Method Details

.discard_on(*exceptions, **options, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/durable_flow/workflow.rb', line 26

def discard_on(*exceptions, **options, &block)
  super(*exceptions, **options) do |job, error|
    begin
      block.call(job, error) if block
    ensure
      job.send(:fail_workflow_after_unhandled_error!, error) if job.is_a?(DurableFlow::Workflow)
    end
  end
end

.retry_on(*exceptions, **options, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/durable_flow/workflow.rb', line 14

def retry_on(*exceptions, **options, &block)
  return super(*exceptions, **options) unless block

  super(*exceptions, **options) do |job, error|
    begin
      block.call(job, error)
    ensure
      job.send(:fail_workflow_after_unhandled_error!, error) if job.is_a?(DurableFlow::Workflow)
    end
  end
end

Instance Method Details

#checkpoint!Object



53
54
55
56
# File 'lib/durable_flow/workflow.rb', line 53

def checkpoint!
  refresh_execution_lock!
  interrupt!(reason: :stopping) if queue_adapter.respond_to?(:stopping?) && queue_adapter.stopping?
end

#child_workflow(name, workflow_class = nil, *args, timeout: nil, on_failure: :raise, **kwargs, &block) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/durable_flow/workflow.rb', line 117

def child_workflow(name, workflow_class = nil, *args, timeout: nil, on_failure: :raise, **kwargs, &block)
  validate_child_workflow_failure_policy!(on_failure)

  base_name = name.to_s
  child = start_child_workflow_step("#{base_name}_start", workflow_class, *args, **kwargs, &block)

  wait_for_child_workflow("#{base_name}_wait", child, timeout: timeout, on_failure: on_failure)
end

#child_workflows(name, collection = nil, key: nil, timeout: nil, concurrency: nil, on_failure: :raise, &block) ⇒ Object



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
# File 'lib/durable_flow/workflow.rb', line 146

def child_workflows(name, collection = nil, key: nil, timeout: nil, concurrency: nil, on_failure: :raise, &block)
  validate_child_workflow_failure_policy!(on_failure)

  if collection.nil?
    raise ArgumentError, "Provide a child workflow collection or builder block" unless block

    builder = ChildWorkflowBuilder.new
    block.call(builder)
    collection = builder.requests
    block = nil
    key ||= :workflow_key
  end

  batches = child_workflow_batches(collection, concurrency)

  batches.flat_map do |batch|
    children = batch.map do |item|
      item_key = child_workflow_item_key(item, key)
      child = start_child_workflow_step("#{name}_#{item_key}_start") do
        block ? block.call(item) : start_child_workflow_request(item)
      end

      child.merge("key" => item_key)
    end

    children.map do |child|
      completion = wait_for_child_workflow("#{name}_#{child.fetch("key")}_wait", child, timeout: timeout, on_failure: on_failure)
      completion.to_h.with_indifferent_access.merge(
        "key" => child.fetch("key"),
        "run_id" => child.fetch("run_id"),
        "workflow_class" => child["workflow_class"] || completion_value(completion, :workflow_class),
      ).compact
    end
  end
end

#continuable_stepObject



9
# File 'lib/durable_flow/workflow.rb', line 9

alias_method :continuable_step, :step

#each_child_workflow(name, collection, key:, timeout: nil, on_failure: :raise, &block) ⇒ Object

Raises:

  • (ArgumentError)


182
183
184
185
186
# File 'lib/durable_flow/workflow.rb', line 182

def each_child_workflow(name, collection, key:, timeout: nil, on_failure: :raise, &block)
  raise ArgumentError, "Provide a block that starts each child workflow" unless block

  child_workflows(name, collection, key: key, timeout: timeout, on_failure: on_failure, &block)
end

#invoke_workflow(name, workflow_class = nil, *args, timeout: nil, on_failure: :raise, **kwargs, &block) ⇒ Object



126
127
128
# File 'lib/durable_flow/workflow.rb', line 126

def invoke_workflow(name, workflow_class = nil, *args, timeout: nil, on_failure: :raise, **kwargs, &block)
  child_workflow(name, workflow_class, *args, timeout: timeout, on_failure: on_failure, **kwargs, &block)
end

#invoke_workflows(name, collection, timeout: nil, concurrency: nil, on_failure: :raise, &block) ⇒ Object

Raises:

  • (ArgumentError)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/durable_flow/workflow.rb', line 130

def invoke_workflows(name, collection, timeout: nil, concurrency: nil, on_failure: :raise, &block)
  validate_child_workflow_failure_policy!(on_failure)
  raise ArgumentError, "Provide a block that returns workflow requests" unless block

  requests = collection.map do |item|
    request = block.call(item)
    unless request.respond_to?(:workflow_key)
      raise ArgumentError, "invoke_each blocks must return a workflow request with a stable workflow_key"
    end

    request
  end

  child_workflows(name, requests, timeout: timeout, concurrency: concurrency, on_failure: on_failure)
end

#logObject



188
189
190
# File 'lib/durable_flow/workflow.rb', line 188

def log
  @workflow_logger ||= WorkflowLogger.new(self)
end

#perform_nowObject



37
38
39
40
41
42
# File 'lib/durable_flow/workflow.rb', line 37

def perform_now
  super
rescue Exception => error
  fail_workflow_after_unhandled_error!(error)
  raise
end

#retry_job(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/durable_flow/workflow.rb', line 44

def retry_job(options = {})
  if options[:error]
    persist_retrying_workflow!(options[:error])
    failed_workflow_step&.retry!(options[:error], retry_at: retry_at_from(options))
  end

  super
end

#sleep_step(name, duration = nil, until_time: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/durable_flow/workflow.rb', line 64

def sleep_step(name, duration = nil, until_time: nil)
  durable_step(name) do
    step_record = current_workflow_step
     = step_record.
    wake_at = durable_flow_parse_time(["wake_at"]) || durable_flow_time_from(duration, explicit_time: until_time)

    raise ArgumentError, "Provide a duration or until: time for sleep step #{name.inspect}" unless wake_at

    if Time.current < wake_at
      ["wake_at"] = wake_at.utc.iso8601(9)
      step_record.update!(status: "sleeping", metadata: )
      pause_or_interrupt!(reason: :sleeping, status: "sleeping", resume_options: { wait_until: wake_at })
    end

    nil
  end
end

#step(name = nil, start: nil, isolated: false, &block) ⇒ Object



58
59
60
61
62
# File 'lib/durable_flow/workflow.rb', line 58

def step(name = nil, start: nil, isolated: false, &block)
  return StepProxy.new(self) if name.nil?

  durable_step(name, start: start, isolated: isolated, &block)
end

#wait_for_event_step(name, event_name:, timeout:, match:, allow_past_events: false, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/durable_flow/workflow.rb', line 82

def wait_for_event_step(name, event_name:, timeout:, match:, allow_past_events: false, &block)
  durable_step(name) do
    step_record = current_workflow_step
    wait = find_or_initialize_wait(step_record, event_name: event_name, timeout: timeout, match: match)

    if (event = matched_event_for(wait, allow_past_events: allow_past_events))
      wait.update!(status: "matched", workflow_event: event)
      payload = event.payload_value
      block ? block.call(payload) : payload
    elsif wait.timeout_at && Time.current >= wait.timeout_at
      wait.update!(status: "timed_out")
      step_record.update!(status: "failed", metadata: step_record..merge("timeout_at" => wait.timeout_at.utc.iso8601(9)))
      raise WaitTimeoutError.new(event_name: event_name.to_s, step_name: name.to_s)
    else
      step_record.update!(
        status: "waiting",
        metadata: step_record..merge(
          "event_name" => event_name.to_s,
          "timeout_at" => wait.timeout_at&.utc&.iso8601(9),
        ).compact,
      )

      if wait.timeout_at
        pause_or_interrupt!(reason: :waiting, status: "waiting", resume_options: { wait_until: wait.timeout_at })
      else
        pause_or_interrupt!(reason: :waiting, status: "waiting")
      end
    end
  end
end

#wait_for_workflow(name, workflow_or_run_id, timeout: nil) ⇒ Object



113
114
115
# File 'lib/durable_flow/workflow.rb', line 113

def wait_for_workflow(name, workflow_or_run_id, timeout: nil)
  StepProxy.new(self).wait_for_workflow(name, workflow_or_run_id, timeout: timeout)
end