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"
    end
end
  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.
# http://www.mikeperham.com/2013/11/13/advanced-sidekiq-host-specific-queues/
:queues:
  - critical
  - default
  - <%= `hostname`.strip %>
  - mailers
  - low
  
# Schedule tasks
:schedule:
  test_worker:
    cron: '0 * * * * *'   # Runs once per minute
    class: TestWorker

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

To set the right cron time, use crontab.guru.

  1. Create a worker file.
require xxxx

class TestWorker
  include Sidekiq::Worker

  def perform
    puts '😈 Run classifications'
  end
end
  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.
TestWorker.perform_async(resource_id)
  1. To run the Sidekiq web interface, add the following to your routes file and config.ru.
# config/routes.rb
require "sidekiq/web"
require 'sidekiq-scheduler/web'
mount Sidekiq::Web => "/sidekiq"
# config.ru

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.