5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Herokuでrakeファイルを定期実行する

Posted at

rakeファイルを自動実行

Heroku Schedulerを使用して、コマンドを自動実行する

Heroku Scheduler追加方法

Heroku SchedulerはResources->Add-onsから追加可能
Standard - Freeプランを選択

コマンドで追加する場合はこちら

$ heroku addons:create scheduler:standard
スクリーンショット 2017-07-12 8.35.52.png

キャプチャ.PNG

Heroku Schedulerで実行したいコマンドを指定する。

下のキャプチャでは、上の$ rake destroy_past_post:destroyを1日一回
下の$ rake post:updateを10分間隔で実行しています。

実行間隔は、1日毎、1時間毎、10分毎と痒いところに手が届かない仕様なので、そこはrakeファイル内で制御しましょう。
なお実行時間はUTC時間なので、気をつけましょう。

UTC->JSTに変換
https://utctojst.appspot.com/utctojst

キャプチャ2.PNG

ログの確認

$ heroku logs --ps scheduler.xxxx 
#(xxxxは実行時に割り当てられたプロセス番号)

プロセス番号はlogs -tで探す

$ heroku logs -t
2017-07-12T15:40:20.722747+00:00 app[api]: Starting process with command `bundle exec rake post:update` by user scheduler@addons.heroku.com
2017-07-12T15:40:26.524329+00:00 heroku[scheduler.8540]: Starting process with command `bundle exec rake post:update`
2017-07-12T15:40:27.097075+00:00 heroku[scheduler.8540]: State changed from starting to up
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?