Restart my heroku application automatically

后端 未结 7 1538
北海茫月
北海茫月 2020-12-13 08:01

This terminal command restarts my heroku application:

heroku restart

Is there a way to run a script that will run this command and restart

相关标签:
7条回答
  • 2020-12-13 08:35

    I solved this with a very simple curl command script within the repo that is triggered using the free Heroku scheduler.

    #!/bin/sh curl -X DELETE "https://api.heroku.com/apps/${HEROKU_APP_NAME}/dynos" \ --user "${HEROKU_CLI_USER}:${HEROKU_CLI_TOKEN}" \ -H "Content-Type: application/json" \ -H "Accept: application/vnd.heroku+json; version=3"

    See https://gist.github.com/mattheworiordan/f052b7693aacd025f025537418fa5708.

    0 讨论(0)
  • 2020-12-13 08:36

    A script isn't necessary, just "crash" your application and Heroku will restart it.

    Just don't do this more frequently than once every ten minutes or Heroku will subject you to a 10 minute timeout.

    In node.js you do this with process.exit(0).

    From Chris at Heroku Support:

    A crashed dyno will be restarted immediately. If the dyno moves from a crashed state into an "up" state (meaning that the dyno bound to $PORT) then it's subject to being a normal running dyno. If it crashes again during the boot or "starting" sequence then it won't be restarted again until after the TIMEOUT period. The TIMEOUT period is currently 10 minutes but that is subject to change. This prevents dynos that are continually crashing from putting extraneous load on the platform.

    However, as good as that sounds, it doesn't work in practice. You will hit the timeout every time you exit because the dyno manager expects your app to be up:

    For your worker process management you're exiting the process cleanly but the platform is expecting the dyno to be up. It sounds like you're essentially crashing the dyno as a result.

    So again, if you need to restart periodically -- and that period can be set to > 10 minutes -- this is a easy and simple way to manage memory clearing. If you need to reboot dynamically (for example, when idle is detected) or frequently you will need to explore other options.

    You can access the name of the dyno (ex. "worker.3", "web.1") through the environment variable "PS" and issue a heroku API restart command programmatically.

    0 讨论(0)
  • 2020-12-13 08:40

    We solved this by using a buildpack to get the heroku command available to the dyno itself, then using Heroku Scheduler.

    We added the https://github.com/gregburek/heroku-buildpack-toolbelt buildpack per its instructions:

    heroku buildpacks:add https://github.com/gregburek/heroku-buildpack-toolbelt.git
    heroku config:add HEROKU_TOOLBELT_API_EMAIL=`heroku whoami`
    heroku config:add HEROKU_TOOLBELT_API_PASSWORD=`heroku auth:token`
    

    Then made sure the app slug was rebuilt per the instructions:

    git push heroku master
    

    In Heroku Scheduler, we added this as a hourly job:

    vendor/heroku-toolbelt/bin/heroku ps:restart -a $HEROKU_APP_NAME
    

    You can determine if it's working by looking for Scheduler output in the Heroku logs, and of course by the memory graph of the app in the Heroku dashboard (if you're restarting to work around a memory leak).

    0 讨论(0)
  • 2020-12-13 08:47

    Inspired by https://www.stormconsultancy.co.uk/blog/development/ruby-on-rails/automatically-restart-struggling-heroku-dynos-using-logentries/

    # Setup
    heroku plugins:install https://github.com/heroku/heroku-oauth
    heroku authorizations:create -s write
    heroku config:add RESTART_API_KEY=<API KEY>
    heroku config:add APP_NAME=<App Name>
    
    heroku addons:add scheduler:standard -a <App Name>
    heroku addons:open scheduler -a <App Name>
    add `rake restart`
    
    # Gemfile
    gem 'platform-api', require: false
    
    # Rakefile
    task :restart do
      require 'platform-api'
      app_name = ENV.fetch('APP_NAME')
      key = ENV.fetch('RESTART_API_KEY')
      connection = PlatformAPI.connect_oauth(key)
      connection.dyno.list(app_name).map do |info|
        if info['type'] == 'web' && info['state'] == 'up'
          puts "Restarting #{info.inspect}"
          connection.dyno.restart(app_name, info['name'])
        else
          puts "Skipping #{info.inspect}"
        end
      end
    end
    
    0 讨论(0)
  • 2020-12-13 08:50

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

    One question though - why?

    0 讨论(0)
  • 2020-12-13 08:53

    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.

    0 讨论(0)
提交回复
热议问题