外部模块中的 Rails Resque 未定义方法错误
我在从 resque 工作线程中包含的模块调用方法时遇到问题。在下面的示例中,当我尝试在工作程序(位于 TestLib 模块中)内调用 say
方法时,我不断收到未定义的方法错误。我已将代码简化为最基本的内容来说明问题:
控制器 (/app/controllers/test_controller.rb)
class TestController < ApplicationController
def testque
Resque.enqueue( TestWorker, "HI" )
end
end
库 (/lib/test_lib.rb)
module TestLib
def say( word )
puts word
end
end
工人 (/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
我使用以下命令运行 resque:rakeenvironment resque:work QUEUE='*'
Gems: 导轨 (3.0.4) 雷迪斯(2.2.2) redis 命名空间 (1.0.3) resque(1.19.0)
服务器: nginx/1.0.6
有人知道那里发生了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您包含模块时,其方法将成为实例方法。当你扩展时,它们就变成了类方法。您只需将
include TestLib
更改为extend TestLib
就可以了。When you include a module, its methods become instance methods. When you extend, they become class methods. You just need to change
include TestLib
toextend TestLib
and it should work.