Tuesday 9 August 2016

Active Jobs

Active job is a rails framework for running background jobs, It's included as rails component as same like other components like active record, Active model etc

What is Background Jobs and why do we need?
You’re always striving to give your users a better experience when they use your website or application, 

One of the most important ways to achieve this is by giving them quick server response times and processing the non web responses in the background like Email, Payments, sms, Data processing, Video, audio upload etc..


  • Active Job was first included in Rails 4.2
  • Why to use Active Job instead of directly using Third party Background processing
  • Gives the common place to write the jobs
  • We will be able to switch between the queuing backend without having to rewrite the jobs just by changing the queue and backend processing table.
  • Active Job allows your Rails app to work with any one of the backend queue through a single standard interface

  • Even if you aren’t ready to use a queue in your application, you can still use Active Job with the default Active Job Inline backend Jobs enqueued with the Inline adapter get executed immediately.

Active Job adapters Backends Features


|                   | Async | Queues | Delayed    | Priorities | Timeout | Retries |
|-------------------|-------|--------|------------|------------|---------|---------|
| Backburner        | Yes   | Yes    | Yes        | Yes        | Job     | Global  |
| Delayed Job       | Yes   | Yes    | Yes        | Job        | Global  | Global  |
| Qu                | Yes   | Yes    | No         | No         | No      | Global  |
| Que               | Yes   | Yes    | Yes        | Job        | No      | Job     |
| queue_classic     | Yes   | Yes    | Yes*       | No         | No      | No      |
| Resque            | Yes   | Yes    | Yes (Gem)  | Queue      | Global  | Yes     |
| Sidekiq           | Yes   | Yes    | Yes        | Queue      | No      | Job     |
| Sneakers          | Yes   | Yes    | No         | Queue      | Queue   | No      |
| Sucker Punch      | Yes   | Yes    | Yes        | No         | No      | No      |
| Active Job Async  | Yes   | Yes    | Yes        | No         | No      | No      |
| Active Job Inline | No    | Yes    | N/A        | N/A        | N/A     | N/A     |

Resque and delayed Job

  1. Resque supports multiple queues
  2. DelayedJob supports numeric priorities
  3. Resque workers are resilient to memory leaks
  4. DelayedJob workers are extremely simple and easy to modify
  5. Resque requires Redis
  6. DelayedJob requires ActiveRecord
  7. Resque can only place JSONable Ruby objects on a queue as arguments
  8. DelayedJob can place any Ruby object on its queue as arguments
  9. Resque includes a Sinatra app for monitoring what's going on
  10. DelayedJob can be queried from within your Rails app if you want to add an interface

A. Generating a job
     Active Job provides a Rails generator to create jobs
     rails generate job site_subscription_payment
B. Enqueue the Job
    SiteSubscriptionPaymentJob.perform_later subscription_obj
    SiteSubscriptionPaymentJob.set(wait_until:         Date.tomorrow.noon).perform_later(subscription_obj)
    SiteSubscriptionPaymentJob.set(wait: 1.week).perform_later(subscription_obj)
    SiteSubscriptionPaymentJob.perform_later(subscription_obj, company)
C. Job Execution
For enqueuing and executing jobs in production we need to set up a queuing backend Rails itself only provides an in-process queuing system, which only keeps the jobs in RAM. If the process crashes or the machine is reset, then all outstanding jobs are lost with the default async back-end
D. Setting the Backend  config/application.rb
config.active_job.queue_adapter = :delayed_job
E. Starting the Backend
Since jobs run in parallel to your Rails application, most queuing libraries require that you start a library-specific queuing service (in addition to starting your Rails app) for the job processing to work
F. Queues
MyJob.set(queue: :another_queue).perform_later(record)
queue_as :low_priority
Callbacks

Active Job provides hooks during the life cycle of a job. Callbacks allow you to trigger logic during the life cycle of a job.

A. Available callbacks
before_enqueue
around_enqueue
after_enqueue
before_perform
around_perform
after_perform

 before_enqueue do |job|
    # Do something with the job instance
  end

  around_perform do |job, block|
    # Do something before perform
    block.call
    # Do something after perform
  end

Action Mailer
One of the most common jobs in a modern web application is sending emails outside of the request-response cycle
So the user doesn't have to wait on it. Active Job is integrated with Action Mailer so you can easily send emails asynchronously

# If you want to send the email now use #deliver_now
ApplicationNotifier.user_approved(@user).deliver_now

# If you want to send the email through Active Job use #deliver_later
ApplicationNotifier.user_approved(@user).deliver_later






1 comment:

  1. It is nice blog Thank you provide important information and I am searching for the same information to save my time
    Ruby on Rails Online Training

    ReplyDelete