非常感謝您提供的問題解決方案(以及啓發它的答案:-)),即使對於多個工作人員(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