Back to Home

Sidekiq and Sidekiq Scheduler Checklist

Friday, February 4, 2022 at 9:33:02 PM

Like Active Storage, I implement Sidekiq functionality just often enough to forget the exact steps. Documentation for Sidekiq is broad and unspecific. I’ve lost many hours trying to figure out fairly basic functionality. So, let’s write up another checklist.

  1. Create a task to test your worker
namespace :test_task do
    desc "Test worker code"
    task run: :environment do
        puts "✌️ Write test code"
  1. Run to test worker code
rake test_task:run
  1. Install Sidekiq gems
gem install sidekiq
Gem install sidekiq-scheduler
  1. Add Sidekiq configuration. Set timeout to 8 and low concurrency to not throw errors on the Heroku Redis free tier.
# Sample configuration file for Sidekiq.
# Options here can still be overridden by cmd line args.
# Place this file at config/sidekiq.yml and Sidekiq will
# pick it up automatically.
:verbose: false
:concurrency: 5

# Set timeout to 8 on Heroku, longer if you manage your own systems.
:timeout: 8

# Sidekiq will run this file through ERB when reading it so you can
# even put in dynamic logic, like a host-specific queue.
  - critical
  - default
  - <%= `hostname`.strip %>
  - mailers
  - low
# Schedule tasks
    cron: '0 * * * * *'   # Runs once per minute
    class: TestWorker

# you can override concurrency based on environment
  :concurrency: 5
  :concurrency: 5

To set the right cron time, use

  1. Create a worker file.
require xxxx

class TestWorker
  include Sidekiq::Worker

  def perform
    puts '😈 Run classifications'
  1. Run Sidekiq. It’s a good idea to add the command to your Prockfile.
worker: bundle exec sidekiq
  1. Run worker in your codebase. You can add simple params like a resource id. Don’t pass the entire resource.
  1. To run the Sidekiq web interface, add the following to your routes file and
# config/routes.rb
require "sidekiq/web"
require 'sidekiq-scheduler/web'
mount Sidekiq::Web => "/sidekiq"

require 'sidekiq/web'
require 'sidekiq-scheduler/web'
run Sidekiq::Web

You can find additional documentation at Sidekiq and Sidekiq Scheduler

And that’s about it. Let me know if you have any tips or ticks. 👋


This post was not edited. If you see a typo, let me know and I'll fix it.