Skip to content

Commit

Permalink
Add sidekiq_option for on-conflict-reschedule perform_in time (#813)
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
mgmarlow authored Nov 9, 2023
1 parent 8c9f633 commit e430e39
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 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,7 @@ 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])
if job_class.set(queue: item["queue"].to_sym).perform_in(schedule_in, *item[ARGS])
reflect(:rescheduled, item)
else
reflect(:reschedule_failed, item)
Expand All @@ -30,6 +30,10 @@ def call
reflect(:unknown_sidekiq_worker, item)
end
end

def schedule_in
job_class.get_sidekiq_options["schedule_in"] || 5
end
end
end
end
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 e430e39

Please sign in to comment.