2

I'm trying to pass a list of arguments to a backgroundrb
in documentation it says:
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"}

any ideas how to solve this??

Daniel Pietzsch
  • 1,369
  • 14
  • 21
AndreDurao
  • 5,600
  • 7
  • 41
  • 61
  • Well, you may want to look at the code. In the example, "charge_customer" accepts one argument, so sending :arg with just the one argument seems alright. I'm wondering why you changed from :arg to :args though.. Have you tried just :arg => [1,2,3] ? – Trevoke Feb 08 '10 at 16:10

2 Answers2

1

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.

David J.
  • 31,569
  • 22
  • 122
  • 174
0

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.

Daniel Pietzsch
  • 1,369
  • 14
  • 21
  • Thanks for the answer Daniel, altough nowadays I almost forgot what I was using in that time (I asked that in feb 2010). I think I switched to delayedjob to solve this.. – AndreDurao Jun 02 '11 at 11:43