简体   繁体   English

Ruby on rails计划任务

[英]Ruby on rails scheduled tasks

This is my first time scheduling a task and I am not sure of the best implementation (or the proper implementation). 这是我第一次安排任务,我不确定最佳实施(或正确的实施)。

My Goal: I have a ruby on rails 4 app setup with twilio and deployed on Heroku. 我的目标:我在rails 4应用程序设置上使用twilio并部署在Heroku上。 I want the app to automatically text all of my users once a week with a customized text message (which is written and created by information in the database). 我希望应用程序每周一次使用自定义文本消息(由数据库中的信息编写和创建)自动为所有用户发送文本。

From research I have come down to the following Gems: Whenever and Rufus-Scheduler . 从研究中我得到了以下宝石: 每当Rufus-Scheduler

I believe that both these gems can get the Job done, but upon reading on the Rufus' docs: "please note: rufus-scheduler is not a cron replacement" I got stuck trying to understand if what I want is indeed a cron job or a "Rufus-Scheduler". 我相信这两个宝石都可以完成工作,但是在阅读Rufus的文档时:“请注意:rufus-scheduler不是cron替代品”我试图理解我想要的东西确实是一个cron工作或一个“Rufus-Scheduler”。

I am left with the following questions: What is a cron job and when is the appropriate time to use it? 我留下了以下问题:什么是cron作业以及何时适合使用它? Why is Rufus-Scheduler not a cron replacement and what does it do differently? 为什么Rufus-Scheduler不是cron替代品,它有什么不同的做法? Which one should I use? 我应该使用哪一个?

rufus-scheduler is not a cron replacement rufus-scheduler不是cron替代品

can be expanded to "rufus-scheduler was never written as a drop-in replacement for cron". 可以扩展到“rufus-scheduler从未被写为cron的替代品”。 The bigger message is "you are a developer and as a developer you should know the environment you inherit and the tools it comes with. You should know when to use them, when to imitate them, when to replace them." 更重要的信息是“你是一名开发人员,作为一名开发人员,你应该了解你继承的环境及其附带的工具。你应该知道何时使用它们,什么时候模仿它们,何时更换它们。”

Rufus-scheduler understands the "* * * * *" syntax of Cron. Rufus-scheduler理解Cron的“* * * * *”语法。 This has led some people to say "rufus-scheduler tries to be a Cron replacement". 这导致一些人说“rufus-scheduler试图成为Cron的替代者”。 It could better be formulated as "some people have abused rufus-scheduler instead of thinking (knowing) that old faithful Cron would have been better in that situation". 它可以更好地表述为“有些人滥用rufus-scheduler而不是思考(知道)老忠实的Cron在那种情况下会更好”。

To become a good developer you should think seriously about some *nix sysadmin skills, else you'll be a pain to work with. 要成为一名优秀的开发人员,你应该认真考虑一些* nix系统管理员技能,否则你将会很痛苦。 It can be as easy as "install, run and manage a linux box in a VirtualBox in your system". 它可以像“在系统中的VirtualBox中安装,运行和管理linux盒子”一样简单。

A rufus-scheduler schedule runs in a Ruby process. rufus-scheduler计划在Ruby进程中运行。 In a vanilla world, one runs rufus-scheduler in the same Ruby process that services http requests (Rails or Sinatra web application), and, oh, oops, the schedules don't run when the application doesn't run. 在一个vanilla世界中,一个在同一个Ruby进程中运行rufus-scheduler来为http请求提供服务(Rails或Sinatra Web应用程序),哦,oops,当应用程序不运行时,调度不会运行。

Cron is a service provided by your *nix operating system. Cron是您的* nix操作系统提供的服务。 Other applications and services on your host rely on it. 主机上的其他应用程序和服务依赖于它。 Cron reads its crontab and runs the script indicated in it at the given times. Cron读取其crontab并在给定时间运行其中指示的脚本。 Thanks to the excellent Whenever, Cron can be told to run scripts in your Rails application. 由于优秀的Whenever,可以告诉Cron在Rails应用程序中运行脚本。

This might interest you as well: https://devcenter.heroku.com/articles/clock-processes-ruby 您可能也会对此感兴趣: https//devcenter.heroku.com/articles/clock-processes-ruby

About Cron: 关于Cron:

Cron is name of program which does scheduled tasks on nix systems. Cron是在nix系统上执行计划任务的程序的名称 what Scheduled Tasks are in Windows, Cron does something similar for Linux at the conceptual level.Cron is one of the most useful tool in Linux or UNIX like operating systems. 什么是计划任务在Windows中,Cron在概念层面为Linux做类似的事情.Cron是Linux或UNIX中最有用的工具之一,如操作系统。 The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron. cron服务(守护程序)在后台运行并不断检查/ etc / crontab文件和/ etc / cron。 / directories. /目录。 It also checks the /var/spool/cron/ directory. 它还检查/ var / spool / cron /目录。

For Scheduling tasks on Heroku 对于Heroku上的调度任务

Good news is that on Heroku there is a thing called Scheduler which is an add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment. 好消息是,在Heroku上有一个名为Scheduler的东西,它是一个附加组件,用于在预定的时间间隔在您的应用程序上运行作业,就像传统服务器环境中的cron一样。 so you really don't need to fiddle/player with cron or gems like whenever. 所以你真的不需要随时随地使用cron或gem来玩弄/玩家。 just use the Scheduler addon on Heroku. 只需在Heroku上使用Scheduler插件即可。

For More info see: https://devcenter.heroku.com/articles/scheduler 有关详细信息,请参阅: https//devcenter.heroku.com/articles/scheduler

A cron job is a program run on an automated time schedule, using the cron software. cron作业是使用cron软件在自动时间表上运行的程序。

Rufus-Scheduler is different from cron because it runs inside of Ruby processes. Rufus-Scheduler与cron不同,因为它在Ruby进程内部运行。

For what you're describing I believe either would be fine. 对于你所描述的,我相信要么会好的。

whenever allows you to write ruby code that would be transformed in a crontab file, which is a file specifying a set of commands and a frequency for each command. whenever允许您编写将在crontab文件中转换的ruby代码时,该文件是指定一组命令和每个命令的频率的文件。 This file is used by cron . 该文件由cron使用

rufus-scheduler is pure ruby, you write ruby and jobs are running in ruby, for example inside your application loop calling scheduler.join , or in another thread calling ruby your/rufus_scheduler_script.rb . rufus-scheduler是纯ruby,你编写ruby并且作业在ruby中运行,例如在你的应用程序循环中调用scheduler.join ,或者在调用ruby your/rufus_scheduler_script.rb另一个线程中。

In my opinion they do the same things, with cron you are using a linux command, but I don't see any other difference. 在我看来他们做同样的事情,使用cron你正在使用linux命令,但我没有看到任何其他区别。

There are other options too , in my experience I have had problems with rufus-scheduler, and whenever (they did not find classes in my Rails app, but maybe was just a quirk), on the other side clockwork worked for me. 还有其他选择 ,根据我的经验,我遇到了rufus-scheduler的问题,并且每当(他们在我的Rails应用程序中找不到类,但可能只是一个怪癖)时,另一方面发条工作对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM