Delayed Job如何在Ruby on Rails中工作?

iCy*_*org 8 ruby-on-rails ruby-on-rails-3

我是新手,对于Delayed Job如何工作有点困惑?

我知道它会创建一个表并将作业放在表中然后我需要运行

rake jobs:work
Run Code Online (Sandbox Code Playgroud)

开始后台进程.现在我的问题是

  1. DJ脚本是否每分钟检查一次表格,当时间与job_at时间匹配时,它会运行该作业吗?

  2. 它是如何与cron(每当gem)不同,如果脚本每分钟只检查一次表?

谢谢

dee*_*our 9

  1. DJ脚本是否每分钟检查一次表格,当时间与job_at时间匹配时,它会运行该作业吗?

当您运行rake jobs:workDelayedJob时,将轮询该delayed_jobs表,执行与job_at列值匹配的作业(如果已设置).这部分你是对的.

  1. 它是如何与cron(每当gem)不同,如果脚本每分钟只检查一次表?

whenever是一个可以帮助您配置crontab的gem.它与定期在服务器上执行任务没有直接关系.

可以设置一个cron来每分钟delayed_job运行队列中存在的任何任务,但是让守护进程运行有多种好处.

  • 即使cron每分钟运行一次,delayed_job守护程序也会看到并执行在cron运行之间的1分钟窗口内排队的任何作业
  • 每次cron运行时,它都将重建一个新的Rails环境,用于执行作业.当守护进程可以立即准备好执行新排队的作业时,这会浪费时间和资源.

如果你想delayed_job每分钟通过cron 进行配置,你可以在你的crontab中添加这样的东西

* * * * * RAILS_ENV=production script/delayed_job start --exit-on-complete
Run Code Online (Sandbox Code Playgroud)

每一分钟,delayed_job都会启动,执行任何准备好的工作,或者它必须从先前失败的运行中重试,然后退出.我不建议这样做.将delayed_job设置为守护进程是正确的方法.


jvn*_*ill 7

DJ脚本是否每分钟检查一次表格,当时间与job_at时间匹配时,它会运行该作业吗?

是.它每5秒检查一次数据库.

它是如何与cron(每当gem)不同,如果脚本每分钟只检查一次表?

在后台工作的背景下,他们并没有那么不同.他们的主要区别在于他们通常如何经营这些工作.

          DJ                  |            Crontab
 uses additional database     | you should either set up a rake task
 table but that's it. easier  | or a runner which can be called on the
 to code compared to crontab  | crontab
------------------------------|------------------------------------------
 requires you to run a worker | requires you to setup your cron which
 that will poll the database  | you can easily do using the whenever gem
------------------------------|------------------------------------------
 since this uses a table, it  | you have to setup some sort of logging so
 is easier to debug errors    | that you have an idea what caused the error
 when they happen             |
------------------------------|------------------------------------------
 the worker should always be  | as long as your crontab is set up properly,
 running to perform the job   | you should have no issues
------------------------------|------------------------------------------
 harder to setup recurring    | easy to setup recurring tasks
 tasks                        |
------------------------------|------------------------------------------
Run Code Online (Sandbox Code Playgroud)