Skip to content

Instantly share code, notes, and snippets.

@phil-monroe
Last active December 12, 2015 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phil-monroe/4703411 to your computer and use it in GitHub Desktop.
Save phil-monroe/4703411 to your computer and use it in GitHub Desktop.
Embedding rufus scheduler within sidekiq. Put this file into your rails `config/initializers` folder.
require 'rufus/scheduler'
require 'redis'
require 'redis-lock'
if $0 == 'sidekiq'
$redis = Redis.new
def run_rake task
$redis.lock_for_update(task, 100, 0) do
puts "scheduling #{task}"
Rake::Task[task].reenable
Rake::Task[task].invoke
end
rescue => e
puts e.message
end
scheduler = Rufus::Scheduler.start_new
scheduler.cron '0 8 * * *' do
run_rake "schedule:daily_mailer"
end
scheduler.cron '0 0 * * *' do
run_rake "schedule:midnight_job"
end
scheduler.cron '0 8 * * 6' do
run_rake "schedule:weekly_job"
end
end
@phil-monroe
Copy link
Author

Updated the gist to use rufus scheduler. Cleans things up a ton and has better support for cron-like scheduling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment