Help us understand the problem. What is going on with this article?

Whenever 複数環境で動作させるスケジュールを共存させる

@koshi_life です。

Whenever 便利で驚きました。
最近関わった案件で schedule.rb 内で複数環境でタスクを共存させたいシーンがあり、
情報がなかったので試した内容を備忘します。

(ちなみに、既存バッチが production で動作中の環境でログファイルと各種設定を分けたバッチを1本追加したいシーンでした。)

前提

  • Whenever v0.10.0
  • OS: macOS HighSierra

schedule.rb

set :environment, :HOGEHOGE を各タスクのスケジュール設定の手前で宣言するだけでした。

以下スケジュールを設定する例を示します。同じタスク Tasks::Hoge.execute を稼働させます。

  • 1つ目のスケジュール 毎時0分に production 環境
  • 2つ目のスケジュール 毎時30分に tmp 環境
config/schedule.rb
set :environment, :production
every :hour, at: 0 do
    runner "Tasks::Hoge.execute"
end

set :environment, :tmp
every :hour, at: 30 do
    runner "Tasks::Hoge.execute"
end

crontab 登録

$ bundle exec whenever --update-crontab
[write] crontab file updated

crontab 確認

$ crontab -l
# Begin Whenever generated tasks for: <RAILS_APP_PATH>/config/schedule.rb at: 2019-02-14 20:42:00 +0900
0 * * * * /bin/bash -l -c 'cd <RAILS_APP_PATH> && bundle exec bin/rails runner -e production '\''Tasks::Hoge.execute'\'''

30 * * * * /bin/bash -l -c 'cd <RAILS_APP_PATH> && bundle exec bin/rails runner -e tmp '\''Tasks::Hoge.execute'\'''

# End Whenever generated tasks for: <RAILS_APP_PATH>/config/schedule.rb at: 2019-02-14 20:42:00 +0900

参考

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away