Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Scheduled Jobs

Jonathan Hyman edited this page · 23 revisions

Sidekiq allows you to schedule the time when a job will be executed. You use perform_in(interval, *args) or perform_at(timestamp, *args) rather than the standard perform_async(*args):

MyWorker.perform_in(3.hours, 'mike', 1)
MyWorker.perform_at(3.hours.from_now, 'mike', 1)

This is useful for example if you want to send the user an email 3 hours after they sign up.

Recurring Jobs

For recurring tasks, I recommend using the whenever gem with cron directly. I do not recommend gems that integrate directly with Sidekiq or reinvent cron as they typically add more complexity than I'm comfortable with.

With whenever, you'll just do something like this in config/schedule.rb:

every, :at => '12:01 pm' do
  runner 'Warehouse::FtpPull.perform_async'

If you are worried about the overhead of booting your Rails app to push jobs, you can create a stripped down job type that just knows how to push a job to Redis without booting Rails:

#! /usr/bin/env ruby
# scripts/sidekiq_pusher.rb
# bundle exec scripts/sidekiq_pusher.rb Warehouse::FtpPull
klass = ARGV[0]
require 'sidekiq'
Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://...' }
Sidekiq::Client.push('class' => klass, 'args' => [])

And then in your whenever config/schedule.rb:

job_type :job, "cd :path && :environment_variable=:environment bundle exec script/sidekiq_pusher.rb :task :output"

every, :at => '12:01 pm' do
  job 'Warehouse::FtpPull'

Checking for New Jobs

Sidekiq checks for scheduled jobs every 15 seconds by default. You can adjust this interval:

Sidekiq.configure_server do |config|
  config.poll_interval = 15

This means each Sidekiq process checks every 15 seconds. If you have 15 Sidekiq processes, you'll be checking on average every second and this may be too often for your application. If you do not set poll_interval, Sidekiq will automatically adjust the poll interval to ensure that your entire set of Sidekiq processes checks every 15 seconds on average. Override the default poll_interval at your own peril.

Example: if you have 20 Sidekiq processes and want to check every 5 seconds, you'd set the poll_interval to 100.

Previous: Advanced Options Next: Delayed Extensions

Something went wrong with that request. Please try again.