What is active job and its purpose in ROR?

In Ruby on Rails (ROR), active job is the framework known for declaring, scheduling, and executing background jobs and making them run on various queuing backends.

We use this for almost every job, such as scheduled clean-ups, billing charges, sending an email, etc.

Instead of pushing off these everyday jobs in the primary process, we can push them onto a queue to execute them sequentially.

The active job framework in Ruby handles anything divided into smaller chunks of work and runs them simultaneously.

Note: The Rails 4.2 version supports the active job framework for creating, queueing (scheduling), and running multiple jobs.

Purpose

The primary aim is to ensure that every Rails app is equipped with a job infrastructure.

Example

Suppose we have to send a professional email that includes thousands of registered employees. Each email takes around 1 second. To send an email to 1500 users will cost 1500 seconds - approximately 25 min.

We would have to wait 25 minutes for the completion of this job, and no user would prefer to wait that long.

If we implement this with an active job, background jobs will run parallel to the normal flow of requests. We can access other applications along with emailing all the employees with ease.

Active job supports parallel jobs
Active job supports parallel jobs

With active job ‘queues’, we can perform multiple actions on running tasks, as shown in the illustration above.

  • DelayedJob: defines jobs and executes them on various queuing backends.

  • Resque: returns a successful response and then schedules some computation to execute later (outside the original request/response cycle).

  • Sidekiq: queueing adapter for storing all the operational data.

Creating an active job

Code

ROR has a built-in generator to create new jobs.

bin/rails generate job send_emails_to_users
invoke test_unit
create test/jobs/send_emails_to_users.rb  
create app/jobs/send_emails_to_users.rb  
[/bash]

This will create a new file called `send_emails_to_users` under the `jobs` directory:  

class SendEmailsToUsers < ActiveJob::Base  
queue_as :default

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

Explanation

The above code snippet generates a Rails job named send_emails_to_users in the bin/rails path.

test/jobs and app/jobs are just paths of Ruby folders where the Ruby file exists.

The class SendEmailsToUsers inherits the ActiveJob base class and includes a method perform() that executes some task.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved