简体   繁体   English

Ruby on Rails Cron作业示例

[英]Ruby on Rails Cron Job Example

Hi um quite new to rails platform and um looking for a rails cron job scheduling tutorials . 嗨,对于rails平台来说还是一个新手,你正在寻找一个rails cron作业调度教程。 I ve gone through with tutorials use whenever and the other scheduling gems , but um looking for a core ruby implementation with the cron tab on rails . 我已经遍历了教程的用法,无论何时何地,以及其他计划中的gems,但是我一直在寻找一个核心的ruby实现,它的cron选项卡在轨道上。 Thank you in advance 先感谢您

Railscasts has a decent tutorial on using Cron. Railscasts有一个关于使用Cron的体面教程

EDIT 编辑

If you would like an example of how it was implemented from scratch, you could have a look at how the Whenever Gem is implemented here 如果您想了解如何从头开始实施它的示例,您可以看一下如何在这里实现Whenever Gem

To set cron jobs, you can depend the help of a simple gem named WHENEVER 要设置cron作业,您可以依赖名为WHENEVER的简单gem的帮助

Its very easy to implement. 它很容易实现。 Go for it. 去吧。

Many people are against this approach (see this SO thread ) but triggering your application's action with curl/wget from a cron job may be a quick-and-easy solution for periodic tasks. 许多人都反对这种方法(请参阅此SO线程 ),但是使用cron作业中的curl / wget触发应用程序的操作可能是解决周期性任务的快速简便的解决方案。

You just have to keep a few things in mind: 你只需记住一些事情:

  • Keep execution time of your action low (as it will block your application just like a regular web request would do) 减少操作的执行时间(因为它会阻止您的应用程序,就像常规的Web请求一样)
  • Make sure that you do not allow anyone to trigger the action (by using IP restrictions, secret tokens or other security measures) 确保您不允许任何人触发操作(通过使用IP限制,秘密令牌或其他安全措施)

For more information on this topic, I have written an article about it. 有关此主题的更多信息,我写了一篇关于它的文章

For minimal setup of "cron-like" tasks in "core" rails/ruby, I created https://github.com/Ebbe/arask 为了在“core”rails / ruby​​中设置“cron-like”任务的最小化,我创建了https://github.com/Ebbe/arask

No need to install anything (other than the gem) or setup anything outside of rails. 无需安装任何东西(gem除外)或在滑轨之外进行任何设置。

Add gem 'arask' to your Gemfile, run bundle install , rails generate arask:install and rails db:migrate . gem 'arask'添加到您的Gemfile中,运行bundle installrails generate arask:installrails db:migrate

Now you can setup your tasks in the file config/initializers/arask.rb: 现在,您可以在config / initializers / arask.rb文件中设置任务:

arask.create task: 'send:logs', cron: '0 2 * * *' # At 02:00 every day
arask.create script: 'puts "IM ALIVE!"', interval: :daily
arask.create task: 'my:awesome_task', interval: :hourly
arask.create task: 'my:other_awesome_task', interval: 2.hours

The tasks will automatically run if the server is running. 如果服务器正在运行,任务将自动运行。

You can also use and external free service to outsource cronjobs http://guardiano.getpeople.in 您还可以使用外部免费服务来外包cronjobs http://guardiano.getpeople.in

DISCLAIMER: I made it 免责声明:我做到了

I'm using rufus-scheduler , It uses threads to execute functions scheduled. 我正在使用rufus-scheduler ,它使用线程来执行调度的函数。 Very simple to configure. 配置非常简单。 just 3 steps: 只需3个步骤:

1 - Add gem gem 'rufus-scheduler', '~> 3.6' 1 - 添加宝石gem 'rufus-scheduler', '~> 3.6'

2 - Create file config/initializers/scheduler.rb 2-创建文件config/initializers/scheduler.rb

3 - Programming schedule in scheduler.rb: 3 - scheduler.rb中的编程时间表:

require 'rufus-scheduler'
s = Rufus::Scheduler.singleton
s.every '5s' do
    #do every 5 seconds exec this code
    puts "WOWWWWWWWWWWWWWWWWWWWW"
end

scheduler.in '2d' do
  # every 2 days exec this
  puts "Now it's me"
end

For more doubts : https://github.com/jmettraux/rufus-scheduler 如有更多疑问: https : //github.com/jmettraux/rufus-scheduler

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

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