How to use ActiveJob for background processing in Rails

Techie     April 2023

Introduction

Background processing is an essential component of modern web applications. With the growing demand for high-performance applications, background processing is becoming increasingly important to handle long-running or resource-intensive tasks, such as sending emails, processing files, or generating reports. In the Ruby on Rails framework, ActiveJob is a convenient and straightforward way to perform background processing. ActiveJob provides a unified interface to work with different queuing backends, such as Delayed Job, Resque, or Sidekiq.

In this section, we will explore how to use ActiveJob for background processing in Rails.


What is ActiveJob?

ActiveJob is a high-level interface for declaring, enqueuing, and executing background jobs in Rails applications. It is part of the Rails framework and abstracts away the differences between different queuing backends, allowing developers to switch between them with minimal effort.

ActiveJob defines a simple API that developers can use to define jobs, specify parameters, and enqueue them for execution. Once a job is enqueued, ActiveJob takes care of dispatching it to the selected queuing backend, and then executing it asynchronously. ActiveJob also provides mechanisms for handling retries, failures, and monitoring the status of jobs.

ActiveJob supports a wide range of queuing backends, including in-process queuing, database-backed queuing, and external queuing systems such as RabbitMQ, Amazon SQS, or Google Cloud Pub/Sub. This flexibility makes ActiveJob an excellent choice for any Rails application that needs to perform background processing.


a). Creating an ActiveJob

To create a new ActiveJob, we can use the rails generate job command. This will generate a new job file in the app/jobs directory of our Rails application. For example, to create a job called NotificationJob, we can run:


$ rails generate job Notification


This will create a file called notification_job.rb in the app/jobs directory, with the following contents:


class NotificationJob < ApplicationJob
  queue_as :default

  def perform(*args)
    # Do something later
  end
end


This file defines a new job class called NotificationJob, which inherits from ApplicationJob, the base class for all ActiveJobs in the Rails application. The queue_as method specifies the queue in which the job will be enqueued. In this case, we use the :default queue, which is the default queue for the selected queuing backend.

The perform method is the entry point for the job. This method will be called asynchronously when the job is executed, and it can receive any number of arguments. Inside this method, we can implement the logic for the job, which can include database operations, HTTP requests, or any other kind of processing.


b). Enqueuing a job

To enqueue a job, we can create a new instance of the job class and call the perform_later method on it. For example, to enqueue a NotificationJob with the argument “Hello, world!”, we can write:


NotificationJob.perform_later("Hello, world!")


This will add a new job to the queue, which will be executed asynchronously by ActiveJob.

By default, ActiveJob uses the Async queuing backend, which performs background processing in the same process as the Rails application. This can be useful for development or testing, but it is not recommended for production environments, where a separate queuing backend should be used.


c). Configuring the queuing backend

ActiveJob supports several queuing backends, each with its own advantages and disadvantages. The most common queuing backends are Delayed Job, Resque, and Sidekiq, which provide more robust and scalable background processing than the default Async backend. In this section, we will explore how to configure ActiveJob to use these queuing backends.


i). Delayed Job

Delayed Job is a popular queuing backend for Rails applications. It provides a simple way to perform background processing using a separate process or thread pool. To use Delayed Job with ActiveJob, we need to add the delayed_job gem to our Gemfile and run bundle install . Then, we can configure ActiveJob to use the Delayed Job backend by setting the queue_adapter option in our application configuration:


# config/application.rb
config.active_job.queue_adapter = :delayed_job


With this configuration, ActiveJob will use the Delayed Job backend to enqueue and execute jobs.


ii). Resque

Resque is another queuing backend for Rails applications, based on Redis. It provides a scalable and fault-tolerant way to perform background processing, with support for job priorities, job dependencies, and job retries. To use Resque with ActiveJob, we need to add the resque and resque-scheduler gems to our Gemfile and run bundle install. Then, we can configure ActiveJob to use the Resque backend by setting the queue_adapter option in our application configuration:


# config/application.rb
config.active_job.queue_adapter = :resque


With this configuration, ActiveJob will use the Resque backend to enqueue and execute jobs.


iii). Sidekiq

Sidekiq is a powerful and popular queuing backend for Rails applications, based on Redis and multi-threading. It provides a fast and efficient way to perform background processing, with support for job retries, job batching, and job scheduling. To use Sidekiq with ActiveJob, we need to add the sidekiq gem to our Gemfile and run bundle install. Then, we can configure ActiveJob to use the Sidekiq backend by setting the queue_adapter option in our application configuration:


# config/application.rb
config.active_job.queue_adapter = :sidekiq


With this configuration, ActiveJob will use the Sidekiq backend to enqueue and execute jobs.


Conclusion

ActiveJob is a powerful and flexible way to perform background processing in Rails applications. It provides a unified interface for working with different queuing backends, allowing developers to switch between them with minimal effort. By using ActiveJob, we can offload long-running or resource-intensive tasks from the main request-response cycle, improving the performance and scalability of our applications. With the support for retries, failures, and monitoring, ActiveJob makes it easy to build robust and fault-tolerant background processing workflows.


Thanks for reading, see you in the next one!