Rufus-Scheduler is a handy gem that allows you to perform background jobs. Rufus-Scheduler is unique in that it runs in-process and requires no help from outside tools such as cron or database engines. In this article we will show you how to use rufus-scheduler in your Rails applications. Let's get started!
Rails Application Setup
In order to use Rufus Scheduler, we must first add the
rufus-scheduler gem to our Gemfile. To do this, open up your Gemfile and add in the line listed below.
Now run a bundle install to install the gem.
Great, now we need to create an initializer that will contain the jobs we wish to schedule. Create a new initializer called
scheduler.rb in the
config/initializers directory and add in the code listed below.
require 'rufus-scheduler' scheduler = Rufus::Scheduler::singleton # jobs go below here.
Now we are ready to schedule jobs.
Rufus Scheduler provides a simple syntax for scheduling jobs. You can schedule both one time jobs and recurring jobs. One time jobs can be either scheduled at a specific time, or you can tell Rufus to wait a specific amount of time before running the job (a delayed job). For recurring jobs and delayed jobs, you can either use a simple plain English (30m = 30 minutes, etc.) format or you can use the cron format for scheduling jobs. For this article we'll focus on the English format.
Scheduling recurring jobs is easy. Simply use the following format in your scheduler.rb file:
scheduler.every '5s' do # do stuff end
The code above would perform your background task every 5 seconds. The list below will give you an idea of what time units are supported.
|s||Example: 5s - seconds, specifies the number of seconds you wish to wait|
|m||Example: 5m - the number of minutes you wish to wait|
|h||Example: 5h - the number of hours you wish to wait|
|d||Example: 5d - the number of days you wish to wait|
|w||Example: 5w - the number of weeks you wish to wait|
|M||Example: 5M - the number of months you wish to wait|
|y||Example: 1y - the number of years you wish to wait|
For example, the following code would tell Rufus you wish to schedule a job for every 11 and a half hours:
scheduler.every '11h30m' do # do stuff end
Scheduling Delayed Jobs
Scheduling a delayed job is easy. The syntax is similar to the recurring syntax listed above, but we use the
.in method instead of
.every. For example, the following code would run a task 4 hours after the server starts.
scheduler.in '4h' do # do stuff end
Scheduling Jobs for Specific Dates/Times
You can also schedule a job for a specific date and time. To do this we use the
at method. For example, the following code would run at 12:01am on December 1st, 2015.
scheduler.at '2015/12/01 00:01:00' do # do something end
- Because Rufus Scheduler runs in process, the scheduler will reset if your Rails server gets restarted. This means that the timer for your jobs will get reset, so don't count on any monthly or yearly jobs getting called. If you need to persist jobs across server resets, use a job backend. We will show you how to do this in another article
- Rufus does not work with Active Job
- Some additional setup is needed for production environments (see below).
Production servers require a bit of additional setup. On most production web servers, idle Ruby processes are killed. In order for Rufus to work, you'll need to stop this from happening. For Passenger/Nginx you can copy the following code below to your
nginx.conf config file for your website after the line that says
passenger_spawn_method direct; passenger_min_instances 1; passenger_pool_idle_time 0;
Rufus-Scheduler is a simple, easy to use scheduler that provides great functionality. It can be used for stuff like sending emails, cleaning up temp files, and much more. If you have any questions/comments on this article, please leave a comment below. Thanks for reading!
- Rufus Scheduler Github Page: https://github.com/jmettraux/rufus-scheduler