Background Cron Jobs in Rails Using Sidekiq, Redis, and Whenever gems.

Earlier this week, I was tasked to add a feature that sent out notifications when a certain task didn’t check in from a third-party API. I had to come up with a background job that did this heavy lifting in intervals to keep the users informed of certain failures in certain transactions. This is how I solved this challenge and I hope my approach helps you implement your own solution.

NB: This tutorial is based on a fictional scenario that you could easily learn from and implement in your own project. This tutorial could be useful for scenarios like sending out scheduled marketing emails, app administration, etc.

For this, I needed to work with the Whenever gem for running the scheduled checks/jobs in the background and Sidekiq for executing the jobs(also visualizing the queued jobs ) at the same time in form of threads and Redis memory database to store this data in the memory as the app ran.

So let’s dive into it already.

Add the whenever gem to your Gemfile.

gem 'whenever', require: false

then run to install the gem.

Next we the project by running . Read more about whenever gem options from

A the file will be generated in the project Config folder. In this file, you can add your scheduled task runners. In this example, we shall be working with rake tasks. Read more about rake tasks here


// set where you want to see the logs for jobs at.
set :output, { error: '/log/error.log', standard: '/log/cron.log'}
every 2.hours do
rake 'shopping_cart_notifier:pending_checkout_clearance'

then at this point, let’s go ahead and create the rake tasks because what we have up there is just a placeholder. To be honest you could do this first and add it to the schedule.rb later.


require 'rake'
namespace :shopping_cart_notifier do
desc 'Sends notification about items in the cart that haven't been paid for and checked out.'
task pending_checkout_clearance: :environment do

Let's go ahead and create the actual function that does the job . Now this will require Sidekiq and Redis installed.

Add these gems to your Gemfile and run bundle install.

gem ‘sidekiqgem ‘redis’, ‘~> 4.0’gem ‘letter_opener’ // you may need this to preview emails locally in case its ehat you're using emails as the form of notification.

then go ahead and place this line in the to set up Sidekiq to work within the application. Add at the end right before the keyword.

config.active_job.queue_adapter = :sidekiq

Go to the initializers and configure Sidekiq to use Redis. config/initializers/sidekiq.rb

if Rails.env.production?
redis_host = ''
redis_port = 6379
redis_db = 1
redis_host = 'localhost'
redis_port = 6379
redis_db = 0

Sidekiq.configure_server do |config|
config.redis = { url: "redis://#{redis_host}:#{redis_port}/#{redis_db}" }

Sidekiq.configure_client do |config|
config.redis = { url: "redis://#{redis_host}:#{redis_port}/#{redis_db}" }

Then let’s go to the routes and set up a route to view the Sidekiq user interface by adding these two lines.

require 'sidekiq/web'
authenticate :[user(for example the ordinary user)] do
mount Sidekiq::Web => '/sidekiq'

Then lastly, let's configure our app to use the letter opener for testing to check if the letters were sent through. Add these two lines to the config/environments/development.rb file.

config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true

Then lets create a job file in app/jobs/notify_pending_checkout_job.rb

class NotifyPendingCheckoutJob < ApplicationJob
sidekiq_options retry: 5
queue_as :default

def perform(*_args)
$pending_checkouts = ShoppingCart.where(status: :not_checked_out).where(['created_at < ?', 2.hours.ago]).all
AdminMailer.notify_admin_pending_checkouts($pending_checkouts).deliver! if $pending_checkouts.any? // this is for sending out email notifications

Let’s create the mailer functions now. This can be done in app/mailers/admin_mailer.rb

class AdminMailer<ApplicationMailer
default from: '[some email]@[some_domain].com'
layout 'mailer'

def notify_admin_pending_checkouts(pending_checkout)
to: '[some admin mail]',
subject: "#{@pending_checkout.count} cart items have not been checked out for the past two hours."

Let’s create an html.erb template to display this data to the admin in the tables. Better we do in a table. Create it in app/views/admin_mailer/notify_admin_pending_checkouts.html.erb.

<h1>These users need to be reminded to checkout their cart items. </h1>

// add your display logic here
<table style="width:100%">
<tr style="text-align:center; ">
<th>cart Id</th>
<%= $pending_checkouts.each do |item|%>
do something
<% end %>

To view Sidekiq in the browser, you have to ensure that Redis is installed and running by running in the terminal and then accessing . To do this in your app template on a nav link or button, you can also use the Sidekiq path since it's already added to the routes.

<%= link_to sidekiq_web_path,  class: "[your class]", target: "_blank" do%>
<% end %>

Looks like we’re done with this. So, let’s test it out locally. Go ahead and create a seeder to create mock data. Alternatively, go ahead and open up the rails console and achieve the same result.

Go to the rake task and change the to to be able to see the task in action when you run the rake task in the terminal for instance.

rake 'shopping_cart_notifier:pending_checkout_clearance'

Please remember to revert it after testing. Also, test out the scenario for delivering later by reducing the schedule times in the schedule.rb.

I think that will be all for this post. I will look into writing about using Sidekiq in production managed by Systemd and Redis in my next post.

Read more about Sidekiq:

Thank you for reading.

I am a React, React Native & Rails developer. I love designing user interfaces. I also write music outside when I am not working. Website:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store