Ruby executes periodic tasks

Source: Internet
Author: User
Tags cron script
1. preface whether using Ruby for system management or using rails for web development, periodic tasks may be encountered. They are subject to a certain period of time (1 hour, 2 days ......) continuously triggered. In Ruby, I think it is very convenient to use sidekiq for a one-time task, while for a periodic task, we need to use gems such as whenever, sidetiq, and Clockwork. 2. Whenever First, whenever is based on the cron service of Linux. Therefore, there is no direct way to use this gem on the Windows platform. Whenever should be regarded as a Cron translator, which translates Ruby code into a Cron script, so that periodic tasks can be handed over to cron for actual completion. It may not be worth mentioning for Shell programmers who are proficient in cron, but it is not for rubyist. First, we can use the ruby language to write the task code and control the code at the ruby level to avoid switching from some shell scripts. In addition, the cron command is very powerful, but I can't remember its command parameters. To avoid having to go to man's manual over and over again, the ruby syntax is quite friendly. First, install whenever:
$ gem install whenever  

Switch to the project folder where the task is written to ensure that there is a config folder under the folder. If you create a whenever task in the rails project, the config folder already exists.
$ cd /project  $ wheneverize .  
The whenverize command will create the schedule. RB file in the config folder. Our task code needs to be defined in this file. The following is an example of the schedule. RB file:
every 30.minutes do  runner "Blog.parseAll"endevery 30.minutes, :at => 17 do  runner "PostWeibo.post"endevery 15.minutes do   runner "WeiBo.update"endevery 30.minutes, :at => 20 do  runner "RSSGenerator.generate"endevery 1.day, :at => ‘2:00 am‘ do  command "cd /var/www/mzread/current/public && gunzip -c sitemap1.xml.gz > sitemap1.xml && touch sitemap1.xml "end
For example, in the sample code, whenever defines three types of tasks by default: runner, rake, and command. We can also define our own tasks. For example, the following code defines the separation from the rails environment, types of Ruby code executed independently:
job_type :ruby, "cd :path && /usr/bin/ruby ‘:task‘.rb"    every :hour do    ruby ‘have_a_rest‘  end  
This example describes how to run the have_a_rest.rb script in the current folder every hour. The following describes how to write a task to the cron service.
$ Whenever # The whenever without parameters will display the code of the cron task in the conversion process, and will not write the cron task table $ whenever-W # Write the cron task table and start to execute $ whenever-C # cancel the task.
To view the cron task table, run the Linux Command to list all cron tasks:
$ crontab -l  
3. sidetiq is a brother of sidekiq. If sidekiq is used in the rails project to process Background tasks, it is natural to use sidetiq to deliver periodic tasks. Install sidetiq:
$ [sudo] gem install sidetiq  
Define periodic tasks:
class MyWorker    include Sidekiq::Worker    include Sidetiq::Schedulable      recurrence { daily }      def perform      # do stuff ...    end  end  

Like sidetiq, sidekiq relies on redis messages to process messages. When the rails project is started, these periodic tasks are automatically loaded and executed. 4. Clockwork is the same as sidetiq and does not depend on cron. It can meet the "cross-platform" requirements. The following is a sample code (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‘)  

Start the task:
$ clockwork clock.rb  Starting clock for 4 events: [ frequent.job less.frequent.job hourly.job midnight.job ]  Triggering frequent.job  
If you want to bring the rails environment, add the following 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.