Ruby on Rails + Sidekiq: How do I change a scheduled job start time?

ruby, ruby-on-rails, sidekiq

Solution

I dont think editing/updating jobs is the way. I usually create new job each time something changes. And then in the job itself check if the time to execute certain task is right... If the time is right, continue with the task otherwise just skip...

Problem

I started working with Sidekiq recently and noticed that it has an awesome feature that I've been searching for a long time: ``` UserMailer.delay_until(5.days.from_now).find_more_friends_email ``` Basically I can schedule a job in the future so I don't need my app to poll continuously for new events with a start time. Now this works like a charm, but how do I change the start time of a job? Often some scheduled events will have their start time changed. How do I replicate this in sidekiq? I know I can delete the job and create a new one, but is it possible to just modify the start time? EDIT: I built upon Oto Brglez's idea and here is the documented code: ``` module TaskStuff class TaskSetter include Sidekiq::Worker sidekiq_options retry: false def perform(task_id) task = Task.find(task_id) # Get the worker that's performing this job if worker = AppHelpers.find_worker(jid) # If the worker matches the "at" timestamp then this is the right one and we should execute it if worker.last["payload"]["at"].to_s.match(/(\d*\.\d{0,3})\d*/)[1] == task.start.to_f.to_s.match(/(\d*\.\d{0,3})\d*/)[1] task.execute else custom_logger.debug("This worker is the wrong one. Skipping...") end else custom_logger.error("We couldn't find the worker") end end end end module AppHelpers [...] def self.find_worker(jid) Sidekiq::Workers.new.select {|e| e.last["payload"]["jid"] == jid}.first end [...] end > task = Task.create(start: 5.hours.from_now) > TaskStuff::TastSetter.perform_at(task.start, task.id) ``` Now if I do this ``` > task.update_attributes(start: 4.hours.from_now) > TaskStuff::TastSetter.perform_at(task.start, task.id) ``` the task will get executed in 4 hours and the other job (that will get executed in 5 hours) will get ignored and removed when it reaches it's time. Initially I tried using `Time.now` instead of `worker.last["payload"]["at"]` but that could have been pretty inaccurate because a scheduled job will not always get executed on time. There is a check interval of 15 seconds, and if all workers are busy elsewhere, the job could be delayed further. I had to use `Regexp` for matching the start time because when reading the `task.start` I might have gotten a float with a different number of decimals and the if condition would not pass. This way I'm getting both values to 3 decimal. I found that the only way of getting the "at" attribute of the job is by getting it through the worker. If I were to ask Redis or use Mike's `Sidekiq::ScheduledSet.new` I would not get the current job because it would have already been pulled out of Redis. EDIT 2: For anyone interested, I went with a similar but different approach. Basically instead of comparing the start time of the `Task`, I added an extra field to the model and Sidekiq call, called `start_token`. If the sidekiq job has been called with the same token that the object has then it's valid, otherwise discard and skip the job. The token gets updated every time the model changes start_time.

Original source