The Heroku Scheduler is a simple and handy add-on for any Heroku application, to run recurring commands. I recently had the need to run a Heroku CLI command from within the Scheduler itself. Why would you need to do such a thing? In my case, at YNAB, we have an app pipeline which includes a staging app for our marketing site. We don’t want this staging app running all the time for various reasons, including not wanting others to accidently stuble upon it and not wanting search engines to crawl it. So, we wanted to put the app in maintenace mode on a scheduled basis. When we deploy an update to the staging app we take the app out of maintenance mode as part of the deployment process but then we want the Scheduler to automatically take it back into maintence mode.

There are other uses cases for this as well:

  1. Automatically promoting apps within a pipeline
  2. Database management such as forking, attaching to another app, etc.
  3. Management of any third-party addons
  4. Scaling up/down on a scheduled basis (scale up to 2 dynos in the morning; back down to 1 in the evening)

Here’s a how I got it to work.

  1. First, head to your Manage Account page within Heroku and grab your API Key. You’ll need to click the Reveal button first to see it and to be able to copy it. Heroku API Key
  2. Now, head over to app Settings page and click Config Vars. Heroku Config Vars
  3. Add a new var with the KEY of HEROKU_API_KEY and VALUE of the API Key you copied earlier. Click the Add button to save it. Heroku New Config Var
  4. On the app Resources page, click on Heroku Scheduler (add it as an Add-on first if you haven’t already done so) to manage its settings. Heroku Scheduler
  5. Click “Add New Job” and then enter this command:
curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz \
  | tar xz && ./heroku-client/bin/heroku [command] -a [app_name]

In my above described scenario where I want to put my app in mainteannce mode, my command looks like this:

curl -s https://s3.amazonaws.com/assets.heroku.com/heroku-client/heroku-client.tgz \
  | tar xz && ./heroku-client/bin/heroku maintenance:on -a stark-bakin-2764

Heroku Add Jost 6. Save the new job and you are done. It will now run on a schedule.