-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Scheduled Jobs
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.
For recurring tasks, I recommend using the whenever or clockwork gems. I do not recommend cron gems that integrate directly with Sidekiq as they typically add more complexity to Sidekiq than I'm comfortable with.
With whenever, you'll just do something like this in config/schedule.rb:
every 1.day, :at => '12:01 pm' do
runner 'Warehouse::FtpPull.perform_async'
endIf you are worried about the overhead of booting your Rails app every minute, 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://...' }
end
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 1.day, :at => '12:01 pm' do
job 'Warehouse::FtpPull'
end
Sidekiq checks for scheduled jobs every 15 seconds by default. You can adjust this interval:
Sidekiq.configure_server do |config|
config.poll_interval = 15
endThis means each Sidekiq process checks every 15 seconds. If you have 15 Sidekiq processes, you'll be checking on average every second and this is way too often. Internally Sidekiq will adjust poll_interval to ensure that an entire set of Sidekiq processes only checks every 15 seconds on average. Override the default poll_interval at your own peril.