Crom is very simple scheduler for Rails.
Add this line to your Gemfile:
gem 'crom', :git => 'http://github.com/kgiszczak/crom'
and run:
bundle install
You can run crom generator now:
rails g crom:install
This command will create config/schedule.rb file where you can define your scheduled jobs. Sample file looks like this:
Crom.schedule do # every '1s' do # Some task # end # cron '* * * * *' do # Some task # end # schedule_in '20m' do # Some task # end # schedule_at 'Thu Mar 26 07:31:43 +0900 2020' do # Some task # end end
You can run crom with rake command:
rake crom:start
This is helpful for debuging, or you can run crom as a daemon with:
./scrip/crom start
By default daemon is run with production environment. You can change this by passing -e flag:
./scrip/crom start -e development
You can define error handler like this:
Crom.handle_errors do |err| Mail.deliver do from '[email protected]' to '[email protected]' subject err.message body err.backtrace.join("\n") end end
This handler will be called every time error occurs.
Only possible on cedar stack!
Create a file called Procfile in the root of your app and then add this line:
worker: bundle exec rake crom:start
Heroku by default runs just one web process so you have to add extra worker process. Run this command on your command line:
heroku scale worker=1
and now you can check if processes are running:
heroku ps
you should see something like this:
web.1 up for 2m bundle exec rails server -p $PORT worker.1 up for 2m bundle exec rake crom:start
I went into problems with installing dependencies. I couldn’t install latest version of daemons and rufus-scheduler gems. If you have this problem just add older versions of this gems to your Gemfile e.g.
gem 'daemons', '1.1.3' gem 'rufus-scheduler', '2.0.9'