LoginSignup
0
0

More than 1 year has passed since last update.

Heroku Scheduler から Heroku Data for Redisにコマンドを送信するには

Last updated at Posted at 2022-12-21

経過的な措置ではあるのですが、本番環境だとうまくRedisのkeyが消えてくれずに、メモリリミットに到達して事故るということが起きてまして、ジョブ自体は流れてるのでflushしてしまえばよいのですが、その方法がわかりませんでしたということで備忘録です。

手順

  • Heroku CLI Buildpackをアプリケーションに追加 (参考)
  • アプリケーションに HEROKU_API_KEY 環境変数を設定
  • Heroku Schedulerで echo "flushall" > /tmp/cmd.txt; heroku redis:cli -a slack-all-timeline < /tmp/cmd.txt のような形でコマンドを設定

Heroku CLI Buildpackをアプリケーションに追加

$ heroku config:set HEROKU_API_KEY=`heroku auth:token` -a アプリケーション名

$ heroku buildpacks:add heroku-community/cli -a アプリケーション名

上述のコマンドを打ってください。するとこんな感じでレスポンスが返ってきます。

 ›   Warning: token will expire 12/31/2022
 ›   Use heroku authorizations:create to generate a long-term token
Setting HEROKU_API_KEY and restarting ⬢ アプリケーション名... done, v524
 ▸    Warning: The "HEROKU_" namespace is protected and shouldn't be used.
HEROKU_API_KEY: xxxxxxx-37c9-456b-83b9-xxxxxxx

ここで出力されるHEROKU_API_KEYはexpire dateが決まってるのでここでは使いません。

HEROKU_API_KEYを出力する

~ $ heroku authorizations:create
Creating OAuth Authorization... done
Client:      <none>
ID:          xxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxx
Description: Long-lived user authorization
Scope:       global
Token:       xxxxxx-xxxx-xxxx-xxxxx-xxxxxxxxx
Updated at:  Wed Jan 25 2023 04:05:02 GMT+0000 (Coordinated Universal Time) (less than a minute ago)

Token部分をコピーして、Herokuアプリの管理画面のSettingsから環境変数HEROKU_API_KEYに入れてください

このあとにアプリケーションを再ビルドする必要があります。
$ git push heroku master と書いてありますが、手順については自分たちのビルド方法に従ってください。
弊社環境ではGithubからデプロイしていますので、デプロイしなおしました。

Heroku Schedulerの設定

Heroku Schedulerで echo "flushall" > /tmp/cmd.txt; heroku redis:cli -a アプリ名 < /tmp/cmd.txt のような形でコマンドを設定

REDISを複数追加してある場合は
echo "flushall" > /tmp/cmd.txt; heroku redis:cli -a アプリ名 HEROKU_REDIS_XXXX < /tmp/cmd.txt のようにする必要があります

これで実行されます。

あとはここに書いたとおりですが

ローカルからRedisにログインして keys * とか info keyspace とか打って、データ量を確認してみましょう。

0
0
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
0
0