yiqun
yiqun

Reputation: 347

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

Upvotes: 8

Views: 14701

Answers (2)

Amokrane Chentir
Amokrane Chentir

Reputation: 30385

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!

Upvotes: 4

Unixmonkey
Unixmonkey

Reputation: 18784

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

Upvotes: 14

Related Questions