Xronor

Build Status codecov Gem Version MIT License

Timezone-aware Job Scheduler DSL and Converter

job_template "/bin/bash -l -c ':job'"

job_type :rake, "bundle exec rake :task RAILS_ENV=production"

default do
  timezone "Asia/Tokyo" # UTC+9
end

every 1.hour, at: 15 do
  name "Send awesome mails"
  rake "send_awesome_mail"
end

every :day, at: '0:00 am' do
  name "Send greeting notifications"
  description "Send greeting notifications for all users"
  rake "send_greeting_notification"
end

every :day, at: '0:00 am', timezone: "Europe/Berlin" do # UTC+1
  name "Send notifications for Berlin"
  description "Send notifications for Berlin"
  rake "send_notification[Europe/Berlin]"
end

every :wednesday, at: '0:10 am' do
  name "Create new companies"
  rake "create_new_companies"
end

every "0 10 10,20 * *" do
  name "Healthcheck"
  rake "ping"
end

Table of contents

Why

Scheduled job execution system

As you know, Cron is commonly used for scheduled jobs. However, Cron has some difficulties:

  • Does not consider timezone. It depends on machine environment where cron daemon runs.
  • Does not contain job metadata (name, description, ...).
  • Cron daemon cannot be distributed. Machine where cron daemon runs can be SPOF.

Recently there are solutions for the last point, e.g. Azure Scheduler, CloudWatch Events and Kubernetes Cron Job, but those services still have fixed timezone to UTC.

Scheduler DSL

Whenever gem is very useful to describe scheduled jobs in human-friendly format. However, Whenever cannot treat timezone and metadata so that it is just a wrapper of Cron expression.

:point_right:

To resolve above problems, we need:

  • timezone-aware job scheduler DSL
    • Just like an enhance of Whenever
  • a DSL converter which is easy to register CloudWatch Events rule

Requirements

Ruby 2.2.2 or above

Installation

Add this line to your application's Gemfile:

gem 'xronor'

And then execute:

$ bundle

Or install it yourself as:

$ gem install xronor

Usage

Commands:
  xronor crontab SCHEDULEFILE
  xronor cwa SCHEDULEFILE --cluster=CLUSTER --container=CONTAINER --function=FUNCTION --table=TABLE --task-definition=TASK_DEFINITION
  xronor template SCHEDULEFILE --template=TEMPLATE
  xronor template_per_job SCHEDULERFILE --outdir=OUTDIR --template=TEMPLATE

Xronor CLI converts DSL file to:

  • xronor cwa CloudWatch Events Rule (requires AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION environment variables)
  • xronor crontab crontab file
  • file(s) from ERB template
    • xronor template write all jobs in one file
    • xronor template_per_job generate files per job

Xronor DSL

:point_right: docs/dsl

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/dtan4/xronor.

Author

Daisuke Fujita (@dtan4)

License

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