evoo
evoo

Reputation: 300

How do you go about rescheduling a job in Sidekiq?

I have implemented scheduled notifications using Sidekiq. I want to also add an ability of rescheduling jobs that haven't been run yet. I do see a reschedule method in the sidekiq api code https://github.com/mperham/sidekiq/blob/061068c551bd96eb6aa790dda0a4dad85bf55381/lib/sidekiq/api.rb but I am not sure what to pass as the arguments when initializing the SortedEntry class. There are no examples in the documentation so I was wondering if anybody has any experience with Sidekiq rescheduling? By the way I grab the jid when I create a job:

message_jid = MessagesWorker.perform_in(@message.deliver_at, msgs2_ids, @message.id)

Upvotes: 5

Views: 5237

Answers (2)

Mike Perham
Mike Perham

Reputation: 22208

You do this:

Sidekiq::ScheduledSet.new.find_job(@message.jid).reschedule(1.day.from_now)

The find_job call is very slow and will not scale to lots of jobs in the scheduled set. I recommend you either:

  1. Use job cancellation, as documented on the FAQ wiki page.
  2. Use Sidekiq Pro's higher performance API extensions.

Upvotes: 8

evoo
evoo

Reputation: 300

Maybe there is a simpler way of rescheduling a message but I managed to do it somehow. My code:

scheduler = Sidekiq::ScheduledSet.new
job = scheduler.select {|s| s.klass == 'MessagesWorker' && s.jid == @message.jid }.first
entry = Sidekiq::SortedEntry.new(job.parent, job.score, job.item)
entry.reschedule(params[:message][:deliver_at])

Upvotes: 0

Related Questions