Delayed_job: how to use handle_asynchronously to work with a function?

yiqun picture yiqun · Mar 19, 2011 · Viewed 12.6k times · Source

The function is:

def createuser(name,pass,time)
   puts name,pass,time
end

I try:

handle_asynchronously :createuser("a","b","c")

and got a error:syntax error, unexpected '(', expecting keyword_end

thank you.

===EDIT===

user database in japen and web server in beijing. so i use this way to create a user.

def createuser(name,pass,time)
   Net::HTTP.get(URI.parse("http://www.example.net/builduser.php?hao=#{name}&mi=#{pass}&da=#{time}"))
end

Answer

Unixmonkey picture Unixmonkey · Mar 19, 2011

You don't need to pass parameters into the handle_asynchronously method, it is just a way to say your method should always be passed into delayed_job.

So in your example:

def create_user(name,pass,time)
  puts name,pass,time
end
handle_asynchronously :create_user

does exactly what you need it to. When you call

create_user('john','foo',Time.now)

is the same thing as calling

delay.create_user('john','foo',Time.now)

I just setup a test app doing exactly this to test the answer, and here is the delayed_job serialized performable handler:

--- !ruby/struct:Delayed::PerformableMethod
object: !ruby/ActiveRecord:User
attributes: 
   name: 
   pass:
   created_at:
   updated_at: 
   method_name: :create_user_without_delay
     args: 
       - John
       - foo
       - 2011-03-19 10:45:40.290526 -04:00