gpt4 book ai didi

ruby - 使用 iron_worker_ruby gem 时在 Iron Worker 中的继承

转载 作者:数据小太阳 更新时间:2023-10-29 08:33:09 25 4
gpt4 key购买 nike

我正在考虑将 IronWorker 用于一个项目,以便我可以轻松地扩展它(预期高流量,有很多后台作业)。

为了保持 DRY,我尝试使用继承来定义工作人员,但我不断收到以下错误:

/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>'

这是基本 worker 类:

#  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

知道如何解决这个问题吗?

最佳答案

我建议使用我们的下一代 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)

关于ruby - 使用 iron_worker_ruby gem 时在 Iron Worker 中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13305866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com