通过在应用程序启动时启动工作程序来初始化“延迟作业” gem [英] Initialize the Delayed Jobs gem by starting the workers on application start
问题描述
我正在使用Ruby on Rails 3.0.9,并且试图设置 delay_job gem。如果重新启动Apache2服务器后,我可以在Terminal\Console中运行以下命令,则所有工作正常:
I am using Ruby on Rails 3.0.9 and I am trying to setup the delay_job gem. All works if, after rebooting the Apache2 server, I run in the Terminal\Console following commands:
RAILS_ENV=development script/delayed_job stop
RAILS_ENV=development script/delayed_job -n 2 start
但是,因为我总是要在应用程序启动时启动工作程序,请在我的 config / initializers / delayed_job.rb
中添加以下代码(可同时处理开发和生产模式):
However, since I always want to start the workers on application start, in my config/initializers/delayed_job.rb
I add the following code (that handles both development and production mode):
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中运行上述命令行时,它不会处理作业。
However, by using the above code and after re-rebooting the Apache2 server, the DJ gem doesn't work anymore as expected. That is, it doesn't process jobs as it makes when I run the above command lines in the Terminal\Console.
如何制作DJ来工作正常吗?有什么问题?
PS:我想这样做是为了使流程自动化。
P.S.: I would like to do that in order to automatize processes.
它表明 config / initializers / delayed_job.rb
文件中的上述代码没有创建 RAILS_ROOT / tmp / pids
目录中与DJ相关的 pids文件。只能通过手动运行上述命令行来创建它们。为什么会发生这种情况?
It seams that the above code in the config/initializers/delayed_job.rb
file doesn't "create" the "pids" files related to DJ in the RAILS_ROOT/tmp/pids
directory. Those are created only by running the above command lines manually. Why this happens?
更新用于 @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
推荐答案
尝试使用以下代码:
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} stop"
system "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
在控制台中对其进行测试将产生以下输出,指示其应可正常工作:
Testing it in the console produced this output which indicates it should work:
Loading development environment (Rails 3.0.9)
ruby-1.9.2-p290 :001 > Rails.root.join('script','delayed_job')
=> #<Pathname:/home/devin/testsoapp/script/delayed_job>
ruby-1.9.2-p290 :002 > "RAILS_ENV=production #{Rails.root.join('script','delayed_job')} -n 2 start"
=> "RAILS_ENV=production /home/devin/testsoapp/script/delayed_job -n 2 start"
ruby-1.9.2-p290 :003 >
这篇关于通过在应用程序启动时启动工作程序来初始化“延迟作业” gem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!