非常感谢您提供的问题解决方案(以及启发它的答案:-)),即使对于多个工作人员(Rails 3.2.9,Ruby 1.9.3p327),它也适用于我。
它让我担心,我可能会忘记在对lib进行一些更改后重新启动delayed_job,例如,让我在实现之前进行几个小时的调试。
添加以下到我的script/rails
文件,以允许在执行我们每次启动轨道,但每次都不工人开始时间的问题提供的代码:
puts "cleaning up delayed job pid..."
dj_pid_path = File.expand_path('../../tmp/pids/delayed_job.pid', __FILE__)
begin
File.delete(dj_pid_path)
rescue Errno::ENOENT # file does not exist
end
puts "delayed_job ready."
一个小缺点,我与此面对面的是,它也被称为rails generate
例如。我没有花太多时间寻找解决方案,但欢迎提供建议:-)
请注意,如果您使用的是独角兽,则可能需要在调用before_fork
之前将相同的代码添加到config/unicorn.rb
。
- 编辑: 周围多一点与上面的解决方案打后,我终于实现了以下内容:
我创建了一个文件script/start_delayed_job.rb
与内容:
puts "cleaning up delayed job pid..."
dj_pid_path = File.expand_path('../../tmp/pids/delayed_job.pid', __FILE__)
def kill_delayed(path)
begin
pid = File.read(path).strip
Process.kill(0, pid.to_i)
false
rescue
true
end
end
kill_delayed(dj_pid_path)
begin
File.delete(dj_pid_path)
rescue Errno::ENOENT # file does not exist
end
# spawn delayed
env = ARGV[1]
puts "spawing delayed job in the same env: #{env}"
# edited, next line has been replaced with the following on in order to ensure delayed job is running in the same environment as the one that spawned it
#Process.spawn("ruby script/delayed_job start")
system({ "RAILS_ENV" => env}, "ruby script/delayed_job start")
puts "delayed_job ready."
现在我可以在任何需要的位置需要这个文件,包括'script/rails'和'config/unicorn.rb':
# in top of script/rails
START_DELAYED_PATH = File.expand_path('../start_delayed_job', __FILE__)
require "#{START_DELAYED_PATH}"
# in config/unicorn.rb, before before_fork, different expand_path
START_DELAYED_PATH = File.expand_path('../../script/start_delayed_job', __FILE__)
require "#{START_DELAYED_PATH}"
在你的回答中,我们是否也应该提供'-e production'? – nathanvda 2010-09-14 12:19:46
使用rails3这个解决方案不适合我。开始这个过程完全错误:它一直在开始额外的工作。我回到了卡皮斯特拉诺的任务:) – nathanvda 2010-09-14 12:54:21