A time-based background job scheduler daemon (just like Cron) for Rails
A time-based background job scheduler daemon (just like Cron) for Rails
Crono is a time-based background job scheduler daemon (just like Cron) for Ruby on Rails.
Currently, there is no such thing as Ruby Cron for Rails. Well, there's Whenever but it works on top of Unix Cron, so you can't manage it from Ruby. Crono is pure Ruby. It doesn't use Unix Cron and other platform-dependent things. So you can use it on all platforms supported by Ruby. It persists job states to your database using Active Record. You have full control of jobs performing process. It's Ruby, so you can understand and modify it to fit your needs.
Add the following line to your application's Gemfile:
gem 'crono'Run the bundle command to install it.
After you install Crono, you can run the generator:
rails generate crono:installIt will create a configuration file config/cronotab.rb and migration
Run the migration:
rake db:migrateNow you are ready to move forward to create a job and schedule it.
You can specify a simple job by editing config/cronotab.rb:
# config/cronotab.rb
class TestJob
def perform
puts 'Test!'
end
end
Crono.perform(TestJob).every 5.secondsThen, run a crono process:
bundle exec crono -e development
Schedule list is defined in the file config/cronotab.rb, that created using rake crono:install. The semantic is pretty straightforward:
# config/cronotab.rb
Crono.perform(TestJob).every 2.days, at: {hour: 15, min: 30}
Crono.perform(TestJob).every 1.week, on: :monday, at: "15:30"You can schedule one job a few times if you want the job to be performed a few times a day or a week:
Crono.perform(TestJob).every 1.week, on: :monday
Crono.perform(TestJob).every 1.week, on: :thursdayThe at can be a Hash:
Crono.perform(TestJob).every 1.day, at: {hour: 12, min: 15}You can schedule a job with arguments, which can contain objects that can be serialized using JSON.generate
Crono.perform(TestJob, 'some', 'args').every 1.day, at: {hour: 12, min: 15}You can set some options that not passed to the job but affect how the job will be treated by Crono. For example, you can set to truncate job logs (which stored in the database) to a certain number of records:
Crono.perform(TestJob).with_options(truncate_log: 100).every 1.week, on: :mondayCrono can use Active Job jobs from app/jobs/. Here's an example of a job:
# app/jobs/test_job.rb
class TestJob :environment do
puts "hello"
end
endPlease note that crono uses threads, so your code should be thread-safe
Run crono in your Rails project root directory:
bundle exec crono -e developmentUsage:
…To run Crono as a daemon, please add to your Gemfile:
gem 'daemons'Then:
bundle install; bundle exec crono start RAILS_ENV=developmentThere are "start", "stop", and "restart" commands.
Crono can display the current state of Crono jobs.
Add the following to your config/routes.rb:
Rails.application.routes.draw do
mount Crono::Engine, at: '/crono'
...Access management and other questions described in the wiki.
For Rails 5, in case of the errors:
`require': cannot load such file -- rack/showexceptions (LoadError)See the related issue #52
Use the capistrano-crono gem (github).
To run the tests, you need to have a database. You can use the default SQLite database:
bundle exec rspecTo publish a new version, you need to update the version number in lib/crono/version.rb and then run:
bundle exec rake releaseFeel free to create an issues
Please see LICENSE for licensing details.
No open issues yet, or sync has not completed.