Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created September 30, 2024 10:28
Show Gist options
  • Save scottwater/88430a7fc1ee3dee9e2ba26b29447e81 to your computer and use it in GitHub Desktop.
Save scottwater/88430a7fc1ee3dee9e2ba26b29447e81 to your computer and use it in GitHub Desktop.
Using JobInterator to build a multi-step Job with ActiveJob
class DemoSteppedJob < SteppedJob
# without a retry_on, the job will not be retried where it failed
retry_on StandardError, attempts: 4
def steps = %i[step_one step_two step_three]
private
def step_one(arg1, arg2)
Rails.logger.info("Step one with args #{arg1} and #{arg2}")
end
def step_two(*args)
Rails.logger.info("Step two with args #{args.inspect}")
end
def step_three(arg1, arg2)
Rails.logger.info("Step three with args #{arg1} and #{arg2}")
end
end
class SteppedJob < ApplicationJob
include JobIteration::Iteration
queue_as :default
def steps = nil
def build_enumerator(*, cursor:)
raise ArgumentError, "No steps were defined" if steps.blank?
raise ArgumentError, "Steps must be an array" unless steps.is_a?(Array)
enumerator_builder.array(steps, cursor:)
end
def each_iteration(step, *)
send(step, *)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment