Skip to content

Commit

Permalink
Add around iterate callback
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavompo committed Jan 2, 2024
1 parent 92712b9 commit 723a7d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/job-iteration/iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def inspected_cursor

included do |_base|
define_callbacks :start
define_callbacks :iterate
define_callbacks :shutdown
define_callbacks :complete

Expand Down Expand Up @@ -70,6 +71,10 @@ def on_complete(*filters, &blk)
set_callback(:complete, :after, *filters, &blk)
end

def around_iterate(&blk)
set_callback(:iterate, :around, &blk)
end

private

def ban_perform_definition
Expand Down Expand Up @@ -173,7 +178,9 @@ def iterate_with_enumerator(enumerator, arguments)
tags = instrumentation_tags.merge(cursor_position: cursor_from_enumerator)
ActiveSupport::Notifications.instrument("each_iteration.iteration", tags) do
found_record = true
each_iteration(object_from_enumerator, *arguments)
run_callbacks(:iterate) do
each_iteration(object_from_enumerator, *arguments)
end
self.cursor_position = cursor_from_enumerator
end

Expand Down
9 changes: 9 additions & 0 deletions test/unit/active_job_iteration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SimpleIterationJob < ActiveJob::Base
self.on_complete_called = 0
cattr_accessor :on_shutdown_called, instance_accessor: false
self.on_shutdown_called = 0
cattr_accessor :around_iterate_called, instance_accessor: false
self.around_iterate_called = 0

on_start do
self.class.on_start_called += 1
Expand All @@ -29,6 +31,11 @@ class SimpleIterationJob < ActiveJob::Base
on_shutdown do
self.class.on_shutdown_called += 1
end

around_iterate do |_, block|
self.class.around_iterate_called += 1
block.call
end
end

class MultiArgumentIterationJob < SimpleIterationJob
Expand Down Expand Up @@ -333,6 +340,7 @@ def setup
klass.on_start_called = 0
klass.on_complete_called = 0
klass.on_shutdown_called = 0
klass.around_iterate_called = 0
end
JobShouldExitJob.records_performed = []
super
Expand Down Expand Up @@ -365,6 +373,7 @@ def test_works_with_private_methods
assert_equal(1, PrivateIterationJob.on_start_called)
assert_equal(1, PrivateIterationJob.on_complete_called)
assert_equal(1, PrivateIterationJob.on_shutdown_called)
assert_equal(3, PrivateIterationJob.around_iterate_called)
end

def test_failing_job
Expand Down

0 comments on commit 723a7d9

Please sign in to comment.