Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Background on Rails – why do you need to learn all the background job APIs?

Posted on 2018-06-15 by Rodrigo Urubatan

Rails has a new library called ActiveJob, the idea of this library is to make it easier to create background jobs without worring about what library to use, and the specific API for each one, and even changing it if a better one comes out in the future without changing your application.

ActiveJob will keep one simple API and it already provides an in memory implementation that you can use to test and in development environment, but do not forget to select a real driver for your production environment.

ActiveJob is so cool, that it out of the box, allows you to do things like this:

MyMailer.prepare_email(param1, param2, param3).deliver_later

And the email will be delivered by the job queue, and only this would already be a huge performance improvement for a lot of shitty web applications around.

Of course it is not just that, you can create your own jobs with the simple command:

rails g job MySuperComplexLogic

that will just create a file app/jobs/my_super_complex_logic_job.rb according to the name you’ve passed to the generator.

The file looks like any other Ruby class, because it is a simple ruby class as you can see bellow:

class MySuperComplexLogicJob < ApplicationJob
  queue_as :default
 
  def perform(*args)
    # Do something later
  end
end

you simply implement the perform method, and when you want to invoke it, you will call:

MySuperComplexLogicJob.perform_later

And if you think the API looks really similar to Sidekiq, it is not by accident 😛

But the API is extended, you can also set options before calling the “perform_later” method, for example:

MySuperComplexLogicJob.set(queue: 'other').perform_later
MySuperComplexLogicJob.set(wait: 5.minutes).perform_later(some,arguments)
MySuperComplexLogicJob.set(wait_until: Time.now.tomorrow).perform_later

With this, you can delay the execution, set the time when it will run or even select the exact queue where the job will be executed, assumming the backend supports different queues.

After this, you just need to select wich background job implementation you’ll really use in production, and ActiveJob out of the box supports these backends:

  • Backburner
  • Delayed Job
  • Qu
  • Que
  • queue_classic
  • Resque
  • Sidekiq
  • Sneakers
  • Sucker Punch

I’m probably going with sidekiq, mostly because I’m already used to it, but if you do not like any of these, you can always write an adapter for your favorite backend.

If you have any questions about how to use ActiveJob or why to use it, please leave a comment bellow, or contact me by email (sobrecodigo@urubatan.com.br).

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme
Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy