Sidekiq 101

The latest versions as May 26, 2022:

The creator, Mike Perham, has prolific blog: https://www.mikeperham.com/.

In particular you should read this post: A Tour of the Sidekiq API.

Sidekiq Documentation and the Discussions for questions and ideas.

bin/sidekiq --config ./config/sidekiq.yml --concurrency 1 starts a Sidekiq Process.

Sidekiq Job is a unit of work.
Perform a Sidekiq Job pushes a job to Sidekiq Queue.
A Sidekiq Queue is a list of jobs ready to execute now.
Sidekiq Process uses Ruby Threads that to execute jobs from Sidekiq Queues.

Next are some common tasks in Sidekiq.

class SomeJob
  include Sidekiq::Job
end

SomeJob.perform_async

SomeJob.perform_bulk [[args_for_somejob], [args_for_somejob], [args_for_somejob]]

Sidekiq::Queue.new("queuename")

retry_set = Sidekiq::RetrySet.new

worker_names = [
  "YourJobClassA",
  "YourJobClassB",
]

worker_names.each do |name|
  count = retry_set.scan(name).count { |job| job.display_class == name }
  puts "Deleting #{count} jobs of #{name} from Sidekiq Retries."
  retry_set.scan(name).select { |job| job.display_class == name }.map(&:delete)
  puts "Done."
end

Based on A Tour of the Sidekiq API.

Sidekiq’s default Retry formula:

(count**4) + 15 + jitter

and you can custom this retry using sidekiq_retry_in block:

class SidekiqWorker
  include Sidekiq::Job
  sidekiq_options retry: 5 # default is 25

  sidekiq_retry_in do |count, exception|
    case exception
    when SpecialException
      10 * (count + 1) # (i.e. 10, 20, 30, 40, 50)
    end
  end

  def perform
    ...
  end
end

Cluster is for caching and similar workloads that can scale horizontally. That is not Sidekiq.
https://github.com/mperham/sidekiq/issues/4012

Requires Sidekiq Enterprise Unique Jobs feature.

class MinuteWorker
  include Sidekiq::Job

  sidekiq_options queue: "critical", retry: false
  sidekiq_options unique_for: 1.minute, until: :start
end

# config/initializers/sidekiq.rb
config.periodic do |periodic|
  periodic.register "* * * * *", "MinuteWorker"
end