Delayed_job: how to use handle_ asynchronously to work with a function?

Function:

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

I'm trying to:

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

and got an error: syntax error, unexpected '(', expecting end_keyword

thanks.

=== EDIT ===

user database in japen and web server in Beijing. therefore, I use this method 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 
+6
ruby ruby-on-rails-3 gem delayed-job
source share
2 answers

You do not need to pass parameters to the handle_asynchronously method, this is just a way of saying that your method should always be passed to 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. When you call

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

is the same as a challenge

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

I just installed the test application, doing just that to check the answer, and here is the serialized delayed_job 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 
+10
source share

Why do you want to pass parameters to a method? Because the problem * I think * is that you should use it as follows:

 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 .. (therefore, without passing any parameters to the method that you pass as the handle_asynchronously parameter).

EDIT

Delaying a job is a lengthy task that you want to do asynchronously. handle_asynchronously is called once, right after the method declaration, so the transfer parameters are useless, because the code inside the method also uses this area!

+3
source share

All Articles