Skip to main content Link Search Menu Expand Document (external link)

Active Job

In this article, we will learn how to make job that will run on background to make our application more optimize.

ActiveJob specializes on task/procedure that takes time to be completed. By using the ActiveJob, the process will be done on background making the user have more time on their hand.

Table of contents

  1. What is Active Job?
  2. Setup
  3. Creating a Job

What is Active Job?

Active job is the framework in Ruby on Rails (ROR) for declaring, scheduling, and executing background tasks and having them execute on multiple queuing backends.

Setup

ActiveJob, a single processing interface, was added to Rails 4.2+ versions. Just as ActiveRecord may support several databases via different Queue Adapters, ActiveJob can support a variety of scheduling tools.

The scheduling tool that we will use is sidekiq. Let’s install sidekiq.

Reminder:

Make sure that your containers are up and running.

In your gemfile, add gem 'sidekiq'.

gem 'sidekiq'

Then run bundle install.

 root@0122:/usr/src/app# bundle install
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
...
Installing connection_pool 2.3.0
Fetching redis-client 0.12.1
Installing redis-client 0.12.1
Fetching sidekiq 7.0.3
Installing sidekiq 7.0.3
Bundle complete! 27 Gemfile dependencies, 100 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

The Active Job adapter must be set to :sidekiq otherwise it will use the Rails default value of :async. This can be done in config/application.rb. While we are at it, let’s also add a configuration that will read the jobs we make in our application.

# config/application.rb

class Application < Rails::Application
  # ...
+
+ config.active_job.queue_adapter = :sidekiq
+ config.eager_load_paths += %W( #{config.root}/app/jobs )
end

When using sidekiq, the local Mac needs also to install Redis, Since we are using docker, we just need to make sure that we have redis on our compose file.

# docker-compose.yml

services:
  redis:
    image: redis
    command: ["redis-server", "--appendonly", "yes"]
    ports:
      - "6379:6379"

Sidekiq will try to connect to Redis by default at localhost:6379, which must be adjusted in Docker’s Redis URL.

Create config/initializers/sidekiq.rb, then add these config.

# config/initializers/sidekiq.rb

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://redis:6379' }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://redis:6379' }
end

Creating a Job

To create a new job, we may use the rails generator.

 root@0122:/usr/src/app# rails generate job Sample
      create  app/jobs/sample_job.rb

Sidekiq requires you to specify which queues it should handle when it starts. This may be accomplished by listing the queue names in config/sidekiq.yml.

# config/sidekiq.yml

:queues:
  - critical
  - default
  - low
  - lowest

Use the -C flag to tell Sidekiq where the sidekiq config file is located, do this on Procfile.dev.

# Procfile.dev

  # ...
+ sidekiq: bin/sidekiq -C config/sidekiq.yml

Jobs can be added to the job queue from anywhere.

irb(main):001:0> SampleJob.perform_later
xxxx-xx-xxxxx:xx:xx.xxxx pid=116 tid=2r4 INFO: Sidekiq 7.0.3 connecting to Redis with options {:size=>5, :pool_name=>"internal", :url=>"redis://redis:6379"}
Enqueued SampleJob (Job ID: 4988e068-e2f0-48bb-81eb-5a7f9fc1f066) to Sidekiq(default)
=> #<SampleJob:0x000055dfb11ec940 ... >

You can also set a delay when performing a job.

irb(main):001:0> SampleJob.set( wait: 20.minutes ).perform_later
Enqueued SampleJob (Job ID: da0f1d84-b8d8-4da2-97b7-f5ea49e9a2d5) to Sidekiq(default) at xxxx-xx-xx xx-xx-xx UTC
=> #<SampleJob:0x000055dfb2285838 ... >

Sidekiq also has a Web UI interface that allows us to see what tasks are presently being run.

# config/routes.rb

Rails.application.routes.draw do
  # ...
+
+ require 'sidekiq/web'
+ mount Sidekiq::Web => '/sidekiq'
end

That is all for Active Job.


Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.