Cron To Go
Last updated 01 December 2019
Table of Contents
Cron To Go is an add-on for running scheduled jobs on one-off dynos using cron expressions. Cron To Go combines the simplicity of using cron to schedule jobs with the scale and elasticity of the cloud. Instead of running always-on processes, use Cron To Go to start one-off dynos only when you need them. Another benefit of using Cron To Go over code based schedulers is that you don’t need to deploy new code when you have to make changes or additions to your scheduled jobs - simply add, edit or delete a job from the Cron To Go dashboard.
In addition to your Cron To Go plan’s price, you are billed for one-off dynos executed by the scheduler.
Cron To Go relies on Heroku APIs for job execution. If the APIs are unavailable, scheduled job execution might be missed. If scheduled jobs are critical to your application, it is recommended to run a custom clock process instead.
Provisioning the add-on
Cron To Go can be attached to a Heroku application via the CLI:
A list of all plans available can be found here.
$ heroku addons:create crontogo
-----> Adding crontogo to sharp-mountain-4005... done, v18 (free)
After you provision Cron To Go, you can use its dashboard to define and schedule jobs.
Free Trial
The free trial allows you to experiment with all features of the silver plan for 7 days. After 7 days, the jobs are disabled and you won’t be able to run ad-hoc jobs until you upgrade to a paid plan.
Dashboard
The Cron To Go dashboard allows you to:
- Create jobs
- Edit job definitions
- Delete jobs.
- Duplicate a job to create a new job based on an existing job.
- Pause an active job.
- Resume an inactive job.
- View job logs.
- View job history - see previous executions status and duration
- Import jobs from other scheduling systems.
You can access the dashboard via the CLI:
$ heroku addons:open crontogo
Opening crontogo for sharp-mountain-4005
or by visiting the Heroku Dashboard and selecting the application in question. Select Cron To Go from the Add-ons menu.
Importing jobs
Click the import button to smoothly migrate from other scheduling systems. With a single click you can import all jobs defined in your app’s Heroku Scheduler. This allows you to define your job schedule using cron expressions and manage all jobs in a single place.
Defining Jobs
A job in Cron To Go consists of the following:
- An optional nickname (for example,
Daily ETL
). - The job’s frequency, in cron expression format. The smallest interval you can specify is every minute.
- The command to execute.
- The size of one-off dyno to execute the command in. Read more about available one-off dyno types.
- One-off dyno timeout in seconds. Sets the time for the one-off dyno to expire, after which it will be killed. You can read more about it here.
When triggered, these jobs run in one-off dynos and show up in your logs as a dyno named crontogo_[job_id]
.
Cron expression examples
Expression | Description |
---|---|
*/5 * * * * |
every 5 minutes |
*/5 * * * 1-5 |
every 5 minutes on weekdays (Monday-Friday) |
0 1 * * 5 |
every Friday at 1am |
10 */1 * * * |
once an hour, at xx:10 |
You can use crontab.guru to generate other cron expressions.
API Access
The environment variables CRONTOGO_API_KEY
and CRONTOGO_ORGANIZATION_ID
are added to the app when Cron To Go add-on is provisioned and are used for API access. API access is currently in beta. Reach out to us at support@crazyantlabs.com if you’d like to join the beta.
Popular use cases
- Running periodic SQL statements on Heroku Postgres. You can refresh materialized views, run cleanup statements to get rid of stale or old data or perform BI processes such as in-database transformations. The following example makes use of psql command line with the DATABASE_URL environment variable to refresh a materialized view:
psql $DATABASE_URL -c "REFRESH MATERIALIZED VIEW vw_account_counters"
- Hitting HTTP endpoints. You can make HTTP requests to your web API or 3rd party API endpoints easily using Curl to start application processes. The following example uses curl to hit a 3rd party endpoint:
curl http://itsthisforthat.com/api.php?json
- Scheduling periodical batch jobs such as grabbing orders from Shopify, generating weekly reports or sending email notifications to application users by running your own code. The examples below show how to run Rails and Node apps using Cron To Go.
Using with Rails
Cron To Go can run any command that can be run in your application. In Rails, the convention is to set up rake
tasks. To create tasks in Rails, copy the code below to lib/tasks/scheduler.rake
and customize it to fit your needs:
desc "This task is called by the Cron To Go"
task :eat_breakfast => :environment do
puts "Eating breakfast..."
Kitchen.eat
puts "done."
end
task :excercise => :environment do
puts "Starting workout..."
Gym.workout
puts "done."
end
After writing a task and deploying it to Heroku, you can test it using heroku run
:
$ heroku run rake eat_breakfast
After testing on Heroku, you can add a job to Cron To Go that specifies
rake eat_breakfast
as the command to run.
Using with Node.js
Cron To Go can run any command that can be run in your application. For Node.js, you can simply add a file to your app called schedules.js
and execute it:
module.exports.eat_breakfast = function()
{
console.log('Eating breakfast...');
Kitchen.eat();
console.log('done.');
}
require('make-runnable');
After writing your task and deploying it to Heroku, test it using heroku run
:
$ heroku run node schedules.js eat_breakfast
After testing on Heroku, you can add a job to Cron To Go that specifies
node schedules.js eat_breakfast
as the command to run.
Monitoring and logging
One-off dynos that execute as part of Cron To Go output their logs with process crontogo_[job_id]
.
To retrieve the logs for the dynos started by Cron To Go run the following command:
$ heroku logs -t -a <app-name> -d crontogo
Use the dashboard to view the streamed dyno log from Heroku. For more production-ready persistence of logs, you can add one of the Heroku platform’s available logging add-ons to your app.
Long running jobs
You can control the maximum execution time for a job using the timeout setting for the job. You can make sure only one instance of a job executes by setting the timeout to a value lower than the frequency in which the jobs executes as set by the cron expression.
Migrating between plans
Application owners should carefully manage the migration timing to ensure proper application function during the migration process.
Use the heroku addons:upgrade
command to migrate to a new plan.
$ heroku addons:upgrade crontogo:starter
-----> Upgrading crontogo:newplan to sharp-mountain-4005... done, v18 ($49/mo)
Your plan has been updated to: crontogo:starter
Removing the add-on
You can remove Cron To Go via the CLI:
This will destroy all associated data and cannot be undone!
$ heroku addons:destroy crontogo
-----> Removing crontogo from sharp-mountain-4005... done, v20 (free)
Frequently Asked Questions
Q: In CTG, I can only select Hobby/Free Dynos, but when trying to run the job I get the following error: Requested type Hobby is not available. Please use Standard-1X, etc
.
A: CTG shows the dyno types available for your app. If you’re getting this message, you probably have to add a procfile to your app. After adding it, you will be able to select the appropriate dyno type.
Known issues
Cron To Go and Container Registry
If you are using Cron To Go and Container Registry as your deployment method, your process must be accessible from the web image. If your process requires a custom process type, please reach out to us at support@crazyantlabs.com to join the private beta.
Support
All Cron To Go support and runtime issues should be submitted via one of the Heroku Support channels. Any non-support related issues or product feedback is welcome at support@crazyantlabs.com.