rails的cron工作:最佳实践?

本文翻译自:A cron job for rails: best practices?

What's the best way to run scheduled tasks in a Rails environment? 在Rails环境中运行计划任务的最佳方法是什么? Script/runner? 脚本/亚军? Rake? 耙?


#1楼

参考:https://stackoom.com/question/1CKL/rails的cron工作-最佳实践


#2楼

Probably the best way to do it is using rake to write the tasks you need and the just execute it via command line. 可能最好的方法是使用rake编写你需要的任务,然后通过命令行执行它。

You can see a very helpful video at railscasts 您可以在railscast上看到非常有用的视频

Also take a look at this other resources: 另请查看其他资源:


#3楼

The problem with whenever (and cron) is that it reloads the rails environment every time it's executed, which is a real problem when your tasks are frequent or have a lot of initialization work to do. 每次(和cron)的问题是它每次执行时都会重新加载rails环境,这在你的任务频繁或需要进行大量初始化工作时才是真正的问题。 I have had issues in production because of this and must warn you. 因此我在制作方面遇到了问题,必须警告你。

Rufus scheduler does it for me ( https://github.com/jmettraux/rufus-scheduler ) Rufus调度程序为我做( https://github.com/jmettraux/rufus-scheduler

When I have long jobs to run, I use it with delayed_job ( https://github.com/collectiveidea/delayed_job ) 当我有很长的工作要运行时,我将它用于delayed_job( https://github.com/collectiveidea/delayed_job

I hope this helps! 我希望这有帮助!


#4楼

I'm a big fan of resque / resque scheduler . 我是resque / resque调度程序的忠实粉丝。 You can not only run repeating cron-like tasks but also tasks at specific times. 您不仅可以在特定时间运行重复的类似cron的任务,还可以运行任务。 The downside is, it requires a Redis server. 缺点是,它需要一台Redis服务器。


#5楼

In our project we first used whenever gem, but confronted some problems. 在我们的项目中,我们首先使用gem,但遇到了一些问题。

We then switched to RUFUS SCHEDULER gem, which turned out to be very easy and reliable for scheduling tasks in Rails. 然后我们切换到RUFUS SCHEDULER gem,结果证明在Rails中调度任务非常容易和可靠。

We have used it for sending weekly & daily mails, and even for running some periodic rake tasks or any method. 我们已经将它用于发送每周和每日邮件,甚至用于运行一些定期的佣金任务或任何方法。

The code used in this is like: 这里使用的代码如下:

    require 'rufus-scheduler'

    scheduler = Rufus::Scheduler.new

    scheduler.in '10d' do
      # do something in 10 days
    end

    scheduler.at '2030/12/12 23:30:00' do
      # do something at a given point in time
    end

    scheduler.every '3h' do
      # do something every 3 hours
    end

    scheduler.cron '5 0 * * *' do
      # do something every day, five minutes after midnight
      # (see "man 5 crontab" in your terminal)
    end

To learn more: https://github.com/jmettraux/rufus-scheduler 要了解更多信息,请访问: https//github.com/jmettraux/rufus-scheduler


#6楼

Once I had to make the same decision and I'm really happy with that decision today. 一旦我做出同样的决定,我今天对这个决定感到非常满意。 Use resque scheduler because not only a seperate redis will take out the load from your db, you will also have access to many plugins like resque-web which provides a great user interface. 使用resque调度程序,因为不仅单独的redis将从数据库中获取负载,您还可以访问许多插件,如resque-web,它提供了一个出色的用户界面。 As your system develops you will have more and more tasks to schedule so you will be able to control them from a single place. 随着系统的发展,您将有越来越多的任务要安排,这样您就可以从一个地方控制它们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值