Skip to content
Pedro Belo edited this page Jul 14, 2015 · 6 revisions

There are several alternatives out there allowing developers to schedule the execution of a task in their app. They can be as low level as setting up a crontab in EC2, or as high level as installing the Heroku Scheduler add-on to your app.

But sometimes you need more. Maybe you're not able to change your tasks the way you want, maybe you need more control and introspection around task execution, maybe you want to test the whole thing... When this time arrives, we suggest bringing the scheduling to your app with Clockwork.

Example

To illustrate, lets add a scheduled task to measure the background worker queue in a Pliny app.

Start adding Clockwork to the Gemfile:

gem 'clockwork'

Then define a task in lib/clock.rb:

require 'clockwork'
require_relative 'application'

module Clockwork
  every(10.seconds, 'monitor_queue') do
    queue_stats = MyBackgroundWorkerLibrary.stats
    Pliny.log(queue_stats)
  end
end

And add a clock entry to your Procfile:

clock: bundle exec clockwork lib/clock.rb

Now you can run your clock process locally like:

foreman start clock

Further reading: