Posted about 4 years ago by Mark Pundsack
Today we're happy to announce the availability of Heroku Scheduler. Scheduler is an add-on for running administrative or maintenance tasks, or jobs, at scheduled time intervals. It's the polyglot replacement of the Cron add-on, with more power and flexibility. And it's free; you just pay for the dyno time consumed by the one-off tasks.
A dashboard allows you to configure jobs to run every 10 minutes, every hour, or every day, and unlike the Cron add-on, you can control when. E.g. Every hour on the half-hour, or every day at 7:00am.
Tasks are any command that can be run in your application or even the Unix shell.
For Rails, the convention is to set up
rake tasks. To create your scheduled tasks in Rails, copy the code below into
lib/tasks/scheduler.rake and customize it to fit your needs.
desc "This task is called by the Heroku scheduler add-on" task :update_feed => :environment do puts "Updating feed..." NewsFeed.update puts "done." end task :send_reminders => :environment do User.send_reminders end
If you're using Python with the popular Fabric automation tool, you can define a
fab clean_sessions task:
from fabric.api import task @task def clean_sessions(): url = urlparse(os.environ.get('REDISTOGO_URL')) db = redis.Redis(host=url.hostname, port=url.port, password=url.password) db.delete('myapp:sessions') print 'done.'
For apps built on other frameworks or languages, another convention is to add a script to bin/ that will perform the task. E.g.
To schedule a frequency and time for a job, open the scheduler dashboard by finding the app in My Apps, clicking "General Info", then selecting "Scheduler" from the Add-ons dropdown.
On the Scheduler Dashboard, click "Add Job...", enter a task, select a frequency and next run time.
For example, add
rake update_feed, select "Hourly" and ":30" to update feeds every hour on the half-hour. Then add
rake send_reminders, select "Daily" and "00:00" to send reminders every day at midnight.
Existing Cron add-on users should migrate to Heroku Scheduler as soon as possible. It has more functionality, is easier to use, and is free. Cron is restricted to running a single command,
rake cron and does not provide control over when daily and hourly tasks are run. Scheduler can do everything the Cron add-on does, and more.
If you want your new jobs to be scheduled as close as possible to when your Cron jobs would run, go to the Cron dashboard and look at the "Scheduled for" information. Then in the Scheduler dashboard, create a new task, set it to be either hourly or daily, and then set the Next Run field to the selection closest to the previous scheduled time. Set the task to