7

我有一个使用delayed_job 的Rails 应用程序。我想检测我是否处于delayed_job 进程中;就像是

if in_delayed_job?
  # do something only if it is a delayed_job process...
else
  # do something only if it is not a delayed_job process...
end

但我不知道怎么做。这就是我现在正在使用的:

IN_DELAYED_JOB = begin
  basename        = File.basename $0
  arguments       = $*
  rake_args_regex = /\Ajobs:/

  ( basename == 'delayed_job' ) ||
  ( basename == 'rake' && arguments.find{ |v| v =~ rake_args_regex } )
end

正如@MrDanA 所说,另一个解决方案是:

$ DELAYED_JOB=true script/delayed_job start
# And in the app:
IN_DELAYED_JOB = ENV['DELAYED_JOB'].present?

但它们是恕我直言的弱解决方案。任何人都可以提出更好的解决方案吗?

4

5 回答 5

1

也许是这样的。在您的类中添加一个字段并在您调用从延迟作业中完成所有工作的方法时设置它:

class User < ActiveRecord::Base
  attr_accessor :in_delayed_job

   def queue_calculation_request
     Delayed::Job.enqueue(CalculationRequest.new(self.id))
   end

   def do_the_work
     if (in_delayed_job)
       puts "Im in delayed job"
     else
       puts "I was called directly"
     end
   end

   class CalculationRequest < Struct.new(:id)
     def perform
       user = User.find(id)
       user.in_delayed_job = true
       user.do_the_work
     end

     def display_name
       "Perform the needeful user Calculations"
     end
   end
end

这是它的外观:

从延迟的工作:

Worker(host:Johns-MacBook-Pro.local pid:67020)] Starting job worker
Im in delayed job
[Worker(host:Johns-MacBook-Pro.local pid:67020)] Perform the needeful user Calculations completed after 0.2787
[Worker(host:Johns-MacBook-Pro.local pid:67020)] 1 jobs processed at 1.5578 j/s, 0 failed ...

从控制台

user = User.first.do_the_work
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 101]]
I was called directly
于 2013-02-13T18:44:31.307 回答
1

我处理这些的方式是通过一个偏执狂的工人。我使用delayed_job 进行上传到我网站的视频转码。在视频模型中,我有一个名为 video_processing 的字段,默认设置为 0/null。每当视频被delayed_job 转码时(无论是创建还是更新视频文件),它都会使用delayed_job 中的钩子,并在作业开始时更新video_processing。作业完成后,已完成的钩子会将字段更新为 0。

在我的视图/控制器中,我可以做video.video_processing? ? "Video Transcoding in Progress" : "Video Fished Transcoding"

于 2013-02-13T17:51:14.643 回答
1

这对我有用:

def delayed_job_worker?
  (ENV["_"].include? "delayed_job")
end

Unix 会将“_”环境变量设置为当前命令。

如果你有一个名为“not_a_delayed_job”的 bin 脚本,那就错了,但不要那样做。

于 2016-10-31T21:55:40.323 回答
0

ENV['PROC_TYPE'] 怎么样
只说到heroku...但是当你是一个工人测功机时,这被设置为'工人'我用它作为我的“我在一个DJ”

于 2014-03-18T05:40:37.743 回答
0

您可以为延迟作业创建插件,例如is_dj_job_plugin.rbconfig/initializers目录中创建文件。

class IsDjJobPlugin < Delayed::Plugin

  callbacks do |lifecycle|
    lifecycle.around(:invoke_job) do |job, *args, &block|
      begin
        old_is_dj_job = Thread.current[:is_dj_job]
        Thread.current[:is_dj_job] = true
        block.call(job, *args) # Forward the call to the next callback in the callback chain
        Thread.current[:is_dj_job] = old_is_dj_job
      end
    end
  end

  def self.is_dj_job?
    Thread.current[:is_dj_job] == true
  end
end

Delayed::Worker.plugins << IsDjJobPlugin

然后,您可以通过以下方式进行测试:

class PrintDelayedStatus
  def run
    puts IsDjJobPlugin.is_dj_job? ? 'delayed' : 'not delayed'
  end
end

PrintDelayedStatus.new.run
PrintDelayedStatus.new.delay.run
于 2019-02-06T23:45:46.060 回答