Learn Active Job in Ruby on Rails

Tram Ho

CONTENT

1. What is ActiveJob?

–  ActiveJob is a framework in rails used to declare jobs and process them in queue order, is the frontend to declare jobs.

–  These jobs can be anything from regularly scheduled cleaning, to billing, to emailing.

–  Anything that can be broken down into small work units and run in parallel independently of the main thread can be queued for execution (backend)

2. What is a background job?

–  BackgroundJob can be called background jobs, jobs that execute on a thread independent of the main thread of the web application.

–  Normally, websites receive requests from users and return a response, but Background jobs are different

=> Starting from a request to the Website but requiring a longer execution time than usual (it can be thought that these requests cannot be processed immediately), we will need to use the Background job

=> Process asynchronously on a separate thread, returning the response to the user.

3. What are the ways to handle background jobs?

– There are 10 ways to handle in the table below:

AsyncQueuesDelayedPrioritiesTimeoutRetries
BackburnerYesYesYesYesJobGlobal
Delayed JobYesYesYesJobGlobalGlobal
QueYesYesYesJobNoJob
queue_classicYesYesYes*NoNoNo
ResqueYesYesYes (Gem)QueueGlobalYes
SidekiqYesYesYesQueueNoJob
SneakersYesYesNoQueueQueueNo
Sucker PunchYesYesYesNoNoNo
Active Job AsyncYesYesYesNoNoNo
Active Job InlineNoYesN/AN/AN/AN/A

note:

Async: Asynchronous handling.

Queues: Queue.

Delayed: Add execution request after perform_later example.

Priorities: The order of jobs can change in the queue.

Timeout: Expiration time

Retries: Retry failed jobs

– By default, Rails provides us with Active job (1 framework to handle Background jobs) built-in queue process (built-in queue).

– Processes in the queue are saved to RAM, so if the server is shut down, the jobs that are not or are in progress will be lost, which can negatively affect our users and services, so it only likes Suitable for small applications or non-critical jobs.

4. How to Create, Configure, Use ActiveJob?

First we have to create a job that you want to use:

With Active job you can send as many parameters as you want, including a complex object like:

  • Basic types (NilClass, String, Integer, Float, BigDecimal, TrueClass, FalseClass)
  • Symbol / Array
  • Date / Time / DateTime
  • ActiveSupport::TimeWithZone / ActiveSupport::Duration /Hash (Keys should be of String or Symbol type)…

config :

Default active job uses queue :default (:async) to save the job, there is also another adapter :inline, To use it, we must configure the following:

config.active_job.queue_adapter = :inline => job will execute immediately in main thread

config.active_job.queue_adapter = :async => job will be executed in another thread pool, which is the default queue adapter, suitable for dev/test environments because it doesn’t need external infrastructure, but it’s not suitable in production environments, as it will eliminate quit pending jobs on reboot.

We can configure the number of active threads as follows:

5. Queue

We can push background work to specific queue and check priority

You can preset queue names for all your jobs using:

config.active_job.queue_name_prefix in file application.rb:

– You can configure the queue in the job as follows:

– You can also configure when calling the job:

6. Callback in ActiveJob?

Similar to ActiveRecord, Active Job also uses callbacks to interfere with the lifecycle of a job

7. Handling exceptions, Retry or Discard failed Job ?

– Default Job failed won’t be retry that job will be discard always

– In case you want retry or discard for the purpose then we raise exception then use retryon exception to retry default wait 3s and try again 5 times

– Discardon exception to delete the job

8. Refer

https://guides.rubyonrails.org/active_job_basics.html

Share the news now