Using AMQP to do cron-like scheduling

This page is a mirrored copy of an article originally posted on the LShift blog; see the archive index here.

Barry Pederson’s excellent py-amqplib Python AMQP client comes with a very cute little demo, demonstrating how easy it is to do sophisticated cron-like things with AMQP and RabbitMQ.

As Barry writes in the example, the trick is to “[fire] off simple messages at one-minute intervals to a topic exchange named ‘clock’, with the topic of the message being the local time as ‘year.month.date.dow.hour.minute‘, for example: ‘2007.′, where the dow (day of week) is 0 for Sunday, 1 for Monday, and so on (similar to Unix crontab). A consumer could then bind a queue to the routing key ‘#.0′ for example to get a message at the beginning of each hour.”



On 8 February, 2008 at 1:07 pm, Ben Hood wrote:


You could even build an extension to the rabbit server to produce the timer event that would be published to the exchange - so that the cron daemon is self contained within the rabbit server.