Skip to content

Repository files navigation

CronoTrigger

Gem VersionBuild Statuscodecov

Asynchronous Job Scheduler for Rails.

The purpose of this gem is to integrate job schedule into Service Domain.

Because of it, this gem uses ActiveRecord model as definition of job schedule.

Installation

Add this line to your application's Gemfile:

gem'crono_trigger'

And then execute:

$ bundle

Or install it yourself as:

$ gem install crono_trigger

Usage

Execute crono_trigger:model generator.

$ rails g crono_trigger:model mail_notification
create db/migrate/20170619064928_create_mail_notifications.rb
create app/models/mail_notification.rb
# ...

Migration sample

classCreateMailNotifications < ActiveRecord::Migrationdefchangecreate_table:mail_notificationsdo |t|
# columns for CronoTrigger::Schedulablet.string:cront.datetime:next_execute_att.datetime:last_executed_att.integer:execute_lock,limit: 8,default: 0,null: falset.datetime:started_at,null: falset.datetime:finished_att.string:last_error_namet.string:last_error_reasont.datetime:last_error_timet.integer:retry_count,default: 0,null: falset.timestampsendadd_index:mail_notifications,[:next_execute_at,:execute_lock,:started_at,:finished_at],name: "crono_trigger_index_on_mail_notifications"endend

Implement #execute method

classMailNotification < ActiveRecord::BaseincludeCronoTrigger::Schedulableself.crono_trigger_options={retry_limit: 5,retry_interval: 10,exponential_backoff: true,execute_lock_timeout: 300,}# `execute` callback is defined# can use `before_execute`, `after_execute`, `around_execute`# If execute method raise Exception, worker retry task until reach `retry_limit`# If `retry_count` reaches `retry_limit`, task schedule is reset.# # If record has cron value, reset process set next execution time by cron definition# If record has no cron value, reset process clear next execution timedefexecutesend_mailthrow:retry# break execution and retry taskthrow:abort# break execution and raise AbortExecution. AbortExecution is not retriedthrow:ok# break execution and handle task as successthrow:ok_without_reset# break execution and handle task as success but without schedule reseting and unlockingendend# one time scheduleMailNotification.create(next_execute_at: Time.current.since(5.minutes))# cron scheduleMailNotification.create(cron: "0 12 * * *").activate_schedule!# orMailNotification.new(cron: "0 12 * * *").activate_schedule!.save# if update cron column or timezone column# update next_execute_at automatically by before_update callbackmail=MailNotification.create(cron: "0 12 * * *").activate_schedule!mail.next_execute_at# => next 12:00 with Time.zonemail.update(cron: "0 13 * * *")mail.next_execute_at# => next 13:00 with Time.zonemail.update(timezone: "Asia/Tokyo")mail.next_execute_at# => next 13:00 with Asia/Japan

Run Worker

$ crono_trigger MailNotification
$ crono_trigger --help
Usage: crono_trigger [options] MODEL [MODEL..]
-f, --config-file=CONFIG Config file (ex. ./crono_trigger.rb)
-e, --envornment=ENV Set environment name (ex. development, production)
-p, --polling-thread=SIZE Polling thread size (Default: 1)
-i, --polling-interval=SECOND Polling interval seconds (Default: 5)
-c, --concurrency=SIZE Execute thread size (Default: 25)
-l, --log=LOGFILE Set log output destination (Default: STDOUT or ./crono_trigger.log if daemonize is true)
--log-level=LEVEL Set log level (Default: info)
-d, --daemonize Daemon mode
--pid=PIDFILE Set pid file
-h, --help Prints this help

Specification

Columns

nametyperequiredrenamedescription
cronstringnonoRecurring schedule formatted by cron style
next_execute_atdatetimeyesyesTimestamp of next execution. Worker executes task if this column <= now
last_executed_atdatetimenoyesTimestamp of last execution
timezonedatetimenoyesTimezone name (Parsed by tzinfo)
execute_lockintegeryesyesTimestamp of fetching record in order to hide record from other transaction during execute lock timeout.
when execution complete this column is reset to 0
started_atdatetimenoyesTimestamp of schedule activated
finished_atdatetimenoyesTimestamp of schedule deactivated
last_error_namestringnonoClass name of last error
last_error_reasonstringnonoError message of last error
last_error_timedatetimenonoTimestamp of last error occured
retry_countintegernonoRetry count.
If execution succeed retry_count is reset to 0

You can rename some columns. ex. crono_trigger_options[:next_execute_at_column_name] = "next_time"

Rollbar integration

This gem has rollbar plugin. If crono_trigger/rollbar is required, Add Rollbar logging process to CronoTrigger.config.error_handlers

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/joker1007/crono_trigger.

License

The gem is available as open source under the terms of the MIT License.

About

Simple Asynchronous Job Scheduler for Rails.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages