2013-07-15 00:56:51 +0900
HerokuのAPIをcurlで叩いてプロセスをスケールさせるメモ。
Heroku上のアプリのworkerプロセスをスケールさせる処理を、Heroku Schedulerを使って定時実行したかったので、curlでAPIにアクセスする方法について調べました。
まず、Getting Started with the Platform APIを参考にAPIキーを取得します。
$ API_KEY=`(echo -n ":" ; heroku auth:token) | base64` ; echo $API_KEY
OjAxMjM0NTY3LTg5YWItY2RlZi0wMTIzLTQ1Njc4OWFiY2RlZgo=
あとは、Platform API Referenceを見てcurlのコマンドを組み立ててAPIキーをAuthorization
ヘッダとして付与すればOK。
プロセスに対する処理はDynoではなくFormation APIというものを使います。example-app
アプリのworker
プロセスを0から1に増やす場合は以下のようなコマンドになります。
$ curl -n -X PATCH https://api.heroku.com/apps/example-app/formation/worker \
-H "Authorization: $API_KEY" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Content-Type: application/json" \
-d "{\"quantity\":1}"
逆にworker
プロセスを1から0に減らす場合は以下のようになります。
$ curl -n -X PATCH https://api.heroku.com/apps/example-app/formation/worker \
-H "Authorization: $API_KEY" \
-H "Accept: application/vnd.heroku+json; version=3" \
-H "Content-Type: application/json" \
-d "{\"quantity\":0}"