[英]How to pass more than one argument to a worker at backgroundrb
I'm trying to pass a list of arguments to a backgroundrb 我正在尝试将参数列表传递给backgroundrb
in documentation it says: 在文档中说:
MiddleMan.worker(:billing_worker).async_charge_customer(:arg => current_customer.id) MiddleMan.worker(:billing_worker).async_charge_customer(:arg => current_customer.id)
but it only works for just one argument, I tried these but none worked for me args => [1,2,3] args => {:t=>"t", :r=> "r"} 但是它仅适用于一个参数,我尝试了这些,但没有一个适用于我args => [1,2,3] args => {:t =>“ t”,:r =>“ r”}
any ideas how to solve this?? 任何想法如何解决这个问题?
What you are trying seems reasonable to me. 您的尝试对我来说似乎很合理。 I took a look at rails_worker_proxy.rb (from the github source code).
我看了一下rails_worker_proxy.rb(来自github源代码)。 From a code read, the async_* methods accept both :arg and :args:
从读取的代码中,async_ *方法同时接受:arg和:args:
arg,job_key,host_info,scheduled_at,priority = arguments && arguments.values_at(:arg,:job_key,:host,:scheduled_at, :priority)
# allow both arg and args
arg ||= arguments && arguments[:args]
# ...
if worker_method =~ /^async_(\w+)/
method_name = $1
worker_options = compact(:worker => worker_name,:worker_key => worker_key,
:worker_method => method_name,:job_key => job_key, :arg => arg)
run_method(host_info,:ask_work,worker_options)
Can you share a code snippet? 您可以共享一个代码段吗? Have you added any debugging statements in your code and/or in the backgroundrb code itself?
您是否在代码中和/或backgroundrb代码本身中添加了任何调试语句? (I usually add a few puts statements and inspect things when things go wrong.)
(我通常会添加一些puts语句,并在出现问题时检查情况。)
Lastly, have you considered using delayed_job? 最后,您是否考虑过使用delay_job? It has more traction nowadays in the Rails community.
如今,它在Rails社区中具有更大的吸引力。
Actually, the second method you've tried ( args => {:t=>"t", :r=> "r"}
) should work. 实际上,您尝试过的第二种方法(
args => {:t=>"t", :r=> "r"}
)应该可以工作。
In your worker: 在您的工人中:
def charge_customer(arg)
customer_id = arg[:customer_id]
customer_name = arg[:customer_name]
#do whatever you need to do with these arguments...
end
And then, you can call the worker like this: 然后,您可以像这样呼叫工作人员:
MiddleMan.worker(:billing_worker).async_charge_customer(:arg => { :customer_id => current_customer.id, :customer_name => current_customer.name })
Basically, what you're doing here is pass a single Hash as the one argument the worker accepts. 基本上,您在这里所做的就是传递一个Hash作为工作人员接受的一个参数。 But since a Hash can contain multiple key-value pairs, you can access all of these individually inside your worker.
但是,由于哈希可以包含多个键值对,因此您可以在工作线程中单独访问所有这些键值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.