DEV Community

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

Posted on

1 1

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
)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay