Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule Heroku to restart dynos every 10 or so minutes

I am developing a REST api with Node.js on Heroku, and one the drivers is giving me problems (I reported that to the driver creator already), but basically a restart of the dynos every half an hour or so seems to fix this. I was hoping y'all could help me with writing a script for scheduler or something similar to restart the dynos automatically every 10 or so minutes as a temporary fix.

p.s. I checked out the scheduler documentation, but it didn't make much sense

Thanks!

like image 443
rickyjuo Avatar asked May 11 '17 22:05

rickyjuo


People also ask

How does Heroku automatically restart dynos?

Dyno cycling happens automatically and transparently by the Dyno Manager, and is logged. In addition to scheduled cycling, dynos are automatically restarted with every new code release, add-on change, or config vars change. They can also be restarted manually.

How often does Heroku restart dynos?

The first time a dyno crashes, it will be restarted immediately. If the dyno crashes again, it will be subject to a cool-off period before a restart is attempted. The first cool-off period is up to 20 minutes, the next one is up to 40 minutes, then up to 60 minutes, up to 180 minutes and finally up to 320 minutes.

How do I automatically restart Heroku app?

As far as I can tell, simply running heroku ps:restart --app APPNAME in the Heroku Scheduler add-on works fine.

How do I schedule a dyno in Heroku?

Click on the Heroku Scheduler Add-on from the Resources tab on the Dashboard or run the command heroku addons:open scheduler from the CLI. Click edit on your scheduled job (usually indicated with a pencil). Under Run Command, look for a dropdown that allows you to select the dyno plan. Save Job.


2 Answers

The steps provided by @rdegges require additional authentication token in the request. Adding more details:

  1. Create Auth Token using Heroku CLI by running this command as mentioned on Heroku platform API

    heroku authorizations:create

  2. Install the Heroku Scheduler add-on into your Heroku application.

  3. Open scheduler and go to add job. Enter the following command in the job editor. This command performs restart the dyno.

`

curl -n -X DELETE https://api.heroku.com/apps/APP_NAME/dynos \n -H "Content-Type: application/json" \n -H "Accept: application/vnd.heroku+json; version=3" \n -H "Authorization: Bearer TokenCreatedInStep1"

Verify in the Heroku app logs, if the scheduler job is running on the set time.

like image 63
Keyul Avatar answered Oct 13 '22 09:10

Keyul


You can do what you're asking by doing the following:

  • Create a script in your project that makes a Heroku Platform API request to restart your dynos. The API call documentation can be found here: https://devcenter.heroku.com/articles/platform-api-reference#dyno-restart-all

  • Provision the Heroku Scheduler addon.

  • Go into Heroku Scheduler's web UI, and tell it to run your restart script every hour (or whatever time period works for you).

This is the best 'pragmatic' way to accomplish this.

like image 27
rdegges Avatar answered Oct 13 '22 09:10

rdegges