Skip to content
Pedro Belo edited this page Aug 13, 2015 · 6 revisions

There are several alternatives out there allowing developers to schedule the execution of tasks.

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; either way, the basic idea is to have something outside your app triggering a custom rake task periodically.

If you need some kind of flexibility, accuracy or guarantee that your scheduling service cannot provide, there is always the option to handle scheduling within your app.

For Pliny we recommend in-app scheduling 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