初めてのRailsアプリ(その6)
railsubject_06
初めてのRailsアプリ(その6)
バッチ作成のお作法教本ってないのかな。すごく大事だと思うけど。
7. Batch on Rails
Railsでのバッチ処理を勉強する。
動作確認
動作確認を行う。cron起動でActiveRecordを介してデータを取得できることを確認する。
バッチファイル作成
lib配下に作成する。今回の例では、./lib/batches/subject_batch
なお、ソース中のCustomLoggerはRailsのLoggerの自前ラップ。→CustomLogger
class Batches::SubjectsBatch
def self.hello
print "hello batch\n"
end
def self.show
subjects = Subject.all
subjects.each { |subject|
# Aplicationログへの出力
CustomLogger.info("batch", "MSG_INF_001", subject.name_content)
# Cron ログへの出力
p CustomLogger.format("batch", "MSG_INF_001", subject.name_content)
}
end
end
rails runner
runnerによる動作確認
mba:railsubject 7010oncajon$ rails runner Batches::SubjectsBatch.show
"(batch) (subjects_batch.rb:10:in `each') 【案件1】rails案件 "
"(batch) (subjects_batch.rb:10:in `each') 【案件2】Java案件 "
whenever
crontabの設定をしてくれるgem(whenever)が良さげなのでで導入。
# crontab管理
gem 'whenever', :require => false
schedule.rbを作成する
mba:railsubject 7010oncajon$ wheneverize .
[add] writing `./config/schedule.rb'
[done] wheneverized!
./config/schedule.rb
# 出力先のログファイルの指定
set :output, 'log/crontab.log'
# ジョブの実行環境の指定
set :environment, "development"
# 2分毎にSubjectsBatch.showを実行する
every 2.minutes do
runner "Batches::SubjectsBatch.show"
end
スケジュールの適用
mba:railsubject 7010oncajon$ wheneverize .
[add] writing `./config/schedule.rb'
[done] wheneverized!
mba:railsubject 7010oncajon$ bundle exec whenever
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /Users/7010oncajon/Documents/workspace/railsubject && script/rails runner -e production '\''Batches::SubjectsBatch.show'\'' >> log/crontab.log 2>&1'
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
mba:railsubject 7010oncajon$ bundle exec whenever --update-crontab
[write] crontab file updated
mba:railsubject 7010oncajon$ crontab -l
# Begin Whenever generated tasks for: /Users/7010oncajon/Documents/workspace/railsubject/config/schedule.rb
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd /Users/7010oncajon/Documents/workspace/railsubject && script/rails runner -e production '\''Batches::SubjectsBatch.show'\'' >> log/crontab.log 2>&1'
# End Whenever generated tasks for: /Users/7010oncajon/Documents/workspace/railsubject/config/schedule.rb
なおスケジュールを削除したい場合は以下。
mba:railsubject 7010oncajon$ bundle exec whenever --clear-crontab
[write] crontab file
mba:railsubject 7010oncajon$ crontab -l
mba:railsubject 7010oncajon$
動作確認
ログ(.log/development.log)をみる。動いている、やったね。
I, [2013-07-27T23:24:03.297666 #61647] INFO -- : Connecting to database specified by database.yml
2013/07/27 23:24:03.776319 #61647 DEBUG Subject Load (0.7ms) SELECT "subjects".* FROM "subjects"
2013/07/27 23:24:03.786608 #61647 INFO (batch) (subjects_batch.rb:11:in `show') 【案件1】rails案件
2013/07/27 23:24:03.786918 #61647 INFO (batch) (subjects_batch.rb:11:in `show') 【案件2】Java案件
I, [2013-07-27T23:26:03.207108 #61659] INFO -- : Connecting to database specified by database.yml
2013/07/27 23:26:03.662893 #61659 DEBUG Subject Load (0.6ms) SELECT "subjects".* FROM "subjects"
2013/07/27 23:26:03.674371 #61659 INFO (batch) (subjects_batch.rb:11:in `show') 【案件1】rails案件
2013/07/27 23:26:03.674651 #61659 INFO (batch) (subjects_batch.rb:11:in `show') 【案件2】Java案件
終わりに
疑問点
- 今回はwebのlogファイルに出力させた。別ファイルにしたいがやり方がわからん。wheneberで設定したログ(log/crontab.log)に出力させることが正解なのか?
- JRubyの場合に、war化してデプロイしたときに、cronから呼び出せるのか。
- Connecting to database specified by database.ymlっていうログがうざいんですけどー
この話題、続きます。(・ω<)