Delayed_job:如何使用handle_asynchronously来处理函数?

时间:2021-01-18 00:02:57

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

并得到一个错误:语法错误,意外'(',期待keyword_end

thank you.

===EDIT===

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

北京的japen和web服务器中的用户数据库。所以我用这种方式创建一个用户。

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

2 个解决方案

#1


10  

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.

您不需要将参数传递给handle_asynchronously方法,只是说您的方法应该始终传递给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:

我只是设置了一个测试应用程序来完成这个以测试答案,这里是delayed_job序列化的可执行处理程序:

--- !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

#2


3  

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).

等等(所以不将任何参数传递给作为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!

延迟作业是您想要异步执行的长期任务。 handle_asynchronously在方法声明之后被调用一次,因此传递参数是没用的,因为方法内部的代码也在共享该范围!

#1


10  

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.

您不需要将参数传递给handle_asynchronously方法,只是说您的方法应该始终传递给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:

我只是设置了一个测试应用程序来完成这个以测试答案,这里是delayed_job序列化的可执行处理程序:

--- !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

#2


3  

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).

等等(所以不将任何参数传递给作为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!

延迟作业是您想要异步执行的长期任务。 handle_asynchronously在方法声明之后被调用一次,因此传递参数是没用的,因为方法内部的代码也在共享该范围!