Question

How to add the dynamic date on sidekiq-scheduler in rails

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. Any one idea how to change scheduler in dynamic.


Submit an answer


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!

Sign In or Sign Up to Answer

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.

KFSys
Site Moderator
Site Moderator badge
September 12, 2023

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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel