LoginSignup
0
2

More than 5 years have passed since last update.

rails: wheneverを使ってrake taskを自動化する。がログが`/private/var/mail`に送られてきてハマる。

Posted at

初期セットアップ

初期セットアップは以下を参考に

やりたいこと

テスト

コンソール上にtest case passedと表示するテストバッチを作成し、そのタスクをwheneverを使って自動化させてみる。一分ごとにtest case passedと表示する。

schedule.rb
# 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
batches.rake
# lib/tasks/batches.rake

namespace :batches do

 desc "test"
    task :test_whenever => :environment do
      puts "test case passed"
    end
end

Screen Shot 2016-11-25 at 5.33.10 PM.png

バッチの動作確認が完了したところで実際にスケジューリングに入る。

bundle exec wheneverwhenever --update-crontabでタスクの確認と登録。しかしなぜかログが/private/var/mailの方に来る。。一応ちゃんと実行はされているのだが。

Screen Shot 2016-11-25 at 6.38.06 PM.png

そしてなぜか怒られれてもいるみたいだ。

エラー一覧

なぜか怒られる/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を本番バッチに変えてみて動かしてみる

を繰り返してエラーが出る度に原因を突き止めていけば動くようになると思います。とりあえず今はまってる現状を脱会できる策を探してみます。

0
2
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
0
2