简体   繁体   English

delay_job传递实例方法和变量?

[英]delayed_job passing instance methods & variables?

I have a problem with getting application data from outside of self.method. 我从self.method外部获取应用程序数据时遇到问题。

To useModel.delay.method, the method has to be a class one. 要使用Model.delay.method,该方法必须是一类。

I really like that, it does all I need (like saving input parameters when calling method), but what I need to do is get variables from outside of method. 我真的很喜欢,它可以满足我的所有需求(例如在调用方法时保存输入参数),但是我需要做的是从方法外部获取变量。

For example: User clicks button and starts the timer. 例如:用户单击按钮并启动计时器。

QuizzesController.delay(run_at: @quiz.time_limit.minutes.from_now).form_timeout(session[:user_id],session[:test_password])

Method form_timeout looks like this: 方法form_timeout看起来像这样:

def self.form_timeout(id, test_password)
  if test_password == session[:test_password]
    Score.create(user_id: id, name: test_password, points: -1) 
    session.delete(:test_password)
  end
end

When I try to do that, I get an error "undefined method session" which means that I cannot reach that data when using self.method. 当我尝试执行此操作时,出现错误“未定义的方法会话”,这意味着使用self.method时无法访问该数据。

Any idea how to bypass that and let session[:test_password] (session has to pass most up-to-date information, because I compare session[:test_password] from time when timer has started (old one) and from time when it ends(new one)) like i did in self.method? 任何想法如何绕过它并让session[:test_password] (会话必须传递最新信息,因为我比较了session[:test_password]从计时器开始(旧的)开始到结束时的时间(新的))就像我在self.method中所做的一样?

Any idea how to bypass that and let session[:test_password] ... 任何想法如何绕过它,让session[:test_password] ...

Nope, no way of doing that. 不,这没有办法。 session is not a simple instance methods. session不是简单的实例方法。 It gets data from cookies (by default). 它从cookie获取数据(默认情况下)。 And cookies are inaccessible in a background worker context. 在后台工作人员上下文中无法访问Cookie。

Rule of thumb for background workers is: delayed method must receive all data it needs in its parameters. 后台工作人员的经验法则是:延迟方法必须在其参数中接收其所需的所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM