Sooey

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}"