Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart my heroku application automatically

This terminal command restarts my heroku application:

heroku restart 

Is there a way to run a script that will run this command and restart my application every hour?

like image 725
Oded Harth Avatar asked Jan 24 '12 11:01

Oded Harth


People also ask

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.

Does Heroku auto restart on crash?

It's also for the free plan, yes. Twice means it will restart once, and if it still fails then it will wait 10 minutes before restarting again.

Why does Heroku app restart?

On occasion, an app will crash or throw strange errors. A quick restart usually resolves the problem. I also usually reboot after running database migrations just to be safe. TIP By default heroku restart will restart all of your dynos, but you can specify a specific dyno to restart (e.g. heroku ps:restart web.


2 Answers

I actually just had to solve this problem for my apps and wrote a post on it with more details. Basically, you need the heroku-api gem now since the heroku gem is replaced by the CLI. Then you need a rake task, a couple of config variables and the heroku scheduler plugin (free except for minimal dyno time).

The rake task looks like this:

namespace :heroku do   desc 'restarts all the heroku dynos so we can control when they restart'   task :restart do     Heroku::API.       new(username: ENV['HEROKU_USERNAME'], password: ENV['HEROKU_PASSWORD']).       post_ps_restart(ENV['HEROKU_APP_NAME'])   end end 

You can also set it up to use your API token instead of putting your username and password into the config. This only matters if you don't want your co-contributors/coworkers knowing your password or the password to your main account on Heroku.

heroku config:set HEROKU_USERNAME=[username] HEROKU_PASSWORD=[password] HEROKU_APP_NAME=[app_name] -a [app_name] 

Now, go ahead and deploy and test:

git push [heroku_remote_name] [feature_branch]:master heroku run rake heroku:restart -a [app_name] 

Lastly, we’ll need to set up the task to run this on schedule. I’ve chosen to go with the free Heroku cron add-on:

heroku addons:add scheduler:standard -a [app_name] heroku addons:open scheduler -a [app_name] 

This will open up the scheduler UI in your browser and you can create a scheduled worker to run the rake task whenever you’d like. We only need it once per day and we’re choosing to run it before our first scheduled job of the day.

My original post with frigged up CSS (see update2 below):

https://web.archive.org/web/20150612091315/http://engineering.korrelate.com/2013/08/21/restart-heroku-dynos-on-your-terms/

UPDATE

I changed the name of the task from "implode" to "restart" to be way more clear as to what is happening. Implode is a fun name but pretty much useless otherwise.

UPDATE2

Apparently my company removed the blog post. I'm adding more of the code here and I've updated the link, but the CSS looks like a dog threw it up. My apologies.

like image 108
WattsInABox Avatar answered Sep 30 '22 13:09

WattsInABox


You could create a heroku cron job that uses the Heroku api on your application to restart itself...

One question though - why?

like image 21
John Beynon Avatar answered Sep 30 '22 11:09

John Beynon