DEV Community

Cover image for Sidekiq in Ruby on Rails for background jobs
Toqeer Abbas
Toqeer Abbas

Posted on

Sidekiq in Ruby on Rails for background jobs

Sidekiq is a full-featured background processing framework for Ruby. It lets you run background tasks concurrently, crucial for responsive and reliable web applications. Whether sending emails, resizing images, or processing CSV files, time-consuming tasks can be done behind the scenes using Sidekiq.

We used Sidekiq to manage complex background jobs like sending emails and performing computations that take time.

Sidekiq Gem
[(https://github.com/sidekiq/sidekiq)]

Let's start to understand Sidekiq with a quick tutorial
rails new sidekiq_tutorial_1.0

Let's go inside the directory
cd sidekiq_tutorial_1.0/

Then add sidekiq in the gem file
bundle add sidekiq

Add background job
rails g sidekiq:job my_first_job

go to the project directory open file my_first_job.rb

class MyFirstJob
  include Sidekiq::Job

  def perform(name, age)
    puts "my name #{name} age #{age}"
    # Do something
  end
end


Enter fullscreen mode Exit fullscreen mode

then go to file application.rb add this line config.active_job.queue_adapter = :sidekiq

add this in application_job.rb file
queue_as :default

let's get the output
run following command in terminal

bundle exec sidekiq

open rails console by typing rails c
MyFirstJob.perform_async("toqeer", 24)

now you can see background job output in sidekiq terminal tab

sidekiq in ruby on rails

[For more tutorial like this you can follow me on youtube:(https://www.youtube.com/@CodeWithNaqvi
)

Top comments (0)