Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether I am in a delayed_job process or not

I have a Rails app in which I use delayed_job. I want to detect whether I am in a delayed_job process or not; something like

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. This is what I'm 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 is, as @MrDanA said:

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

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

like image 615
mdesantis Avatar asked Feb 13 '13 16:02

mdesantis


1 Answers

The way that I handle these is through a Paranoid worker. I use delayed_job for video transcoding that was uploaded to my site. Within the model of the video, I have a field called video_processing which is set to 0/null by default. Whenever the video is being transcoded by the delayed_job (whether on create or update of the video file), it will use the hooks from delayed_job and will update the video_processing whenever the job starts. Once the job is completed, the completed hook will update the field to 0.

In my view/controller I can do video.video_processing? ? "Video Transcoding in Progress" : "Video Fished Transcoding"

like image 186
kobaltz Avatar answered Oct 21 '22 05:10

kobaltz