• Stars
    star
    659
  • Rank 68,396 (Top 2 %)
  • Language
    Ruby
  • Created over 14 years ago
  • Updated about 6 years ago

Reviews

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

Repository Details

A scheduler process to replace cron

This fork is no longer maintained, maybe try this one: https://github.com/Rykian/clockwork


Clockwork - 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.

Clockwork 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

Create clock.rb:

require 'clockwork'
include Clockwork

handler do |job|
  puts "Running #{job}"
end

every(10.seconds, 'frequent.job')
every(3.minutes, 'less.frequent.job')
every(1.hour, 'hourly.job')

every(1.day, 'midnight.job', :at => '00:00')

Run it with the clockwork binary:

$ clockwork clock.rb
Starting clock for 4 events: [ frequent.job less.frequent.job hourly.job midnight.job ]
Triggering frequent.job

Use with queueing

The clock process only makes sense as a place to schedule work to be done, not to do the work. It avoids locking by running as a single process, but this makes it impossible to parallelize. For doing the work, you should be using a job queueing system, such as Delayed Job, Beanstalk/Stalker, RabbitMQ/Minion, or Resque. This design allows a simple clock process with no locks, but also offers near infinite horizontal scalability.

For example, if you're using Beanstalk/Staker:

require 'stalker'

handler { |job| Stalker.enqueue(job) }

every(1.hour, 'feeds.refresh')
every(1.day, 'reminders.send', :at => '01:30')

Using a queueing system which doesn't require that your full application be loaded is preferable, because the clock process can keep a tiny memory footprint. If you're using DJ or Resque, however, you can go ahead and load your full application enviroment, and use per-event blocks to call DJ or Resque enqueue methods. For example, with DJ/Rails:

require 'config/boot'
require 'config/environment'

every(1.hour, 'feeds.refresh') { Feed.send_later(:refresh) }
every(1.day, 'reminders.send', :at => '01:30') { Reminder.send_later(:send_reminders) }

Anatomy of a clock file

clock.rb is standard Ruby. Since we include the Clockwork module (the clockwork binary does this automatically, or you can do it explicitly), this exposes a small DSL ("handler" and "every") to define the handler for events, and then the events themselves.

The handler typically looks like this:

handler { |job| enqueue_your_job(job) }

This block will be invoked every time an event is triggered, with the job name passed in. In most cases, you should be able to pass the job name directly through to your queueing system.

The second part of the file are the events, which roughly resembles a crontab:

every(5.minutes, 'thing.do')
every(1.hour, 'otherthing.do')

In the first line of this example, an event will be triggered once every five minutes, passing the job name 'thing.do' into the handler. The handler shown above would thus call enqueue_your_job('thing.do').

You can also pass a custom block to the handler, for job queueing systems that rely on classes rather than job names (i.e. DJ and Resque). In this case, you need not define a general event handler, and instead provide one with each event:

every(5.minutes, 'thing.do') { Thing.send_later(:do) }

If you provide a custom handler for the block, the job name is used only for logging.

You can also use blocks to do more complex checks:

every(1.day, 'check.leap.year') do
  Stalker.enqueue('leap.year.party') if Time.now.year % 4 == 0
end

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

Created by Adam Wiggins

Inspired by rufus-scheduler and http://github.com/bvandenbos/resque-scheduler

Design assistance from Peter van Hardenberg and Matthew Soldo

Patches contributed by Mark McGranaghan and Lukáš Konarovský

Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

http://github.com/adamwiggins/clockwork

More Repositories

1

rush

Ruby replacement for bash+ssh
Ruby
517
star
2

scanty

The blog that's almost nothing
Ruby
430
star
3

12factor

Official fork now at: https://github.com/heroku/12factor
Ruby
403
star
4

pony

The official fork is now maintained by benprew in http://github.com/benprew/pony
Ruby
233
star
5

yaml_db

Rails plugin for a database-independent dump format, data.yml. This fork no longer maintained, please see:
Ruby
219
star
6

stalker

A minimalist queueing DSL for Beanstalk.
Ruby
166
star
7

sumo

Launch ec2 instances, pronto
Ruby
163
star
8

rifgraf

Fire-and-forget data collection and graphing service
JavaScript
141
star
9

clockwork-sinatra-beanstalk

A sample app using Clockwork with a Sinatra/Beanstalk app to fetch market prices periodically
Ruby
46
star
10

lua-pong

Very simple example of a game engine written in C and game logic written in Lua.
C
38
star
11

qfeedreader

A web-based feed reader, comparing queueing with Delayed::Job, Minion, and Stalker
Ruby
38
star
12

gmail_smtp

rails plugin to send email via gmail smtp
Ruby
35
star
13

scanty-redis

The blog that's almost nothing - now on Redis
Ruby
31
star
14

bitswiki

A sweet wiki written in Rails.
Ruby
31
star
15

clockwork-rails-dj

A sample app using Clockwork with a Rails/DJ app to fetch market prices periodically
Ruby
28
star
16

heroku-client

This fork is not maintained, please see: http://github.com/heroku/heroku
Ruby
25
star
17

cachemanifest

simple example of an html5 cache manifest, deployable to heroku
Ruby
22
star
18

nginx-as-heroku-app

Runing Nginx as a Heroku app with the C buildpack
C
15
star
19

example-nested-resources

An example of nested resources in Rails 2
14
star
20

peruse

Ruby
11
star
21

cocos2d

Python 2D game library, unofficial import from svn
Python
11
star
22

wordpress-cleardb

Wordpress on Heroku, using the ClearDB MySQL add-on free plan.
PHP
10
star
23

postgres-app

C
9
star
24

chef-cookbooks

Ruby
8
star
25

sigterm

Demonstration of graceful shutdown on SIGTERM
Ruby
7
star
26

erowid-mobile

Mobile site prototype for erowid.org
Ruby
7
star
27

sampleapp-sms

JavaScript
6
star
28

s5

S3 slide show
Ruby
6
star
29

merb-on-heroku

Blank Merb app with tweaks for Heroku deployment
Ruby
5
star
30

rack-maintenance

Ruby
5
star
31

ramaze-on-heroku

Ruby
4
star
32

qfeedreader-minion

Ruby
3
star
33

whatswrong

Ruby
3
star
34

redx-dashboard

Example dashboard which consumes event stream from redx-backend
JavaScript
3
star
35

queuedist

Ruby
3
star
36

blendproof

Python
3
star
37

heroku_suspenders

thoughtbot's Suspenders modified for Heroku.
Ruby
3
star
38

redx-backend

Example of Redis backend which forwards events vis https
JavaScript
3
star
39

heroku-branch-app

Heroku client plugin that picks app to use based on branch == remote
Ruby
2
star
40

tinkerdb

Ruby
2
star
41

wedgeguard

Rack middleware for request timeouts
Ruby
2
star
42

hello-node

JavaScript
2
star
43

trydjango

Python
2
star
44

heroku-releases

Ruby
2
star
45

hello-world

JavaScript
1
star
46

cacheit

Ruby
1
star
47

app-with-bundler-0.8-bundled

Ruby
1
star
48

ey-cloud-recipes

A starter repo for custom chef recipes on EY's cloud platform
Ruby
1
star
49

rails31-asset-sync

Ruby
1
star
50

lang-test-case

1
star
51

lumberjack

Ruby
1
star
52

assetbench

Ruby
1
star
53

logarchive

Ruby
1
star
54

psmanage

Ruby
1
star
55

churner

Ruby
1
star
56

nav-ruby19

Ruby
1
star
57

memcache-auth-test

Ruby
1
star
58

sampletest

1
star
59

heroku-devclouds

Ruby
1
star