DEV Community

Afaq Shahid Khan
Afaq Shahid Khan

Posted on

Supercharging Background Processing with Active Job in Rails

Background processing is a crucial part of modern web applications. Tasks like sending emails, processing images, or cleaning up databases often take too long to be handled in real-time during a user’s request-response cycle. This is where background job processing comes into play. In Rails, Active Job provides a unified interface to interact with various queuing backends. This article will walk you through the basics of Active Job and how you can use it to enhance your Rails applications.

What is Active Job?

Image description
Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. The purpose of Active Job is to ensure that all Rails apps have a job infrastructure in place, regardless of which queueing backend is used. Out of the box, Rails supports several backends including Sidekiq, Resque, Delayed Job, and others.

Setting Up Active Job

To get started with Active Job, you'll need to set up a queueing backend. For this example, we will use Sidekiq, one of the most popular and efficient background processing libraries for Rails.

First, add Sidekiq to your Gemfile:

gem 'sidekiq'
Enter fullscreen mode Exit fullscreen mode

Then run bundle install to install the gem.

Next, configure Sidekiq in your Rails application. Create a file named sidekiq.yml in your config directory:

:concurrency: 5
:queues:
  - default
Enter fullscreen mode Exit fullscreen mode

Then, update your config/application.rb to include:

module YourAppName
  class Application < Rails::Application
    # other configurations
    config.active_job.queue_adapter = :sidekiq
  end
end
Enter fullscreen mode Exit fullscreen mode

Finally, you’ll need to create a file at config/initializers/sidekiq.rb to configure Sidekiq with Rails:

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://localhost:6379/0' }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://localhost:6379/0' }
end
Enter fullscreen mode Exit fullscreen mode

Creating a Job

Creating a job in Rails is straightforward. You can generate a job using the Rails generator:

rails generate job Example
Enter fullscreen mode Exit fullscreen mode

This will create a job file in app/jobs/example_job.rb:

class ExampleJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Do something later
  end
end
Enter fullscreen mode Exit fullscreen mode

You can define the tasks you want to be performed in the perform method. For instance, if you want to send a welcome email to a user:

class WelcomeEmailJob < ApplicationJob
  queue_as :default

  def perform(user)
    UserMailer.welcome_email(user).deliver_now
  end
end
Enter fullscreen mode Exit fullscreen mode

Enqueuing a Job

To enqueue the job, you simply call the perform_later method:

WelcomeEmailJob.perform_later(@user)
Enter fullscreen mode Exit fullscreen mode

This will add the job to the queue, and it will be processed by Sidekiq in the background.

Monitoring and Managing Jobs

Sidekiq provides a web interface to monitor and manage your jobs. Add the following to your routes.rb to mount the Sidekiq web interface:

require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
Enter fullscreen mode Exit fullscreen mode

You can now access the Sidekiq web interface at http://localhost:3000/sidekiq, where you can see job statistics, retry jobs, and more.

Error Handling and Retries

Active Job comes with built-in support for error handling and retries. You can specify the number of retries and the wait time between retries in your job class:

class WelcomeEmailJob < ApplicationJob
  queue_as :default
  retry_on SomeSpecificError, wait: 5.minutes, attempts: 3

  def perform(user)
    UserMailer.welcome_email(user).deliver_now
  end
end
Enter fullscreen mode Exit fullscreen mode

You can also define a method to handle cases when retries are exhausted:

class WelcomeEmailJob < ApplicationJob
  queue_as :default

  def perform(user)
    UserMailer.welcome_email(user).deliver_now
  rescue SomeSpecificError => e
    # Handle the error (e.g., notify an admin)
  end
end
Enter fullscreen mode Exit fullscreen mode

Conclusion

Active Job in Rails provides a robust and flexible framework for background processing, allowing you to offload time-consuming tasks and keep your application responsive. By leveraging Sidekiq or other queueing backends, you can ensure your jobs are processed efficiently. Whether you're sending emails, processing images, or performing complex data operations, Active Job can help you manage these tasks seamlessly.

Start integrating Active Job into your Rails applications today and see the difference in performance and user experience!

Top comments (0)