Check if I have a delayed_job process or not.

I have a Rails application in which I use delayed_job. I want to determine if I have a delayed_job process or not; sort of

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

But I canโ€™t figure out how to do this. This is what I am using now:

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

Another solution, as @MrDanA said:

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

but they are weak IMHO solutions. Can anyone suggest a better solution?

+7
source share
5 answers

, , - . delayed_job , . , video_processing, 0/null. , delayed_job ( ), hooks from delayed_job . 0.

/ video.video_processing? ? "Video Transcoding in Progress" : "Video Fished Transcoding"

+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
+1

:

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

Unix "_" .

, bin script, "not_a_delayed_job", .

+1
source

How about ENV ['PROC_TYPE']
Speaking only about heroics ... but when you are a working dinosaur, it is set as a "working", I use it as "I'm in a DJ"

0
source

You can create a plugin for deferred work, for example, create a file is_dj_job_plugin.rbin is_dj_job_plugin.rb config/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

Then you can check as follows:

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

PrintDelayedStatus.new.run
PrintDelayedStatus.new.delay.run
0
source

All Articles