In the post Use background jobs with single process, I have mentioned to the
ActiveJob at the end of post. In this post, I will introduce and explain more detailed about this new feature of Rails 4.2 as well as an overview of popular background job gems.
What is ActiveJob?
ActiveJob is new feature from Rails 4.2 (actually, it is an independent gem before merged into Rails main repository), it acts as a wrapper for background job gems like: Delayed Job, Sidekiq... See Active Job adapters for list of compatible gems.
So, what benefit of using ActiveJob? The main benefit is we can write code once and can use any of available background job gems with only a change to ActiveJob adapter config. Imagine how awesome this benefit is when you can switch around all background job gems to see what is good for you or when migrating from normal VPS to heroku and you need to change background job gem to
sucker_punch to save some pennies.
How it works?
- We still need to add our chosen background job gem
- Configure adapter for ActiveJob
- Write code using ActiveJob
- ActiveJob will use the adapter of background job gem to convert job to the background job gem way
- It works!
Detail of how to use it
What should we use in a dozen of background job gems?
- Pretty much easy to use
- Don't need any more, just use same stack with your app (ActiveRecord, Mongoid)
- It is not good in performance. The worst thing when using Delayed Job is it use main database (SQL/NoSQL) to store queues data which make our database slower and read/write to disk. Delayed Job has Redis (a NoSQL datastore in memory instead of disk) support now but it seem to be not stable (they even don't make it as default) and seem to be too late because its users enjoyed Resque or Sidekiq
- Mixing of sync and async code. Someone will consider it as pros, but I don't think this is good for complex source code
- Run on Redis, which is fast
- It has web dashboard to see workers statistic. It is simply awesome
- Harder to use when comparing with Delayed Job, especially with priorities and clock jobs
- Really damned fast
- Use process resource most effectively
- Harder to use when comparing with Delayed Job
- Need to write
thread safecode and use
thread safelibraries (important). This is hard for even experienced developers
With new ActiveJob, the most important advantage of Delayed Job was gone. When you don't have any special environment like Heroku:
- Use Resque the needs of jobs is not so heavy (like some thousand jobs per hour)
- Use Sidekiq for really heavy background job projects