Last active
December 12, 2015 02:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the gist to use rufus scheduler. Cleans things up a ton and has better support for cron-like scheduling.