Skip to content

Getting Started

Mike Perham edited this page Mar 6, 2025 · 76 revisions

Sidekiq makes every effort to make usage with modern Rails applications as simple as possible. Please see the ActiveJob page if you want to use Sidekiq with ActiveJob.

Rails

  • Add sidekiq to your Gemfile:
bundle add sidekiq
  • Add these two lines to config/routes.rb:
require "sidekiq/web" # require the web UI

Rails.application.routes.draw do
  mount Sidekiq::Web => "/sidekiq" # access it at http://localhost:3000/sidekiq
  ...
end
  • Add a job class in app/sidekiq, run this command in your shell:
bin/rails generate sidekiq:job hard

It generates the job file as well as the test or spec, depending on the test framework you have configured.

class HardJob
  include Sidekiq::Job

  def perform(name, count)
    # do something
  end
end

Your perform method arguments must be simple, basic types like String, integer, boolean that are supported by JSON. Complex Ruby objects will not work.

To namespace your hard job in a rock namespace:

rails generate sidekiq:job rock/hard

This will generate the following:

class Rock::HardJob
  include Sidekiq::Job

  def perform(*args)
    # Do something
  end
end
  • Create a background job to be processed:
HardJob.perform_async('bob', 5)

Note that perform is an instance method, whereas perform_async is called on the class.

You can also create a job to be processed in the future:

HardJob.perform_in(5.minutes, 'bob', 5)
HardJob.perform_at(5.minutes.from_now, 'bob', 5)

Note: The 5.minutes syntax is Rails-specific. If you are not using Rails, pass in a number representing seconds:

HardJob.perform_in(5*60, 'bob', 5)
  • Start sidekiq from the root of your Rails application so the jobs will be processed:
bundle exec sidekiq

That's it.

Plain Ruby

por.rb in examples/ is a "Plain Old Ruby" example.

Watch an overview:

Getting Started

Next: The Basics

Clone this wiki locally