123456789_123456789_123456789_123456789_123456789_

Class: ActiveJob::Continuation::Step

Relationships & Source Files
Inherits: Object
Defined in: activejob/lib/active_job/continuation/step.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, cursor, resumed:, &checkpoint_callback) ⇒ Step

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 25

def initialize(name, cursor, resumed:, &checkpoint_callback)
  @name = name.to_sym
  @initial_cursor = cursor
  @cursor = cursor
  @resumed = resumed
  @checkpoint_callback = checkpoint_callback
end

Instance Attribute Details

#advanced?Boolean (readonly)

Has the cursor been advanced during this job execution?

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 61

def advanced?
  initial_cursor != cursor
end

#checkpoint_callback (readonly, private)

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 74

attr_reader :checkpoint_callback, :initial_cursor

#cursor (readonly)

The cursor for the step.

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 23

attr_reader :cursor

#initial_cursor (readonly, private)

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 74

attr_reader :checkpoint_callback, :initial_cursor

#name (readonly)

The name of the step.

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 20

attr_reader :name

#resumed?Boolean (readonly)

Has this step been resumed from a previous job execution?

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 56

def resumed?
  @resumed
end

Instance Method Details

#advance!(from: nil)

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.

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 49

def advance!(from: nil)
  from = cursor if from.nil?
  raise UnadvanceableCursorError, "Cursor class '#{from.class}' does not implement succ, " unless from.respond_to?(:succ)
  set! from.succ
end

#checkpoint!

Check if the job should be interrupted, and if so raise an Interrupt exception. The job will be requeued for retry.

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 35

def checkpoint!
  checkpoint_callback.call
end

#description

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 69

def description
  "at '#{name}', cursor '#{cursor.inspect}'"
end

#set!(cursor)

Set the cursor and interrupt the job if necessary.

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 40

def set!(cursor)
  @cursor = cursor
  checkpoint!
end

#to_a

[ GitHub ]

  
# File 'activejob/lib/active_job/continuation/step.rb', line 65

def to_a
  [ name.to_s, cursor ]
end