开发者

How to pass more than one argument to a worker at backgroundrb

I'm trying to pass a list of arguments to a backgroundrb

in documentation it says:

MiddleMan.worker(:billing_worker).async_charge_cust开发者_如何转开发omer(: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"}

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). From a code read, the async_* methods accept both :arg and :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? (I usually add a few puts statements and inspect things when things go wrong.)

Lastly, have you considered using delayed_job? It has more traction nowadays in the Rails community.


Actually, the second method you've tried (args => {:t=>"t", :r=> "r"}) should work.

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. But since a Hash can contain multiple key-value pairs, you can access all of these individually inside your worker.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜