Class: ActiveJob::Continuation::Step
- Inherits:
-
Object
- Object
- ActiveJob::Continuation::Step
- Defined in:
- lib/active_job/continuation/step.rb
Overview
Active Job Continuation Step
Represents a step within a continuable job.
When a step is completed, it is recorded in the job’s continuation state. If the job is interrupted, it will be resumed from after the last completed step.
Steps also have an optional cursor that can be used to track progress within the step. If a job is interrupted during a step, the cursor will be saved and passed back when the job is resumed.
It is the responsibility of the code in the step to use the cursor correctly to resume from where it left off.
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
The cursor for the step.
-
#name ⇒ Object
readonly
The name of the step.
Instance Method Summary collapse
-
#advance!(from: nil) ⇒ Object
Advance the cursor from the current or supplied value.
-
#advanced? ⇒ Boolean
Has the cursor been advanced during this job execution?.
-
#checkpoint! ⇒ Object
Check if the job should be interrupted, and if so raise an Interrupt exception.
- #description ⇒ Object
-
#initialize(name, cursor, job:, resumed:) ⇒ Step
constructor
A new instance of Step.
-
#resumed? ⇒ Boolean
Has this step been resumed from a previous job execution?.
-
#set!(cursor) ⇒ Object
Set the cursor and interrupt the job if necessary.
- #to_a ⇒ Object
Constructor Details
#initialize(name, cursor, job:, resumed:) ⇒ Step
Returns a new instance of Step.
25 26 27 28 29 30 31 |
# File 'lib/active_job/continuation/step.rb', line 25 def initialize(name, cursor, job:, resumed:) @name = name.to_sym @initial_cursor = cursor @cursor = cursor @resumed = resumed @job = job end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
The cursor for the step.
23 24 25 |
# File 'lib/active_job/continuation/step.rb', line 23 def cursor @cursor end |
#name ⇒ Object (readonly)
The name of the step.
20 21 22 |
# File 'lib/active_job/continuation/step.rb', line 20 def name @name end |
Instance Method Details
#advance!(from: nil) ⇒ Object
Advance the cursor from the current or supplied value
The cursor will be advanced by calling the succ method on the cursor. An UnadvanceableCursorError error will be raised if the cursor does not implement succ.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_job/continuation/step.rb', line 49 def advance!(from: nil) from = cursor if from.nil? begin to = from.succ rescue NoMethodError raise UnadvanceableCursorError, "Cursor class '#{from.class}' does not implement 'succ'" end set! to end |
#advanced? ⇒ Boolean
Has the cursor been advanced during this job execution?
67 68 69 |
# File 'lib/active_job/continuation/step.rb', line 67 def advanced? initial_cursor != cursor end |
#checkpoint! ⇒ Object
Check if the job should be interrupted, and if so raise an Interrupt exception. The job will be requeued for retry.
35 36 37 |
# File 'lib/active_job/continuation/step.rb', line 35 def checkpoint! job.checkpoint! end |
#description ⇒ Object
75 76 77 |
# File 'lib/active_job/continuation/step.rb', line 75 def description "at '#{name}', cursor '#{cursor.inspect}'" end |
#resumed? ⇒ Boolean
Has this step been resumed from a previous job execution?
62 63 64 |
# File 'lib/active_job/continuation/step.rb', line 62 def resumed? @resumed end |
#set!(cursor) ⇒ Object
Set the cursor and interrupt the job if necessary.
40 41 42 43 |
# File 'lib/active_job/continuation/step.rb', line 40 def set!(cursor) @cursor = cursor checkpoint! end |
#to_a ⇒ Object
71 72 73 |
# File 'lib/active_job/continuation/step.rb', line 71 def to_a [ name.to_s, cursor ] end |