Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 811
yiqun Avatar asked Mar 19 '11 12:03

yiqun


2 Answers

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
like image 184
Unixmonkey Avatar answered Dec 04 '22 18:12

Unixmonkey


Why do you want to pass parameters to the method? Because the problem is * I think * that you are supposed to use it this way:

def create_user
  # do some delayed stuff here
end

handle_asynchronously :create_user, :run_at => Proc.new { 10.minutes.from_now }

Or

handle_asynchronously :create_user, :priority => 10

etc. (so without passing any parameters to the method that you pass as a parameter to handle_asynchronously).

EDIT

A delayed job is a long running task that you want to do asynchronously. handle_asynchronously is called one time, just after the method declaration, so passing parameters is useless because the code inside the method is sharing that scope as well!

like image 31
Amokrane Chentir Avatar answered Dec 04 '22 18:12

Amokrane Chentir