我有一个使用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?
但它们是恕我直言的弱解决方案。任何人都可以提出更好的解决方案吗?