gpt4 book ai didi

ruby-on-rails - 延迟作业 - Rails 4

转载 作者:行者123 更新时间:2023-12-04 05:35:50 25 4
gpt4 key购买 nike

我已经安装了延迟作业 ( gem 'delayed_job_active_record' ),但我不明白它的功能。

首先,在安装delayed_job 之后,我希望我的所有delay() 方法都能开箱即用,而不必手动启动delayed_job。

现在在 README 它说

script/delayed_job can be used to manage a background process which will start working off jobs.



在它提到的几行之后,

If you want to just run all available jobs and exit you can use rake jobs:work



这两个和我应该在我的生产服务器中使用哪一个有什么区别?

如果我使用 rake jobs:work 我应该在初始化程序中启动一个新线程吗?
Thread.new do
system(rake jobs:work)
end

这是一个好习惯吗?

最佳答案

使用 script/delayed_job 进行生产 - 您的 capistrano 部署将在推送新代码后调用以下内容

RAILS_ENV=production script/delayed_job stop
RAILS_ENV=production script/delayed_job start

笔记

Rails 4: replace script/delayed_job with bin/delayed_job



通常在开发中,我会在“进程中”执行作业,但可以选择指定一个环境变量来运行,当我想模拟生产时

参见 https://github.com/collectiveidea/delayed_job#gory-details - config/initializers/delayed_job_config.rb
# execute job synch during tests and dev
# or pass arg for async in those environments
delayed_execution = if ENV['DELAY_JOBS']
true
else
!(Rails.env.test? or Rails.env.development?)
end

Delayed::Worker.delay_jobs = delayed_execution

然后,如果您想在单独的进程中延迟在 dev 中运行,请同时启动 rails 和作业进程 - 请注意,延迟的作业不会自动加载更改,您必须每次都重新启动它
$ DELAY_JOBS=true rails s 
$ DELAY_JOBS=true bundle exec rake jobs:work

关于ruby-on-rails - 延迟作业 - Rails 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19958133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com