Active Job in Rails

26 Feb 2019

Ruby on Rails is the framework that provides the code to build most of the part of the application. When we develop the feature, the job is to write the parts of the application that help to perform a specific task. Ruby on Rails contains different sub-framework to complete feature, one of them is Active Job. When rails application requires to perform the queuing backend job Active Job framework can be used to declare job and make it run.

The active job can be used to send emails, calculate billing, send notifications, scheduled clean-ups. It divides the job in small chunks and runs in parallel. Our effort is to provide the customers with a user-friendly and flexible web application.

This can be approached when server response time is effective. So put jobs that take more time of server to execute in a queue and serve in the backend. Scheduling the task in queue backend distribute traffic on the server by allowing to complete the job when the server is free.

To schedule backend job and run the job resque-scheduler, delayed_job gems can be used.

I worked on the project Google Review management that requires to send the notification to the users whenever there is a new google review comes in application. If I have performed send notification in real time to users that will decrease the response time of server. To achieve this, I found the solution to use Active Job framework.

How to create the Active job in rails?

Active Job framework has a rails generator to create job.

$ rails generate job email_notifier





Above will generate the below structure.

invoke test_unit

create test/jobs/email_notifier_job_test.rb

create app/jobs/email_notifier_job.rb

Created job will be as below

class EmailNotifierJob < ApplicationJob

queue_as :default

def perform(*emails)

# perform notification task



ApplicationJob is the base class for Active Job.

Keep Job in the Queue

1] To enqueue the job to be performed as soon as the queuing system is free.

EmailNotifierJob.perform_later user

2]Schedule job in the queue to be performed tomorrow at noon use

EmailNotifierJob.set(wait_until: Date.tomorrow.noon).perform_later(user)

Execution process of Active Job

To process the queueing backend job in production, need to set up the third party queueing rails library. Rails have its own in-process queuing structure that holds the job in RAM. In case of process crash and resetting system, there can be chances to lost all the scheduled backend task work as default async backend. For backend scheduling, active Job has built-in adapters for multiple queuing backends (Sidekiq, Resque, Delayed Job, Scheduler and others ).

Setup Backend

# config/application.rb

module ReviewApp

class Application < Rails::Application

config.active_job.queue_adapter = :sidekiq




Begin Job in Backend

This job serves the Action mailer’s #deliver_later features which are used to send mails in the background as scheduled. Now a day ’s, it is commonly feature require in web application.

Let us consider the adapter of rails library sidekiq .Method #deliver_later sends emails asynchronously to multiple users. Therefore sidekiq start working using this method after Active job setup. Mailers are queued in the queue mailers.

To start sidekiq processing that enqueue:

bundle exec sidekiq -q default -q mailers

In Job queue to send the notification using actionmailer sidekiq as backend


We can also use other rails adaptor, it depends on the requirement to perform Active job in Rails.

Leave a comment:

Recent Blogs
Contact-Us Contact-Us
Newsletter Newsletter