rufus-scheduler gem 使用笔记

Tuesday, May 18, 2010

开始把计划任务脚本放在项目下的初始化(config/initializers/)中, server开了4个mongrel实例,
结果相同的任务重复的执行了4次, 杯具呀 发现可能是mongrel多实例的问题,把脚本给移到了script目录下, 采取“ ruby
script/all_scheduler.rb production& “ 方式来启动计划任务脚本,脚本需要稍许改动

!/usr/bin/env ruby
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(FILE) + "/../config/environment")
require 'rubygems'
require 'rufus/scheduler'
scheduler = Rufus::Scheduler.start_new
scheduler.cron "/15 *  * *" do
  #  do something
  #  Model.scheduled_task #这种方式就要把rails的environment loding进来
  #  system("rake do_something:start RAILS_ENV=production")  # 这种执行方式的话可以不用去loading rails environment, 也就是可以不需要第2和第3行代码
end
scheduler.join  # 要放上这个来启动线程,而在初始化目录中是由 rails启动时顺带启动的

run me "ruby script/all_scheduler.rb production&"

Reference: Loading Your Rails Environment Into a
Script

This entry was tagged Rails and Cron

comments powered by Disqus

© 2009-2013 lxneng.com. All rights reserved. Powered by Pyramid

go to Top