1. whenever
First, whenever is based on the linux cron service. Therefore, there is no direct way to use the 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:
Copy codeThe Code is as follows:
$ 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.
Copy codeThe Code is as follows:
$ 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 conversion cron task and will not be written to the cron task table.
$ Whenever-w # Write to cron task table and start execution
$ Whenever-c # cancel a task
To view the cron task table, run the linux Command to list all cron tasks:
$ Crontab-l
2. sidetiq
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:
Copy codeThe Code is as follows:
$ [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.
3. clockwork
Clockwork is the same as sidetiq and does not depend on cron. It can adapt to the "cross-platform" requirement. 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'