8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HerokuのDynoの日次再起動の時刻を指定する

Last updated at Posted at 2019-01-30

Herokuのdynoは1日1回再起動する https://qiita.com/pilot/items/d42526c227ea322a2aa1 の続き

SchedulerアドオンでHeroku CLIを叩いて手動再起動する

  1. Heroku CLI用のBuild Packを追加
    1. heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-cli
  2. Heroku CLI用の長期間認証を作成
    1. heroku authorizations:create --description "for <アプリケーション名> HEROKU_API_KEY"
      ★--descriptionでメモを付けておくと後でheroku authorizationsで確認できる
      ★後述するが実運用ではいわゆるシステムアカウントで作成するのがおすすめ
    2. 結果の「Token」欄の値を使う
  3. 環境変数に設定
    1. heroku config:set HEROKU_API_KEY=<長期間認証Token>
      ★環境変数を設定すると起動中のアプリが再起動するので注意★
  4. アプリをデプロイ (git push)
    1. ビルドログに「Heroku CLI installation done」が表示される
  5. Schedulerアドオン導入
    1. heroku addons:create scheduler:standard
    2. 無料、Very Rareに動かない/重複実行あり https://devcenter.heroku.com/articles/scheduler
  6. スケジュール設定
    1. heroku addons:open scheduler
      1. ブラウザが起動してスケジュール設定画面が表示される
    2. 例えば「/app/.heroku/bin/heroku restart -a <アプリ名> web.1」「Standard 1X」「Daily」「20:00 UTC」を設定
      1. web.1とweb.2で時刻をずらす等
      2. 「20:00 UTC」は日本時間の午前5時

Schedulerはheroku run bashと同じくOne Off Dynoで実行されるので「長時間使用で無料枠を超える」等が起きないように留意のこと

再起動処理中もなるべくアプリを使えるようにするためにPrebootも設定しておく
heroku features:enable preboot
Prebootについて https://devcenter.heroku.com/articles/preboot
設定していないと「Dynoは立ち上がったがアプリの準備はまだできていない」状態でリクエストが振られ、エラーになることがある

長期間認証は「発行した個人アカウント」に紐づくため、その人が退職・チーム離脱・パスワード変更等を行うとキーが無効になるため、運用上は「人に紐づかない運用専用アカウント(いわゆるシステムアカウント)」での発行が必要
当該アカウントは当該アプリの再起動を行う権限(少なくともoperate権限)が必要なので、heroku access:add <対象アカウント(メアド)> --permissions=view,deploy,operate -a <アプリ名>で権限を付与しておく
当該アプリに対する権限リストはheroku access -a <アプリ名>で確認できる

以上

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?