• Stars
    star
    688
  • Rank 65,694 (Top 2 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A time-based background job scheduler daemon (just like Cron) for Rails

Job scheduler for Rails

Gem Version

Crono is a time-based background job scheduler daemon (just like Cron) for Ruby on Rails.

The Purpose

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.

Web UI

Installation

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:install

It will create a configuration file config/cronotab.rb and migration
Run the migration:

rake db:migrate

Now you are ready to move forward to create a job and schedule it.

Compatibility

  • Crono v1.1.2 and older are compatible with Ruby 2.7.x and older
  • Crono v2.0.0 and newer are compatible with Ruby 2.7.x and newer

Usage

The basic usage

You can specigy a simple job by editing config/cronotab.rb:

# config/cronotab.rb
class TestJob
  def perform
    puts 'Test!'
  end
end

Crono.perform(TestJob).every 5.seconds

Then, run a crono process:

bundle exec crono -e development

Job Schedule

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: :thursday

The 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: :monday

Job classes

Crono can use Active Job jobs from app/jobs/. Here's an example of a job:

# app/jobs/test_job.rb
class TestJob < ActiveJob::Base
  def perform(options)
    # put you scheduled code here
    # Comments.deleted.clean_up...
  end
end

The ActiveJob jobs are convenient because you can use one job in both periodic and enqueued ways. But Active Job is not required. Any class can be used as a crono job if it implements a method perform:

class TestJob # This is not an Active Job job, but pretty legal Crono job.
  def perform(*args)
    # put you scheduled code here
    # Comments.deleted.clean_up...
  end
end

Run rake tasks

Here's an example of a Rake Task within a job:

# config/cronotab.rb
require 'rake'

Rails.app_class.load_tasks

class Test
  def perform
    Rake::Task['crono:hello'].execute
  end
end

Crono.perform(Test).every 5.seconds

With the rake task of:

# lib/tasks/test.rake
namespace :crono do
  desc 'Update all tables'
  task :hello => :environment do
    puts "hello"
  end
end

Please note that crono uses threads, so your code should be thread-safe

Run crono

Run crono in your Rails project root directory:

bundle exec crono -e development

Usage:

Usage: crono [options] [start|stop|restart|run]
    -C, --cronotab PATH              Path to cronotab file (Default: config/cronotab.rb)
    -L, --logfile PATH               Path to writable logfile (Default: log/crono.log)
    -P, --pidfile PATH               Deprecated! use --piddir with --process_name; Path to pidfile (Default: )
    -D, --piddir PATH                Path to piddir (Default: tmp/pids)
    -N, --process_name NAME          Name of the process (Default: crono)
    -m, --monitor                    Start monitor process for a deamon (Default false)
    -e, --environment ENV            Application environment (Default: development)

Run as a daemon

To run Crono as a daemon, please add to your Gemfile:

gem 'daemons'

Then:

bundle install; bundle exec crono start RAILS_ENV=development

There are "start", "stop", and "restart" commands.

Web UI

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.

Known issues

For Rails 5, in case of the errors:

`require': cannot load such file -- rack/showexceptions (LoadError)

See the related issue #52

Capistrano

Use the capistrano-crono gem (github).

Support

Feel free to create an issues

License

Please see LICENSE for licensing details.

More Repositories

1

awesome-genetics

A curated list of awesome personal genomics software, libraries, and educational resources.
123
star
2

viewssld

viewssld is a free, open source, non-terminating SSLv2/SSLv3/TLS traffic decryption daemon for Snort, and other Network Intrusion Detection Systems (IDS).
C
74
star
3

rgb

A simple Ruby library built to handle easy conversion and manipulation of colors.
Ruby
52
star
4

libdssl

DSSL library: network capture and SSL decryption toolkit
C
25
star
5

dna-raw-tools

Tools to work with 23andMe and AncestryDNA raw files
Ruby
10
star
6

rails_log_autotruncator

Autotruncate Rails test and development log files
Ruby
9
star
7

capistrano-crono

Crono integration for Capistrano
Ruby
8
star
8

mpi_client

MPI client library
Ruby
6
star
9

str2hex

Data formats convertion utility
C
4
star
10

ruby_micro_benchmarks

A set of Ruby benchmarks to test Ruby methods and operators performance
Ruby
2
star
11

gsocrat

Simply gtk dictonary and translator based on google translate.
Ruby
2
star
12

cancer_young_adults_research

The data science course project dedicated to cancer epidemiology research
Jupyter Notebook
2
star
13

plashchynski.github.io

My home pages
HTML
1
star
14

russian-grammar

Russian grammar cheat sheet
1
star
15

sorting-algorithms-python

Implementation of different sorting algorithms in Python
Jupyter Notebook
1
star
16

cm3065_uol_isp_final

Final assignment for the Intelligent Signal Processing (CM3065) module at University of London
Jupyter Notebook
1
star
17

telaio

A toolset to work with video files
Python
1
star
18

vigi

ViGi: Camera surveillance system with motion detection and object recognition for home security.
Python
1
star