Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Resque undefined method error in external module

I'm having trouble calling methods from an included module inside a resque worker. In the example below, I keep getting undefined method errrors when I attempt to call the say method inside the worker (which is in the TestLib module). I've reduced the code down to bare basics to illustrate the issue:

Controller (/app/controllers/test_controller.rb)

class TestController < ApplicationController
  def testque
    Resque.enqueue( TestWorker, "HI" )
  end
end

Library (/lib/test_lib.rb)

module TestLib
  def say( word )
    puts word
  end
end

Worker (/workers/test_worker.rb)

require 'test_lib'

class TestWorker
  include TestLib

  @queue = :test_queue

  def self.perform( word )
    say( word ) #returns: undefined method 'say' for TestWorker:Class
    TestLib::say( word ) #returns: undefined method 'say' for TestLib::Module
  end
end

Rakefile (resque.rake)

require "resque/tasks"
task "resque:setup" => :environment

I'm running resque using the following command: rake environment resque:work QUEUE='*'

Gems: rails (3.0.4) redis (2.2.2) redis-namespace (1.0.3) resque (1.19.0)

Server: nginx/1.0.6

Anyone have any ideas as to what's going on there?

like image 312
internetoutfitters Avatar asked Nov 15 '11 00:11

internetoutfitters


1 Answers

When you include a module, its methods become instance methods. When you extend, they become class methods. You just need to change include TestLib to extend TestLib and it should work.

like image 119
Peter Brown Avatar answered Sep 21 '22 14:09

Peter Brown