Module: SidekiqIteration::Iteration

Includes:
Enumerators
Defined in:
lib/sidekiq_iteration/iteration.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerators

#active_record_batches_enumerator, #active_record_records_enumerator, #active_record_relations_enumerator, #array_enumerator, #csv_batches_enumerator, #csv_enumerator, #nested_enumerator

Instance Attribute Details

#current_run_iterationsObject (readonly)

Returns the value of attribute current_run_iterations.



56
57
58
# File 'lib/sidekiq_iteration/iteration.rb', line 56

def current_run_iterations
  @current_run_iterations
end

#cursor_positionObject (readonly)

Returns the value of attribute cursor_position.



56
57
58
# File 'lib/sidekiq_iteration/iteration.rb', line 56

def cursor_position
  @cursor_position
end

#executionsObject (readonly)

Returns the value of attribute executions.



56
57
58
# File 'lib/sidekiq_iteration/iteration.rb', line 56

def executions
  @executions
end

#start_timeObject (readonly)

The time when the job starts running. If the job is interrupted and runs again, the value is updated.



63
64
65
# File 'lib/sidekiq_iteration/iteration.rb', line 63

def start_time
  @start_time
end

#times_interruptedObject (readonly)

Returns the value of attribute times_interrupted.



56
57
58
# File 'lib/sidekiq_iteration/iteration.rb', line 56

def times_interrupted
  @times_interrupted
end

#total_timeObject (readonly)

The total time the job has been running, including multiple iterations. The time isn’t reset if the job is interrupted.



67
68
69
# File 'lib/sidekiq_iteration/iteration.rb', line 67

def total_time
  @total_time
end

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sidekiq_iteration/iteration.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
  base.extend(Throttling)

  base.class_eval do
    throttle_on(backoff: SidekiqIteration.default_retry_backoff) do |job|
      job.class.max_job_runtime &&
        job.start_time &&
        (Time.now.utc - job.start_time) > job.class.max_job_runtime
    end

    throttle_on(backoff: SidekiqIteration.default_retry_backoff) do
      SidekiqIteration.stopping
    end
  end

  super
end

Instance Method Details

#around_iterationObject

A hook to override that will be called around each iteration. Can be useful for some metrics collection, performance tracking etc.



92
93
94
# File 'lib/sidekiq_iteration/iteration.rb', line 92

def around_iteration
  yield
end

#build_enumeratorEnumerator

The enumerator to be iterated over.

Returns:

  • (Enumerator)

Raises:

  • (NotImplementedError)

    with a message advising subclasses to implement an override for this method.



117
118
119
# File 'lib/sidekiq_iteration/iteration.rb', line 117

def build_enumerator(*)
  raise NotImplementedError, "#{self.class.name} must implement a 'build_enumerator' method"
end

#each_iterationvoid

This method returns an undefined value.

The action to be performed on each item from the enumerator.

Raises:

  • (NotImplementedError)

    with a message advising subclasses to implement an override for this method.



128
129
130
# File 'lib/sidekiq_iteration/iteration.rb', line 128

def each_iteration(*)
  raise NotImplementedError, "#{self.class.name} must implement an 'each_iteration' method"
end

#initializeObject



70
71
72
73
74
75
76
# File 'lib/sidekiq_iteration/iteration.rb', line 70

def initialize
  super
  @arguments = nil
  @job_iteration_retry_backoff = SidekiqIteration.default_retry_backoff
  @needs_reenqueue = false
  @current_run_iterations = 0
end

#on_completeObject

A hook to override that will be called when the job finished iterating.



107
108
# File 'lib/sidekiq_iteration/iteration.rb', line 107

def on_complete
end

#on_resumeObject

A hook to override that will be called when the job resumes iterating.



97
98
# File 'lib/sidekiq_iteration/iteration.rb', line 97

def on_resume
end

#on_shutdownObject

A hook to override that will be called each time the job is interrupted. This can be due to throttling (throttle enumerator), ‘max_job_runtime` configuration, or sidekiq restarting.



103
104
# File 'lib/sidekiq_iteration/iteration.rb', line 103

def on_shutdown
end

#on_startObject

A hook to override that will be called when the job starts iterating. Is called only once, for the first time.



87
88
# File 'lib/sidekiq_iteration/iteration.rb', line 87

def on_start
end

#perform(*arguments) ⇒ Object



79
80
81
82
83
# File 'lib/sidekiq_iteration/iteration.rb', line 79

def perform(*arguments)
  (arguments)
  @arguments = arguments
  interruptible_perform(*arguments)
end