Active Jobs in Rails
Introduction: Active Jobs
Active Job is a framework in Ruby on Rails for creating, scheduling and executing jobs on a variety of queuing backends. Background jobs are the ones that run outside the normal flow of request-response cycle. Background jobs may be anything like scheduled clean ups, mailing, etc. When we use background jobs, it will not affect the normal cycle of application which saves time of executing those jobs.
Create your first background job
1
rails generate job job_name
This command will create a file in app/jobs folder, and it also creates a test case in test/jobs. If you want to create a job by yourself,
create a file under app/jobs and inherit your class from ApplicationJob.
app/jobs/send_article_job.rb
1
2
3
4
5
6
class SendArticleJob < ApplicationJob
queue_as :default
def perform(article)
UserMailer.send_article_mail(article).deliver_now
end
end
Queue your job
Jobs can be added to the job queue from anywhere. There is the facility of queueing a job so that the queueing system executes our job when it is free. The command for this is:
1
SendNotificationsJob.perform_later(parameters)
Similarly, if you want your job to execute after 1 day then:
1
SendNotificationsJob.set(wait_until: Time.now.tomorrow).perform_later(user)
Likewise, you can specify the time to execute your background job so that the system executes it on that specified moment as follows:
1
SendNotificationsJob.set(wait: 30.minutes).perform_later(user)
Here, perform_later
and perform_now
will call the perform
method of your job. You can also pass as many parameters as you want in the
perform method of your Job. Jobs may set which queue they are run in with queue_as
or by using the set
method.
Execute your Job
By Default Rails will provide in-process queuing system, in which it keeps the jobs in RAM. The disadvantage of this is, if the machine crashes or if we restart the system, it will cause the loss of existing jobs in the queue. To overcome this disadvantage, you need to add some third party queuing backend where you can queue your jobs. There are several built-in adapters of Active Job-like Sidekiq, Resque, Delayed Job, etc. You can choose any one of the built-in adapters as your queuing backend. The Queue Adapter has the ability to run the job in a non-blocking manner. It either runs on a separate or forked process, or on a different thread.
Install and Configure Queueing Backend
Install any of the built-in adapters of the Active job like sidekiq, resque, delayed job, etc. The installation procedure is different for
different adapters. By default Active job uses :async
adapter. Here, I will be explaining about installing sidekiq
.
Go to: app/config/application.rb
.
1
2
3
4
class Application < Rails::Application
# ...
config.active_job.queue_adapter = :sidekiq
end
Also, if you want to use different queuing backend for specific Job, you can set your queuing backend as follows:
1
2
3
4
5
6
7
class SendNotificationsJob < ApplicationJob
self.queue_adapter = :resque
queue_as :default
def perform(article)
UserMailer.send_notification_mail(article).deliver_now
end
end
This job uses resque
as its backend queue adapter, It overrides the adapter which you have set in application.rb.
Note: Before adding the adapter name make sure that you installed adapters gem and followed the installation procedure.
Summary
Active Job is a framework in Ruby on Rails for creating, scheduling and executing background jobs. This will help users to perform jobs separately from the normal execution cycle of the system.