使用delayed_job的循环计划

发布于 2024-10-23 19:55:05 字数 462 浏览 2 评论 0原文

是否可以使用delayed_job执行以下操作:

  1. 定义一个名为Tasks的类

  2. 在Tasks run中有一个方法每 5 分钟后:Tasks.do_processing

  3. 当下一个 5 分钟周期到来时周围,​​然后运行 ​​Tasks.do_processing 如果前一个 do_processing 已完成

这是我有的东西吗我自己创建还是可以delayed_job(或其他一些gem/插件)执行此操作?

Ps.我知道操作系统级别的 cron 作业,但如果我使用它,那就意味着每次 cron“触发”时,它都会重新加载整个 Rails 环境,而delayed_job只需要加载一次。

Is it possible to do the following using delayed_job:

  1. Define a class called Tasks

  2. Have a method in Tasks run after every 5 minutes: Tasks.do_processing

  3. When the next 5 minute cycle comes around, then run Tasks.do_processing only if the previous do_processing has completed

Is this something I have to create on my own or can delayed_job (or some other gem/plugin) do this?

Ps. I know about OS-level cron jobs, but if I used that then it would mean that each time the cron "fired" it would re-load the entire Rails environment, whereas delayed_job only needs to load it once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情愿 2024-10-30 19:55:05

您可以考虑创建一个 Rake 任务或其他类似的独立进程来处理此功能,然后用 守护进程:

什么是守护进程?

守护进程提供了一种简单的方法来包装
现有的 ruby​​ 脚本(例如
自编写的服务器)作为
守护进程并由简单的控制
启动/停止/重新启动命令。

如果需要,您还可以使用守护进程
在守护进程中运行 ruby​​ 代码块
过程并控制这些过程
从主应用程序。

除了这个基本功能之外,
守护进程提供许多高级功能
比如异常回溯和日志记录
(以防你的 ruby​​ 脚本崩溃)和
监控并自动重启
如果您的进程崩溃了。

You may consider creating a Rake task or other similar standalone processes that takes care of this functionality, and then wrapping it up with Daemons:

What is Daemons?

Daemons provides an easy way to wrap
existing ruby scripts (for example a
self-written server) to be run as a
daemon and to be controlled by simple
start/stop/restart commands.

If you want, you can also use daemons
to run blocks of ruby code in a daemon
process and to control these processes
from the main application.

Besides this basic functionality,
daemons offers many advanced features
like exception backtracing and logging
(in case your ruby script crashes) and
monitoring and automatic restarting of
your processes if they crash.

硪扪都還晓 2024-10-30 19:55:05

如果您想做类似的事情:

class MyJob

  run_every 1.day

  def perform
    # code to run ...
  end

end

请尝试以下代码: https://gist.github.com /1024726

If you'd like to do something similar to this :

class MyJob

  run_every 1.day

  def perform
    # code to run ...
  end

end

then try out this code: https://gist.github.com/1024726

擦肩而过的背影 2024-10-30 19:55:05

我想我可能会补充一点, delayed_job_recurring gem 是 @kares 答案要点的扩展。

用法示例:

class MyTask
  include Delayed::RecurringJob
  run_every 1.day
  run_at '11:00am'
  timezone 'US/Pacific'
  queue 'slow-jobs'
  def perform
    # Do some work here!
  end
end

并安排它。在 Rails 应用程序中,您可以将其放入初始值设定项中:

MyTask.schedule!

Thought I might add that the delayed_job_recurring gem is an extension of the gist on @kares's answer.

Usage example:

class MyTask
  include Delayed::RecurringJob
  run_every 1.day
  run_at '11:00am'
  timezone 'US/Pacific'
  queue 'slow-jobs'
  def perform
    # Do some work here!
  end
end

And schedule it. In a rails app, you might put this in an initializer:

MyTask.schedule!
窗影残 2024-10-30 19:55:05

在 DJ 2.1 的生产中使用 this 版本已经有一段时间了。没有黑客行为,使用内置的延迟作业挂钩。只需确保将名为“queue”的列添加到延迟作业表中,我相信 DJ 3 无论如何都会将其添加到表中。

Been using a version of this in production for a while now for DJ 2.1 . No hackery, uses Delayed Jobs built in hooks. Just make sure you add column named 'queue' to the delayed jobs table, which I believe is something DJ 3 added to the table anyways.

过潦 2024-10-30 19:55:05

您可以使用 https://github.com/codez/delayed_cron_job 这是一个延迟作业插件来设置周期工作机会。

You can use https://github.com/codez/delayed_cron_job which is a Delayed Job plugin to set periodical jobs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文