Rails 的 cron 作业:最佳实践? [英] A cron job for rails: best practices?

查看:34
本文介绍了Rails 的 cron 作业:最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 环境中运行计划任务的最佳方式是什么?脚本/跑步者?耙?我想每隔几分钟运行一次任务.

What's the best way to run scheduled tasks in a Rails environment? Script/runner? Rake? I would like to run the task every few minutes.

推荐答案

我正在使用 rake 方法(由 heroku)

I'm using the rake approach (as supported by heroku)

使用名为 lib/tasks/cron.rake 的文件 ..

With a file called lib/tasks/cron.rake ..

task :cron => :environment do
  puts "Pulling new requests..."
  EdiListener.process_new_messages
  puts "done."
end

要从命令行执行,这只是rake cron".然后可以根据需要将此命令放在操作系统 cron/任务调度程序上.

To execute from the command line, this is just "rake cron". This command can then be put on the operating system cron/task scheduler as desired.

更新这是一个很老的问答!一些新信息:

Update this is quite an old question and answer! Some new info:

  • 我引用的 heroku cron 服务已被 Heroku Scheduler
  • 取代
  • 对于频繁的任务(尤其是您想避免 Rails 环境启动成本的情况),我的首选方法是使用系统 cron 调用一个脚本,该脚本将 (a) 戳一个安全/私有的 webhook API 来调用所需的任务在后台或 (b) 直接将任务排入您选择的排队系统

这篇关于Rails 的 cron 作业:最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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