Skip to content

Instantly share code, notes, and snippets.

@SimonBrazell
Created June 21, 2018 01:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SimonBrazell/231b4f78370fea22af5974cf6b65bada to your computer and use it in GitHub Desktop.
Save SimonBrazell/231b4f78370fea22af5974cf6b65bada to your computer and use it in GitHub Desktop.
Schedule Heroku Dyno Restarts
# Credit goes to Jon Archer for this: http://www.jonarcher.com/2016/08/scheduled-heroku-dyno-restarts.html
#
# SETUP
# =====
#
# Create new rake task:
# $ rails generate task scheduler restart
#
# Install the Heroku oauth plugin:
# $ heroku plugins:install heroku-cli-oauth
#
# Create a new authorization token:
# $ heroku authorizations:create -d "Restart Scheduler API Token"
# Creating OAuth Authorization... done
# Client: <none>
# ID: 377160e8-<removed>
# Description: Restart Scheduler API Token
# Scope: global
# Token: 453c1322-<removed> <--- Authorization Token.
# Updated at: Thu Jun 21 2018 10:40:10 GMT+1000 (Australian Eastern Standard Time) (less than a minute ago)
#
# Use the new authorization token above in the below code:
require 'net/http'
require 'uri'
namespace :scheduler do
desc "Restart the Heroku production dynos"
task :restart do
uri = URI.parse("https://api.heroku.com/apps/<YOUR_DYNO_NAME>/dynos")
request = Net::HTTP::Delete.new(uri)
request.content_type = "application/json"
request["Accept"] = "application/vnd.heroku+json; version=3"
request["Authorization"] = "Bearer 453c1322-<removed>" # <--- Put your Authorization Token here.
req_options = {
use_ssl: uri.scheme == "https",
}
puts "Sending app dyno restart command..."
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts "Response Status: #{response.code}, Message: #{response.message}"
puts "Dyno restart command sent."
end
end
# Create a Heroku Scheduler task running - rails scheduler:restart, to perform this restart at a time you prefer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment