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.



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

def current_run_iterations
  @current_run_iterations
end

#cursor_positionObject (readonly)

Returns the value of attribute cursor_position.



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

def cursor_position
  @cursor_position
end

#executionsObject (readonly)

Returns the value of attribute executions.



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

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.



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

def start_time
  @start_time
end

#times_interruptedObject (readonly)

Returns the value of attribute times_interrupted.



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

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.



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

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
29
# 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
      defined?(Sidekiq::CLI) &&
        Sidekiq::CLI.instance.launcher.stopping?
    end
  end

  super
end

Instance Method Details

#build_enumeratorEnumerator

The enumerator to be iterated over.

Returns:

  • (Enumerator)

Raises:

  • (NotImplementedError)

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



112
113
114
# File 'lib/sidekiq_iteration/iteration.rb', line 112

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.



123
124
125
# File 'lib/sidekiq_iteration/iteration.rb', line 123

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

#initializeObject



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

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.



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

def on_complete
end

#on_resumeObject

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



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

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.



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

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.



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

def on_start
end

#perform(*arguments) ⇒ Object



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

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