読者です 読者をやめる 読者になる 読者になる

牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Rails3 と whenever でバッチ処理のメモ

Rails

(Rails3.2.11)

Installation

Gemfile
gem 'whenever', :require => false

bundle install

Getting started

$ bundle exec wheneverize .
[add] writing `./config/schedule.rb'
[done] wheneverized!

Example User

rails g scaffold User say
app/models/user.rb
class User < ActiveRecord::Base
  attr_accessible :say

  def self.create_say
    self.create(say: "Hi #{Time.now}")
  end
end

Example schedule.rb file

いちおう環境を development にしてみた

set :environment, "development"
every 1.minutes do
  runner "User.create_say"
end

設定確認

いちおうアップデート

$ bundle exec whenever --update-crontab

確認

$ bundle exec whenever

動作確認

$ rails console
irb(main):001:0> User.count
1
#一分後
irb(main):002:0> User.count
2
停止
$ bundle exec whenever --clear-crontab

Help

$ bundle exec whenever --help
Usage: whenever [options]
    -i [identifier],                 Default: full path to schedule.rb file
        --update-crontab
    -w, --write-crontab [identifier] Default: full path to schedule.rb file
    -c, --clear-crontab [identifier]
    -s, --set [variables]            Example: --set 'environment=staging&path=/my/sweet/path'
    -f, --load-file [schedule file]  Default: config/schedule.rb
    -u, --user [user]                Default: current user
    -k, --cut [lines]                Cut lines from the top of the cronfile
    -r, --roles [role1,role2]        Comma-separated list of server roles to generate cron jobs for
    -v, --version