Skip to content

Commit

Permalink
Add around_iteration hook
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed May 4, 2024
1 parent ab17bff commit 51a9496
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Add `around_iteration` hook

## 0.3.0 (2023-05-20)

- Allow a default retry backoff to be configured
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ class NotifyUsersJob
# Will be called when the job starts iterating. Called only once, for the first time.
end

def around_iteration
# Will be called around each iteration.
# Can be useful for some metrics collection, performance tracking etc.
yield
end

def on_resume
# Called when the job resumes iterating.
end
Expand Down
10 changes: 9 additions & 1 deletion lib/sidekiq_iteration/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def perform(*arguments)
def on_start
end

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

# A hook to override that will be called when the job resumes iterating.
def on_resume
end
Expand Down Expand Up @@ -178,7 +184,9 @@ def iterate_with_enumerator(enumerator, arguments)

enumerator.each do |object_from_enumerator, index|
found_record = true
each_iteration(object_from_enumerator, *arguments)
around_iteration do
each_iteration(object_from_enumerator, *arguments)
end
@cursor_position = index
@current_run_iterations += 1

Expand Down
14 changes: 12 additions & 2 deletions test/iteration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SimpleIterationJob

cattr_accessor :records_performed, default: []
cattr_accessor :on_start_called, default: 0
cattr_accessor :around_iteration_called, default: 0
cattr_accessor :on_resume_called, default: 0
cattr_accessor :on_complete_called, default: 0
cattr_accessor :on_shutdown_called, default: 0
Expand All @@ -23,6 +24,11 @@ def on_start
self.class.on_start_called += 1
end

def around_iteration
self.class.around_iteration_called += 1
yield
end

def on_resume
self.class.on_resume_called += 1
end
Expand All @@ -41,6 +47,7 @@ def setup
SimpleIterationJob.descendants.each do |klass|
klass.records_performed = []
klass.on_start_called = 0
klass.around_iteration_called = 0
klass.on_resume_called = 0
klass.on_complete_called = 0
klass.on_shutdown_called = 0
Expand Down Expand Up @@ -153,8 +160,8 @@ def build_enumerator(cursor:)
active_record_batches_enumerator(Product.all, cursor: cursor, batch_size: 3)
end

def each_iteration(record)
self.class.records_performed << record
def each_iteration(records)
self.class.records_performed << records
end
end

Expand All @@ -176,6 +183,7 @@ def each_iteration(record)
assert_equal(1, BatchActiveRecordIterationJob.records_performed.size)
assert_equal(3, BatchActiveRecordIterationJob.records_performed.flatten.size)
assert_equal(1, BatchActiveRecordIterationJob.on_start_called)
assert_equal(1, BatchActiveRecordIterationJob.around_iteration_called)
assert_equal(0, BatchActiveRecordIterationJob.on_resume_called)

job = peek_into_queue
Expand All @@ -188,6 +196,7 @@ def each_iteration(record)
assert_equal(2, BatchActiveRecordIterationJob.records_performed.size)
assert_equal(6, BatchActiveRecordIterationJob.records_performed.flatten.size)
assert_equal(1, BatchActiveRecordIterationJob.on_start_called)
assert_equal(2, BatchActiveRecordIterationJob.around_iteration_called)
assert_equal(1, BatchActiveRecordIterationJob.on_resume_called)

job = peek_into_queue
Expand All @@ -204,6 +213,7 @@ def each_iteration(record)
assert_equal(12, BatchActiveRecordIterationJob.records_performed.flatten.size)

assert_equal(1, BatchActiveRecordIterationJob.on_start_called)
assert_equal(4, BatchActiveRecordIterationJob.around_iteration_called)
assert_equal(2, BatchActiveRecordIterationJob.on_resume_called)
assert_equal(1, BatchActiveRecordIterationJob.on_complete_called)
end
Expand Down

0 comments on commit 51a9496

Please sign in to comment.