Sidekiq and Sidekiq Scheduler ChecklistFriday, 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.
- 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
- Run to test worker code
- Install Sidekiq gems
gem install sidekiq Gem install sidekiq-scheduler
- 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.
- Create a worker file.
require xxxx class TestWorker include Sidekiq::Worker def perform puts '😈 Run classifications' end end
- Run Sidekiq. It’s a good idea to add the command to your Prockfile.
worker: bundle exec sidekiq
- Run worker in your codebase. You can add simple params like a resource id. Don’t pass the entire resource.
- 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
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.