Rails 的“delayed_job”是吗?真的是为了 cron 任务吗?

发布于 2024-09-16 18:43:41 字数 264 浏览 10 评论 0原文

Delayed_job位于http://github.com/collectiveidea/delayed_job

delayed_job是否有能力执行cron任务?比如每晚凌晨 1 点运行一个脚本。或者每 1 小时运行一个脚本。

如果不能的话,有哪些合适的宝石可以做到这一点?是否可以使用浏览器进行远程监控,并记录成功和错误?

delayed_job is at http://github.com/collectiveidea/delayed_job

Can delayed_job have the ability to do cron task? Such as running a script every night at 1am. Or run a script every 1 hour.

If not, what are the suitable gems that can do that? And can it be monitored remotely using a browser, and have logging of success and error?

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

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

发布评论

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

评论(6

注定孤独终老 2024-09-23 18:43:41

我参与的一个项目尝试使用 DelayedJob 来安排未来的项目。太糟糕了。

相反,我建议您使用whenever gem:

http://github.com/javan/whenever

每当 Ruby gem 提供
定义 cron 作业的清晰语法。
它输出有效的 cron 语法并且可以
甚至为您编写 crontab 文件。
它被设计为与 Rails 良好配合
应用程序并可以部署
卡皮斯特拉诺。每当工作正常时
也可以独立。

代码如下所示(来自 github)

  every 3.hours do
    runner "MyModel.some_process"
    rake "my:rake:task"
    command "/usr/bin/my_great_command"
  end

  every 1.day, :at => '4:30 am' do
    runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
  end

  every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
    runner "SomeModel.ladeeda"
  end

  every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
    runner "Task.do_something_great"
  end

这里有关如何使用的 RailsCast 视频它。

以及相应的 ASCIICast

I worked on a project that tried to use DelayedJob to schedule future items. It sucked.

Instead I recommend you use the whenever gem:

http://github.com/javan/whenever

Whenever is a Ruby gem that provides a
clear syntax for defining cron jobs.
It outputs valid cron syntax and can
even write your crontab file for you.
It is designed to work well with Rails
applications and can be deployed with
Capistrano. Whenever works fine
independently as well.

Code looks like this (from github)

  every 3.hours do
    runner "MyModel.some_process"
    rake "my:rake:task"
    command "/usr/bin/my_great_command"
  end

  every 1.day, :at => '4:30 am' do
    runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
  end

  every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
    runner "SomeModel.ladeeda"
  end

  every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
    runner "Task.do_something_great"
  end

Here's a RailsCast video on how to use it.

And the corresponding ASCIICast.

小…红帽 2024-09-23 18:43:41

我认为 cron 是比 Delayed_job 更好的工具。我之前在一个项目中使用过它,它确实非常擅长在后台或在特定时间运行任务。但是,对于定期发生的重复任务,我认为 cron 是最好的工具。

查看每当(及其Railscast) 轻松安排可以运行 rake 任务(或 thor、shell 脚本或其他任何内容)的 cron 作业。您可以使用 rake 任务更新您的模型,然后使用某种仪表板控制器来查看各种状态。

I think cron is a better tool for this than delayed_job. I've used it in a project before, and it really excels at running at task in the background or at a particular time. But, for recurring tasks that happen at regular times, I think cron is the best tool.

Check out whenever (and its Railscast) to easily schedule cron jobs that can run rake tasks (or thor, or shell scripts, or anything else.) You can use the rake tasks to update your models and then have some sort of dashboard controller that looks at the various statuses.

千仐 2024-09-23 18:43:41

您还可以使用 ClockWork gem:
https://github.com/adamwiggins/clockwork-rails-dj

Clockwork 作为单独的守护进程,可用于触发任何类型的作业,这些作业要么添加到作业排队系统,要么立即运行。

使用 Delayed_Job 发挥它的作用,即可以分布在多个节点(或不分布)的作业排队系统。
使用其他方法在正确的时间将作业添加到队列中。

我使用 rake(或 runner)/cron/whenever gem 来安排后台任务,但发现我的服务器负载非常高,因为我会不断受到 rake/runner 加载 Rails 环境的影响。
Delayed_Job 工作线程是保持运行的 Rails 守护进程,因此您不必在每次需要后台任务时不断启动 Rails。

You can also use the ClockWork gem:
https://github.com/adamwiggins/clockwork-rails-dj

Clockwork runs as a separate daemon and can be used to trigger jobs of any sort that either getting added to a job queueing system or run right away.

Use Delayed_Job for what it's good for, a job queueing system which can be distributed over multiple nodes (or not).
Use something else to add jobs to the queue at the right time.

I was using rake(or runner)/cron/whenever gem to schedule background tasks but was finding my server load was just so high because I would be getting hit constantly with rake/runner loading up the rails environment.
Delayed_Job workers are your rails daemons that stay running so you aren't constantly firing up Rails every time a background task is required.

像你 2024-09-23 18:43:41

每当工作都很棒。

我也喜欢 rufus-scheduler

/config/initializers/task_scheduler.rb

然后在该文件中:

scheduler = Rufus::Scheduler.start_new  

scheduler.every("1m") do  
   DailyDigest.send_digest!  
end 

我最初发现这个帖子 这里

我已经尝试过了,效果很好。

更新

现在我回头看那个链接,它几乎是我想工作的唯一一家铁路公司。他们创造了许多宝石,并为社区做出了很多贡献。更不用说他们有一个庞大的团队!

Whenever works great.

I also like rufus-scheduler

/config/initializers/task_scheduler.rb

Then in that file:

scheduler = Rufus::Scheduler.start_new  

scheduler.every("1m") do  
   DailyDigest.send_digest!  
end 

I originally found this posted here

I've tried it and it works well.

update

Now that I look back at that link it's pretty much the only rails company that I would want to work for. They have made some many gems and add such much to the community. Not to mention they have a huge team!

兰花执着 2024-09-23 18:43:41

我运行多个 crondelayed_jobs 以进行夜间统计和报告生成,并以一定的时间间隔进行数据报废。我是这样做的:

https://aaronvb.com/articles/循环延迟作业-with-cron.html

I run multiple cron delayed_jobs for nightly statistic and report generating and also for data scrapping at certain intervals. Here's how I do it:

https://aaronvb.com/articles/recurring-delayed-job-with-cron.html

长伴 2024-09-23 18:43:41

我为此创建了一个 gem:

https://github.com/sellect/delayed_cron

它适用于 sidekiq 和目前已延迟作业。希望尽快添加resque。我知道这有点晚了,但它几乎完全符合您的要求。

I created a gem for this:

https://github.com/sellect/delayed_cron

It works with sidekiq and delayed_job currently. Looking to add resque soon. I know this is a bit late, but it does pretty much exactly what you were looking for.

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