Report this

What is the reason for this report?

How to set the dynamic scheduler in rails with specific date.

Posted on November 29, 2021

I have added the sidekiq scheduler by using sidekiq-scheduler gem in rails, but we are unable to add the dynamic date on the scheduler. Does anyone idea how to change the scheduler in dynamic.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

If you need to schedule jobs dynamically with Sidekiq, you can use the perform_at or perform_in methods which allow you to set a specific date or delay for when the job should be performed.

Here’s an example:

# schedule the job to be run 3 days from now
MyWorker.perform_in(3.days)

# schedule the job to be run at a specific date/time
time_to_run = Time.new(2023, 8, 24, 12, 0, 0) # Aug 24, 2023 at 12:00PM
MyWorker.perform_at(time_to_run)

The perform_in method schedules a job to be performed after a certain amount of time, specified as an argument. The perform_at method schedules a job to be performed at a specific time, provided as a Time object.

However, if you need to dynamically set the schedule based on some external configuration or criteria, you might want to control Sidekiq’s schedule at runtime.

This is possible using the sidekiq-scheduler’s dynamic schedule feature:

Sidekiq.set_schedule('MyWorker', {
  'every' => ['1m'], # run every minute
  'class' => 'MyWorker',
  'queue' => 'default'
})

You can call Sidekiq.set_schedule from anywhere in your application (for example, in a Rails initializer or a config file) and it will update the schedule in memory.

Remember to enable the dynamic setting in your Sidekiq configuration:

:dynamic: true

You might need to call Sidekiq.reload_schedule! after you change the schedule to ensure the new schedule gets loaded.

Remember that these dynamic schedules will be reset if the Sidekiq process restarts, as they’re only kept in memory. If you want to persist these dynamic schedules, you’d need to store them in some kind of persistent storage (like a database or a file) and then load them into Sidekiq when your application starts.

Hope this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.