Rails Active Job: your App’s Personal Assistant

Are you tired when your server always crashes because there aren’t enough memory? Do you wish that you have something that will take over your main server responsibilities? Now let me introduce you to the one that will help you solve your problem. Introducing Rails Active Job. Rails Active Job is a framework for declaring jobs and making them run on a variety of queuing backends.

Imagine that your server is a staff, and you are the boss. Then you give tasks to your server. When the volume of tasks is still small your staff won’t face any problem, But the problem will begin to occur when you keep increasing the task. It is the same as your server. At some point, your server will collapse.

Now, what can you do to solve this problem? Yes, the easiest way and cheapest way to do it is using a worker, or job, or background job. On Rails, it called Active Job. With Active Job, you can divide which processes you need to do it on your main server and which processes you can do it later using background job, therefore it won't waste your server capacity. Like the title above, it somehow almost the same as you give your server or in this case your staff an assistant to help him do his job.

Active Job allows you to do your task asynchronously, so it will lessen the burden on your server. You can set when to do your task, you can create a schedule with an Active Job. Here is how to implement an Active Job.

1. Generate Job

You need to run this command below to generate Job file:

rails generate job <job_name>

You can check your job file inside app/job directory. Your job will look like this:

class <job_name> < ApplicationJob
queue_as :default
def perform(*params)
# Do something later
end
end

Now, you are able to write whatever task you want inside “perform” action.

2. Enqueue the Job

After you’ve been successfully creating the job, you need to insert your job to queue so your job can be processed. There is a lot of ways to queue the job, you can set your job to performed as soon as possible, or you can set to be processed after a month and etc. Here some ways to do that, assuming that our job is ‘OurJob’:

OurJob.perform_later param

With perform_later, OurJob will be performed as soon as the queue is free.

OurJob.set(wait_until: Date.tomorrow.noon).perform_later(param)

With this, OurJob will be performed tomorrow at noon. You can change the parameter inside wait_until with any date you want.

OurJob.set(wait: 1.week).perform_later(param)

This will perform OurJob a week from now.

3. Set the Priority

You can set your job priority by changing the queue_as like the code below:

class <job_name> < ApplicationJob
queue_as :low_priority
def perform(*params)
# Do something later
end
end

4. Add Some Callbacks

Same with Rails ActiveRecord, Rails ActiveJob have callbacks too. Here to implement callbacks to your job:

class <job_name> < ApplicationJob
queue_as :default
around_perform :callback_action def perform(*params)
# Do something later
end
private
def callback_action
# Do something before perform
yield
# Do something after perform
end
end

Here is the list of callbacks you can use:

before_enqueue
around_enqueue
after_enqueue
before_perform
around_perform
after_perform

5. Recursive Job

If you want your job to run each month, each week or each day you can do a recursive job. To do this, you just need to call your job itself and set when will it be performed. Here is the code:

class OurJob < ApplicationJob
queue_as :default
def perform(*params)
# Do something later
OurJob.set(wait: 1.week).perform_later(param)
end
end

With the code above, it will call itself next week. But you still need to initiate it manually for the first time before it can automatically call itself.

That’s it some basic how-to-use Active Job. Hope this story can help you to do your current or incoming task.

--

--

--

Stories about designing and building tech that matters to people.

Recommended from Medium

OpenTelemetry .NET SDK progress

Traefik 1.6 — Get Our Latest tetedemoine!

But when you move on or your priorities change so too do the majority of your friends.

BASIC DATA TYPES IN PYTHON

Oraichain Studio — Live Demo Transcript

How To Learn Agile Methodology From Scratch

Introducing Jetbrain IDE plugins for Apache Openwhisk

Why I love Linux for Software Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saddhana Arta Daniswara

Saddhana Arta Daniswara

More from Medium

Computer Hardware

The flip side they don’t tell you about: ESOLANG

An overview of the splat operator in Ruby

Hitting the ground running on a Ruby Sinatra project