【发布时间】:2014-05-12 14:02:14
【问题描述】:
我正在使用 Ruby on Rails 3.0.9 并尝试设置 delay_job gem。如果在重新启动 Apache2 服务器后,我在 Terminal\Console 中运行以下命令,则一切正常:
RAILS_ENV=development script/delayed_job stop
RAILS_ENV=development script/delayed_job -n 2 start
但是,由于我总是想在应用程序启动时启动工作程序,因此在我的 config/initializers/delayed_job.rb 中添加以下代码(同时处理开发和生产模式):
if Rails.env.development?
system 'RAILS_ENV=development script/delayed_job stop'
system 'RAILS_ENV=development script/delayed_job -n 2 start'
elsif Rails.env.production?
system 'RAILS_ENV=production script/delayed_job stop'
system 'RAILS_ENV=production script/delayed_job -n 2 start'
end
但是,通过使用上述代码并重新启动 Apache2 服务器后,DJ gem 不再按预期工作。也就是说,它不会像我在 Terminal\Console 中运行上述命令行时那样处理作业。
如何让 DJ 正常工作?有什么问题?
P.S.:我想这样做是为了使流程自动化。
config/initializers/delayed_job.rb 文件中的上述代码没有“创建”RAILS_ROOT/tmp/pids 目录中与 DJ 相关的“pids”文件。这些只能通过手动运行上述命令行来创建。为什么会这样?
@Devin M的
更新
我的config/initializers/delayed_job.rb 包含:
# Options
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 2
Delayed::Worker.max_attempts = 5
Delayed::Worker.max_run_time = 1.hour
Delayed::Worker.delay_jobs = !Rails.env.test?
if Rails.env.development?
system "RAILS_ENV=development #{Rails.root.join('script','delayed_job')} stop"
system "RAILS_ENV=development #{Rails.root.join('script','delayed_job')} -n 2 start"
elsif Rails.env.production?
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} stop"
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
end
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 initialization delayed-job