延迟作业如何在 Ruby on Rails 中工作? [英] How does Delayed Job work in Ruby on Rails ?

查看:68
本文介绍了延迟作业如何在 Ruby on Rails 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很陌生,对延迟作业的工作原理有点困惑?

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

rake 工作:工作

启动后台进程.现在我的问题是

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

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

谢谢

解决方案

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

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

<块引用>

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

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

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

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

如果您想每分钟通过一个 cron 配置 delayed_job,您可以将这样的内容添加到您的 crontab 中

* * * * * RAILS_ENV=生产脚本/delayed_job start --exit-on-complete

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

I am new to this and is little confused about how Delayed Job works ?

I know it creates a table and puts the jobs in the table and then I need to run

rake jobs:work

to start the background process. Now my question is

  1. Does DJ script checks the table every minute and when the time matches job_at time, it runs that job ?

  2. How it is different than cron (whenever gem) if the script is just checking the table every min ?

Thanks

解决方案

  1. Does DJ script checks the table every minute and when the time matches job_at time, it runs that job ?

When you run rake jobs:work DelayedJob will poll the delayed_jobs table, performing jobs matching the job_at column value if it's been set. This part you're correct about.

  1. How it is different than cron (whenever gem) if the script is just checking the table every min ?

whenever is a gem that helps you configure a crontab. It has nothing directly to do with performing tasks on your server on a periodic basis.

You could setup a cron to run whatever tasks exist in the queue every minute, but leaving a delayed_job daemon running has multiple benefits.

  • Even if the cron ran every minute, delayed_job's daemon will see and perform any jobs queued within that 1-minute window between cron runs
  • Every time the cron would run, it will rebuild a new Rails environment in which to perform the jobs. This is a waste of time and resources when the daemon can just sit there immediately ready to perform a newly queued job.

If you want to configure delayed_job through a cron every minute you can add something like this to your crontab

* * * * * RAILS_ENV=production script/delayed_job start --exit-on-complete

Every minute, delayed_job will spin up, perform whatever jobs are ready for it or which it must retry from a previously failed run, and then quit. I don't recommend this though. Setting up a delayed_job as a daemon is the right way to go.

这篇关于延迟作业如何在 Ruby on Rails 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆