Skip to content

Commit

Permalink
Add sidekiq_option for on-conflict-reschedule perform_in time
Browse files Browse the repository at this point in the history
Add schedule_in sidekiq job option to customize the
on-conflict-reschedule duration of 5 seconds. The default remains 5
seconds if no schedule_in is provided.
  • Loading branch information
Graham Marlow committed Oct 16, 2023
1 parent 36ffe8f commit 4f5ca29
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/sidekiq_unique_jobs/on_conflict/reschedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def initialize(item, redis_pool = nil)
# This will mess up sidekiq stats because a new job is created
def call
if sidekiq_job_class?
if job_class.set(queue: item["queue"].to_sym).perform_in(5, *item[ARGS])
schedule_in = job_class.get_sidekiq_options["schedule_in"] || 5
if job_class.set(queue: item["queue"].to_sym).perform_in(schedule_in, *item[ARGS])
reflect(:rescheduled, item)
else
reflect(:reschedule_failed, item)
Expand Down
15 changes: 15 additions & 0 deletions spec/sidekiq_unique_jobs/on_conflict/reschedule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@
allow(UniqueJobOnConflictReschedule).to receive(:perform_in).and_call_original
end

context "when schedule_in is set to ten seconds" do
around do |block|
UniqueJobOnConflictReschedule.use_options({schedule_in: 10 }) do
block.call
end
end

it "schedules a job ten seconds from now" do
expect { call }.to change { schedule_count }.by(1)

expect(UniqueJobOnConflictReschedule).to have_received(:perform_in)
.with(10, *item["args"])
end
end

it "schedules a job five seconds from now" do
expect { call }.to change { schedule_count }.by(1)

Expand Down

0 comments on commit 4f5ca29

Please sign in to comment.