Clockworkd - a clock process to replace cron

Cron is non-ideal for running scheduled application tasks, especially in an app deployed to multiple machines. [More details.](adam.heroku.com/past/2010/4/13/rethinking_cron/)

Clockworkd is a cron replacement. It runs as a lightweight, long-running Ruby process which sits alongside your web processes (Mongrel/Thin) and your worker processes (DJ/Resque/Minion/Stalker) to schedule recurring work at particular times or dates. For example, refreshing feeds on an hourly basis, or send reminder emails on a nightly basis, or generating invoices once a month on the 1st.

Quickstart

Configure application:

In your Gemfile gem "clockworkd", ">= 0.2.5"
Run generator to generate config yml and script file
Use config/initiaizers to configure options for this gem

Modify clockworkd.yml:

# Run job every two minute: */2 * * * *
clear_sesssion_job:
  cron: "59 1 * * *"
  block: Delayed::Job.enqueue ClearSessionsJob.new
  description: "This job clear sessions table"

Run it as console or daemon application (rails 3.x):

$ ruby script/clockworkd run
$ ruby script/clockworkd --identifier=0 start

Use clockworkd with capistrano:

after "deploy:stop",    "clockworkd:stop"
after "deploy:start",   "clockworkd:start"
after "deploy:restart", "clockworkd:restart"

Use monit script to monitor it:

check process clockworkd
  with pidfile /var/www/apps/{app_name}/shared/pids/clockworkd.pid
  start program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/clockworkd start"
  stop program = "/usr/bin/env RAILS_ENV=production /var/www/apps/{app_name}/current/script/clockworkd stop"

In production

Only one clock process should ever be running across your whole application deployment. For example, if your app is running on three VPS machines (two app servers and one database), your app machines might have the following process topography:

  • App server 1: 3 web (thin start), 3 workers (rake jobs:work), 1 clock (clockwork clock.rb)

  • App server 2: 3 web (thin start), 3 workers (rake jobs:work)

You should use Monit, God, Upstart, or Inittab to keep your clock process running the same way you keep your web and workers running.

Meta

Inspired by

Installation

  • Type ‘gem install –local clockworkd’ with root account if you have installed RubyGems.

Example

Example goes here.

Copyright © 2011 arufanov, released under the MIT license.