0
0
Ruby on Railsframework~5 mins

Sidekiq adapter setup in Ruby on Rails

Choose your learning style9 modes available
Introduction

Sidekiq adapter setup lets your Rails app run background jobs fast and efficiently using Sidekiq.

You want to send emails without making users wait.
You need to process large files or images in the background.
You want to handle tasks like notifications or data cleanup asynchronously.
You want to improve app speed by moving slow tasks out of the main request.
You want to use Redis to manage job queues in your Rails app.
Syntax
Ruby on Rails
# config/application.rb or config/environments/production.rb
Rails.application.configure do
  config.active_job.queue_adapter = :sidekiq
end
This line tells Rails to use Sidekiq for background jobs.
Make sure Sidekiq and Redis are installed and running.
Examples
This sets Sidekiq as the adapter for all environments.
Ruby on Rails
# In config/application.rb
module YourApp
  class Application < Rails::Application
    config.load_defaults 7.0
    config.active_job.queue_adapter = :sidekiq
  end
end
This sets Sidekiq only for production environment.
Ruby on Rails
# In config/environments/production.rb
Rails.application.configure do
  config.active_job.queue_adapter = :sidekiq
end
Sample Program

This example shows how to set Sidekiq as the adapter, define a simple job, and enqueue it.

Ruby on Rails
# Gemfile
gem 'sidekiq'

# Run bundle install

# config/application.rb
module MyApp
  class Application < Rails::Application
    config.load_defaults 7.0
    config.active_job.queue_adapter = :sidekiq
  end
end

# app/jobs/hard_worker_job.rb
class HardWorkerJob < ApplicationJob
  queue_as :default

  def perform(name)
    puts "Doing hard work for #{name}"
  end
end

# To enqueue the job in Rails console
HardWorkerJob.perform_later('Alice')
OutputSuccess
Important Notes

Sidekiq requires Redis to be installed and running on your machine or server.

Use bundle exec sidekiq to start the Sidekiq worker process.

Check Sidekiq web UI for monitoring jobs by mounting it in your routes.

Summary

Set config.active_job.queue_adapter = :sidekiq to use Sidekiq in Rails.

Sidekiq runs jobs in the background using Redis queues.

Make sure Redis and Sidekiq worker are running to process jobs.