初期セットアップ
初期セットアップは以下を参考に
- Railsのバッチ処理のcrontab設定をwhenever&capistranoで自動化
- RailsでWheneverを使ってCrontabを仕込む
- アプリケーションに依存するバッチを"アプリケーションで自動生成"してcronに登録する方法
- whenever を使って Rails で cron 的な
- 本家サイト
やりたいこと
テスト
コンソール上にtest case passed
と表示するテストバッチを作成し、そのタスクをwheneverを使って自動化させてみる。一分ごとにtest case passed
と表示する。
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron
# Example:
#
#set :output, "/log/development.log"
job_type :rake, 'cd :path && RAILS_ENV=:environment /usr/bin/env rake :task'
set :environment, :development
set :output, { :error => 'log/error.log', :standard => 'log/cron.log' }
#every 1.day, at: '3:00 am' do
every 1.minute do
rake "scrape:get_scrape_target_list"
rake "item:create_items_from_scraped_items"
end
# every 3.minutes do
# rake "batches:test"
# end
# every 2.hours do
# command "/usr/bin/some_great_command"
# runner "MyModel.some_method"
# rake "some:great:rake:task"
# end
#
# every 4.days do
# runner "AnotherModel.prune_old_records"
# end
# Learn more: http://github.com/javan/whenever
# lib/tasks/batches.rake
namespace :batches do
desc "test"
task :test_whenever => :environment do
puts "test case passed"
end
end
バッチの動作確認が完了したところで実際にスケジューリングに入る。
bundle exec whenever
とwhenever --update-crontab
でタスクの確認と登録。しかしなぜかログが/private/var/mail
の方に来る。。一応ちゃんと実行はされているのだが。
そしてなぜか怒られれてもいるみたいだ。
エラー一覧
なぜか怒られる/bin/bash: shell_session_update: command not found
どうやらshell_session_update
を呼んでいるのにそんなものはないと怒られている?Why am I having / how can I fix this error: “shell_session_update: command not found”を参考に、これはrvm head
で最新版に更新したら解決。
ログが/log/cron_log
ではなく/private/var/mail
に送られてくる。
Output redirection aka logging your cron jobsによると、
you should put this global level set :output above your job definition, otherwise it wouldn't work
と書かれていたのでoutput:
一番上に持ってくる。のだがそれでも現状は変わらず。。解決策探し中。。。
まとめ
やる手順を簡単にまとめると、
1. wheneverセットアップ
2. テストバッチ作成
3. バッチ単体で動かしてみる
4. whenever使ってバッチを動かしてみる
5. 2を本番バッチに変えてみて動かしてみる
を繰り返してエラーが出る度に原因を突き止めていけば動くようになると思います。とりあえず今はまってる現状を脱会できる策を探してみます。