简体   繁体   English

ActiveJobs方法是实例还是类方法?

[英]Are ActiveJobs methods instance or class methods?

I have my job class and was wondering if I could define a helper method in such way that the variables initialized in one task are not seen by the next task that runs that job. 我有我的工作类,并且想知道我是否可以这样定义一个辅助方法,使得在一个任务中初始化的变量不会被运行该工作的下一个任务看到。

In other words, is the following thread safe? 换句话说,以下线程是否安全?

class OrdersUpdateJob < ActiveJob::Base

  def perform
    p my_method
  end

  def my_method
     @sth ||= 0
  end
end

You have class methods and instance methods. 您有类方法和实例方法。 As any class, class methods definition starts with self. 与任何类一样,类方法定义以self开头。

ActiveJob::Base.methods.select{|m| m == :perform}
=> []
ActiveJob::Base.instance_methods.select{|m| m == :perform}
=> [:perform]

There's no "perfom" method on the base class, but there's one perfom method on the instance methods. 基类上没有“perfom”方法,但实例方法上有一个perfom方法。

I think that what you are asking is if the jobs are run by a class or an instance of that class. 我认为你所问的是作业是由一个类或该类的实例运行的。 Seeing that only instace_methods includes :perform, I'd say that ActiveJobs are run as instances. 看到只有instace_methods包括:perform,我会说ActiveJobs是作为实例运行的。 So you should be able to use instance variables with no threading conflicts. 因此,您应该能够使用没有线程冲突的实例变量。

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

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