使用iron_worker_ruby宝石时铁工人的继承

时间:2012-11-09 10:01:25

标签: ruby ironworker iron.io iron

我考虑将IronWorker用作一个项目,以便我可以轻松扩展它(预计流量高,有大量后台工作)。

为了保持干燥,我尝试使用继承来定义工作者,但我一直收到以下错误:

/usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- base_worker.rb (LoadError)
    from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /task/child_worker.rb:3:in `<top (required)>'
    from /task/runner.rb:344:in `require_relative'
    from /task/runner.rb:344:in `<main>'

这是基础工人类:

#  app/workers/base_worker.rb
require 'net/http'
require 'uri'
require 'json'

class BaseWorker < IronWorker::Base
  attr_accessor :params

  # The run method is what IronWorker calls to run your worker
  def run
    data = custom_run(params)
    common_post_process(data)
  end

   def custom_run(params)
     #to be overwritten in the child class 
   end    

   def common_post_process(data)
      # some common post processing => DRY
      ....
   end       
end

这是一个儿童班:

# app/workers/child_worker.rb 
require 'net/http'
require 'uri'
require 'base_worker.rb'

class ChildWorker  <  BaseWorker
  merge "base_worker.rb"

  def custom_run(params)
     #custom work
  end

end

有关如何解决此问题的任何想法?

2 个答案:

答案 0 :(得分:4)

我建议使用我们的下一代gem,iron_worker_ng:https://github.com/iron-io/iron_worker_ruby_ng。 iron_worker gem已被弃用。如果你想保持它与你拥有的风格相似,你的child_worker.rb可能如下所示:

require 'net/http'
require 'uri'
require_relative 'base_worker.rb'

class ChildWorker  <  BaseWorker

  def custom_run(params)
     #custom work
  end

end

# NG gem doesn't run anything in particular, so to run your method:
cw = ChildWorker.new
cw.custom_run(params)

child_worker.worker文件中:

runtime 'ruby'

file 'base_worker.rb'
exec 'child_worker.rb'

然后将其上传到IronWorker:

iron_worker upload child_worker

然后你可以开始为它排队:

worker = IronWorkerNG::Client.new
worker.tasks.create("child_worker", params)

答案 1 :(得分:1)

如果您使用iron_worker_ng,还可以定义run方法。 IronWorker运行时将调用此方法。您必须在.worker文件中指定类。

# child_worker.rb
class ChildWorker
  def run
    puts "doing the hard work"
  end
end

child_worker.worker文件:

# child_worker.worker
runtime 'ruby'
name 'ChildWorker'
exec 'child_worker.rb', 'ChildWorker'