Ruby three kinds of gems to perform periodic tasks introduction to _ruby topics

Source: Internet
Author: User
Tags cron script

1.whenever

First, the whenever is based on the Linux cron service, so there is no direct way to use the gem on the Windows platform. Whenever should technically be a cron translator, translating the Ruby code into a cron script, and handing the recurring task to Cron to actually do it. It may not be worth mentioning to a shell programmer proficient in cron, but not for rubyist. First, we can use the Ruby language to write the task code, control the code at the ruby level, avoiding the switch to some shell scripts, and the cron command is powerful, but I always can't remember its command parameters, in order to avoid going over and over again to man its manual, or Ruby syntax to compare pro-people.

First, install whenever:

Copy Code code as follows:

$ gem Install whenever

Then switch to the Task Authoring folder under project to ensure that there is a config folder under the folder. If you are creating a whenever task in a rails project, the Config folder already exists.

Copy Code code as follows:

$ cd/project
$ wheneverize.

The Whenverize command creates a schedule.rb file under the Config folder, and our task code needs to be defined in that file. The following is an example of the schedule.rb file:

Every 30.minutes do
 runner "Blog.parseall"
end

every 30.minutes,: in => do
 runner "Postweibo.post "
End

every 15.minutes does 
 runner" weibo.update "
end

every 30.minutes: at => do
 Runner" Rssgenerator.generate "
end

every 1.day,: in => ' 2:00 am ' do
 command" cd/var/www/mzread/current/ Public && gunzip-c sitemap1.xml.gz > Sitemap1.xml && touch sitemap1.xml "End

As with the example code, whenever defines three task types by default: Runner, Rake, command, and we can define our own tasks, for example, the following code defines the type of ruby code that is isolated from the rails environment:

Job_type:ruby, cd:p ath &&/usr/bin/ruby ': Task '. RB ' 
 
every:hour do 
 Ruby ' have_a_rest ' 
end

This example describes the Have_a_rest.rb script that executes under the current folder once per hour.

Let's see how to write a task to the Cron service.

$ whenever #不带参数的whenever会显示转换程cron任务的代码, do not write cron task table
$ whenever-w #写入cron任务表, start execution
$ whenever-c #取消任务
If you want to view the Cron task table, you can also use the Linux command to list all cron tasks:

$ crontab-l

2.sidetiq

Sidetiq is Sidekiq's brother, and if you use Sidekiq in a rails project to handle background tasks, it seems natural to use Sidetiq to deliver recurring tasks.

Install Sidetiq:

Copy Code code as follows:

$ [sudo] gem install Sidetiq

To define a recurring task:

Class Myworker 
 include Sidekiq::worker 
 include sidetiq::schedulable 
 
 recurrence {daily} 
 
 def perform 
 # do stuff 
 ... End End 

Sidetiq, like Sidekiq, relies on Redis messages to process messages. These recurring tasks are automatically loaded when the Rails project is started.

3.clockwork

Clockwork, like Sidetiq, does not have to rely on cron to accommodate "cross-platform" requirements. The following is a code example (CLOCK.RB):

Require ' Clockwork ' 
include clockwork 
 
handler do |job| 
 Puts "Running #{job}" 
end 
 
every (10.seconds, ' frequent.job ') 
every (3.minutes, ' less.frequent.job 
') Every (1.hour, ' hourly.job ') 
 
every (1.day, ' Midnight.job ',: at => ' 00:00 ')

To start a task:

$ clockwork clock.rb 
starting clock for 4 events: [Frequent.job less.frequent.job hourly.job midnight.job] 
Tri Ggering Frequent.job

If you want to take a rails environment, add it to the task file:

Require './config/boot ' 
require './config/environment '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.