简体   繁体   English

Rails Resque外部模块中未定义的方法错误

[英]Rails Resque undefined method error in external module

I'm having trouble calling methods from an included module inside a resque worker. 我无法从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). 在下面的示例中,当我尝试在worker(在TestLib模块中)中调用say方法时,我一直得到未定义的方法错误。 I've reduced the code down to bare basics to illustrate the issue: 我已将代码简化为基础知识来说明问题:

Controller (/app/controllers/test_controller.rb) 控制器 (/app/controllers/test_controller.rb)

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

Library (/lib/test_lib.rb) (/lib/test_lib.rb)

module TestLib
  def say( word )
    puts word
  end
end

Worker (/workers/test_worker.rb) 工人 (/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) Rakefile (resque.rake)

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

I'm running resque using the following command: rake environment resque:work QUEUE='*' 我正在使用以下命令运行resque: rake environment resque:work QUEUE='*'

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

Server: nginx/1.0.6 服务器:nginx / 1.0.6

Anyone have any ideas as to what's going on there? 任何人都对那里发生了什么有任何想法?

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. 您只需要更改include TestLibextend TestLib ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM