rails 3.0.9 resque-scheduler和延迟作业错误undefined方法enqueue_at

时间:2011-08-13 01:46:26

标签: ruby-on-rails-3 scheduled-tasks resque

rails 3.0.9的上下文,使用resque 1.17.1和resque-scheduler 2.0.0.0d。 试图在https://github.com/bvandenbos/resque-scheduler/tree/v2.0.0.d关注该文档,我创建了一个resque_scheduler.rake文件:

# Resque tasks
require 'resque/tasks'
require 'resque_scheduler/tasks'    

namespace :resque do
  task :setup do
    require 'resque'
    require 'resque_scheduler'
    require 'resque/scheduler'      

    # you probably already have this somewhere
    Resque.redis = 'localhost:6379'

    # The schedule doesn't need to be stored in a YAML, it just needs to
    # be a hash.  YAML is usually the easiest.
    #Resque.schedule = YAML.load_file('your_resque_schedule.yml')

    # If your schedule already has +queue+ set for each job, you don't
    # need to require your jobs.  This can be an advantage since it's
    # less code that resque-scheduler needs to know about. But in a small
    # project, it's usually easier to just include you job classes here.
    # So, someting like this:
    #require 'jobs'

    # If you want to be able to dynamically change the schedule,
    # uncomment this line.  A dynamic schedule can be updated via the
    # Resque::Scheduler.set_schedule (and remove_schedule) methods.
    # When dynamic is set to true, the scheduler process looks for 
    # schedule changes and applies them on the fly.
    # Note: This feature is only available in >=2.0.0.
    Resque::Scheduler.dynamic = true
  end
end

目前我只对延迟工作感兴趣,所以我没有任何resque_schedule.yml文件。 我用resque测试了我的工人类,它工作正常。当我尝试在控制器中添加延迟和用户enqueue_at时......

def do_delay_job user_id,delay
    Resque.enqueue_at(delay.minutes.from_now, JobDelayer, :user_id => user_id)
    #Resque.enqueue(JobDelayer, user_id) # using basic resque mechanism.
end 

......它只是失败

undefined method `enqueue_at' for Resque Client connected to redis://127.0.0.1:6379/0:Module

任何了解这个问题的线索或提示都将受到赞赏。

2 个答案:

答案 0 :(得分:5)

这里有几个问题。文件并不是很明显,并且假设你应该知道......我没有。因此,经过遍布整个地方后,我得到了很好的恢复;-) initializers \ resque.rb必须引用resque_schedule。

require 'resque_scheduler' 
必须启动

resque任务:

COUNT=5 QUEUE=* rake resque:work
必须启动

resque-schedule任务:

rake resque:scheduler

要监控resque-schedule,必须使用resque的配置文件作为参数启动resque-web。这个不能直接从rails引用任何东西,因为resque-web是一个sinatra应用程序,它将无法正确加载它。

resque-web ~/pathToYourApp/config/initializers/resque.rb 

答案 1 :(得分:2)

确实需要启动worker和scheduler进程。

我发现的另外一点是我需要打电话

require 'resque_scheduler'
在我调用Resque.enqueue_at(...)之前

。这是我案例中“未定义方法”错误的原因。


resque-web实际上可以挂钩到你的rails应用程序。在“config / routes.rb”中添加以下行,重启rails应用程序,然后您可以通过$ YOUR_RAILS_ROOT_URL / resque访问resque-web。

  require 'resque_scheduler'
  mount Resque::Server, :at => "/resque"