3 ways to perform periodic tasks (timed tasks) in Ruby _ruby topics

Source: Internet
Author: User
Tags require cron script

1. Foreword

Whether you use Ruby for System management or web development with rails, you may experience recurring tasks that follow a certain period of time (1 hours, 2 days ...). ) to be triggered continuously. In Ruby, I think it's convenient to use Sidekiq to do a one-time task, and a recurring task needs to be whenever,sidetiq,clockwork and so on.

2.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:

Copy Code code as follows:

Every 30.minutes do
Runner "Blog.parseall"
End

Every 30.minutes,: at =>
Runner "Postweibo.post"
End

Every 15.minutes do
Runner "Weibo.update"
End

Every 30.minutes,: at =>
Runner "Rssgenerator.generate"
End

Every 1.day,: at => ' 2:00 am ' do
Command "Cd/var/www/mzread/current/public && gunzip-c sitemap1.xml.gz > Sitemap1.xml && Touch sitema P1.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:

Copy Code code as follows:

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.

Copy Code code as follows:

$ 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:
Copy Code code as follows:

$ crontab-l

3.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:

Copy Code code as follows:

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.

4.clockwork

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

Copy Code code as follows:

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:

Copy Code code as follows:

$ Clockwork Clock.rb
Starting clock for 4 events: [Frequent.job less.frequent.job hourly.job Midnight.job]
Triggering frequent.job

If you want to take a rails environment, add it to the task file:
Copy Code code as follows:

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.