使用Iron_worker_ruby宝石时,铁工人内的继承



我正在考虑使用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

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

我建议使用我们的下一代宝石,iron_worker_ng:https://github.com/iron-iron-irion/iron_worker_ruby_ng。铁_工厂的宝石被弃用。如果您想保持与拥有的样式相似的样式,那么您的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'

然后将其上传到铁工:

iron_worker upload child_worker

然后您可以开始为此排队工作:

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

如果使用iron_worker_ng,也可以定义run方法。当铁工运行时,将调用此方法。您必须在.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'

相关内容

  • 没有找到相关文章

最新更新