Class: AcidicJob::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/acidic_job/step.rb

Overview

Each AcidicJob::Step requires two phases: [1] execution and [2] progression

Instance Method Summary collapse

Constructor Details

#initialize(step, run, job, step_result = nil) ⇒ Step

Returns a new instance of Step.



6
7
8
9
10
11
# File 'lib/acidic_job/step.rb', line 6

def initialize(step, run, job, step_result = nil)
  @step = step
  @run = run
  @job = job
  @step_result = step_result
end

Instance Method Details

#executeObject

The execution phase performs the work of the defined step



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/acidic_job/step.rb', line 14

def execute
  rescued_error = false
  step_callable = wrap_step_as_acidic_callable @step

  begin
    @run.with_lock do
      @step_result = step_callable.call(@run)
    end
  # QUESTION: Can an error not inherit from StandardError
  rescue StandardError => e
    rescued_error = e
    raise e
  ensure
    if rescued_error
      # If we're leaving under an error condition, try to unlock the job
      # run right away so that another request can try again.
      begin
        @run.update_columns(locked_at: nil, error_object: rescued_error)
      rescue StandardError => e
        # We're already inside an error condition, so swallow any additional
        # errors from here and just send them to logs.
        # TODO: implement and use a logger here
        puts "Failed to unlock AcidicJob::Run #{@run.id} because of #{e}."
      end
    end
  end
end

#progressObject

The progression phase advances the job run state machine onto the next step



43
44
45
46
47
# File 'lib/acidic_job/step.rb', line 43

def progress
  @run.with_lock do
    @step_result.call(run: @run)
  end
end