Alpha
Alpha

Reputation: 682

rails 3.0.9 resque-scheduler and delayed job error undefined method enqueue_at

Context of rails 3.0.9, using resque 1.17.1 and resque-scheduler 2.0.0.0d. Trying to follow the document at https://github.com/bvandenbos/resque-scheduler/tree/v2.0.0.d, I've created a resque_scheduler.rake file :

# 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

For the time being I'm only interested in delayed job, so I don't have any resque_schedule.yml file. I've tested my worker class with resque and it is working fine. When I try to add a delay and user enqueue_at in my controller...

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 

...it just fails

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

Any clue or hint to figure out this issue will be appreciated.

Upvotes: 0

Views: 3019

Answers (2)

Alpha
Alpha

Reputation: 682

Here couple of issue. documentation is not alway obvious and assumed that you should know...I didn't. So after digging all over the place I got resque nice and smooth ;-) initializers\resque.rb must reference resque_schedule.

require 'resque_scheduler' 

resque task must be started:

COUNT=5 QUEUE=* rake resque:work

resque-schedule task must be started:

rake resque:scheduler

To monitor resque-schedule, resque-web must be started with the config file of resque as parameter. This one must not reference anything from rails directly as resque-web is a sinatra app and it won't be able to load it properly.

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

Upvotes: 5

barley
barley

Reputation: 4473

Starting both worker and scheduler processes was necessary indeed.

What I found out in addition was that I needed to call

require 'resque_scheduler'

before I called Resque.enqueue_at(...). This was the very cause of "undefined method" error in my case.


And resque-web can be actually hooked to your rails app. Add following lines in "config/routes.rb", reboot rails app, then you can access to the resque-web via $YOUR_RAILS_ROOT_URL/resque.

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

Upvotes: 2

Related Questions