Scheduling and automating background tasks is key to developing and managing a reliable and efficient product. And yet, sometimes it’s hard to get the ball rolling and actually figure out where and how to begin the process of, well, scheduling. So, Heroku users, rest assured - we’ve composed our own little tutorial covering the fundamentals of Heroku scheduling, just for you.
One of the basic terms you’ll have to get familiar with in Heroku is Dynos. Dynos are isolated, virtualized Linux containers designed to start quickly and execute code based on user-specified commands. You can think of them as mini servers, designed to run a single process. Each app consists of one or more components which make use of different dynos: web dynos allow access to HTTP/S ports and are in charge of responding to web requests, while worker dynos usually consume messages from a queue and process them. In addition to dynos that serve long running components of your app, Heroku also offers the option to run one-off dynos which run one-off processes that usually make use of your deployed application and its code. Since you only pay for dyno uptime, one-off dynos are useful for running scheduled tasks.
Heroku Scheduling 101
Cron To Go is a reliable cloud scheduler available on Heroku, based on the age-old job scheduler - cron. Cron To Go utilizes cron expressions in order to schedule and automate tasks (otherwise known as: cronjobs) while also boasting a sophisticated notification system based on webhook notifications and email. In this tutorial, we will go over how to install and operate all these facets of Cron To Go, so that you’ll be well versed in the art of scheduling jobs by the time you finish reading.
Installing Cron To Go
So let’s start at the beginning - The first step in using your Heroku scheduler is, no surprise here, installing it. You can choose between these two simple methods of installation:
- Use the following Heroku command line:
$ heroku addons:create crontogo -----> Adding crontogo to sharp-mountain-4005... done, v18 (free)
- Make use of the Heroku app dashboard:
Creating a job
Now that you’ve installed Cron To Go, open the Cron To Go dashboard through the Heroku dashboard or by entering this Heroku command line:
$ heroku addons:open crontogo Opening crontogo for sharp-mountain-4005
Click the conspicuous pink
Add job button located right in the middle of the page:
You’ll then be asked to input some crucial details regarding your Cronjob, such as: The time zone in which the job will take place, the exact command you want Cron To Go to execute, the Dyno size, the Dyno’s timeout, and the schedule of the job, which determines when your Cronjob will run.
The Cron schedule is expressed in a certain code known as a “Cron Expression”. You can read all about Cron Expressions in our Complete Guide to Cron Expressions.
How to manually run a job
Congratulations! Your job is up and running! But what if you feel like going off-schedule for a bit, and running the job, like, right now? In that case, you’ll have to:
- Open the `Actions` menu for your job and click `Run job now`.
- Confirm that you want to run the job manually.
- Just sit back and relax. Maybe stretch your arms a bit.
So you’re still not entirely relaxed. We get it, you might be wondering what’s going on with that job you just ran. Well, you can rest assured that Cron To Go will notify you of any unwelcome developments taking place under the hood. Here’s how to enable and customize your Cron Job notifications:
- Click `Notifications` in the top menu - the notifications dialog opens.
- Choose the trigger for notifications (only failures, or failures and successful executions). Note that Cron To Go will notify you on the first failure and first success after a failure per job.
- Choose the recipients. By default, all app members will receive a notification. In some cases, not everybody cares, so you can specify a list of recipients separated by commas.
How to view job history
Maybe something didn’t go according to plan, or maybe you just need to check in on your job, just in case. Either way, the history of your jobs is completely available to you. You can check it out at any time like so:
- Open the
Actionsmenu for your job and click
View job history.
- A dialog box opens which displays the last recorded job activity including the time it started, the duration of the job, and the job’s status. You can also expand each execution and see the return status, error message, and execution log of your job. Note that Cron To Go doesn’t capture the app logs, but rather shows what’s available on Heroku. Heroku only stores the last 1,500 lines of logs, so we recommend that you add a 3rd party logging add-on to capture more logs to allow you to debug and analyze any issues
The end is only the beginning
There you have it! You’re basically an expert scheduler. But now that you’ve mastered the fundamentals, how about broadening your scheduling horizons and checking out some of these creative uses for Cron? And if you have any questions regarding Cron To Go, you can always reach out to us via the chat button. Don’t be a stranger!