LoginSignup
9
13

More than 5 years have passed since last update.

Wheneverを使って簡単にバッチ処理をする。

Last updated at Posted at 2014-10-06

Wheneverとは、crontab管理ライブラリです!
Wheneverを使用する事で、Railsでの定期的に実行する処理の管理や設定を非常に楽に行うことができました。

まず、gemをインストールします。
gem 'whenever', :require => false

次に、wheneverize.を実行します。
すると、config以下にschedule.rbが作成されます。

schedule.rbには、定期的に実行したい処理の設定を書きます。

schedule.rb
set :output, 'log/crontab.log'#出力先
set :environment, :production

every :day, :at => '3:00 pm' do #日時設定
  rake "shuzo:meigen" #rakeタスクを実行
end

ここでは、毎日15時に松岡修造の名言が、log/crontab.logに出力されるという処理を書きました。

wheneverでは、rakeタスク以外にも、

command: bashコマンド実行
runner: Rails内のメソッド実行
script: scriptの実行

もスケジューリングできます。

9
13
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
9
13