What's New in Rails 4.2
SF Rails | Nov 20, 2014 | Ryan Clark
Raptor (Ruby) does 60,000 requests/second? If this is exciting in Ruby land then expectations of what a computer can do is really low.— Kelly Sommers (@kellabyte) November 11, 2014
Built in support for moving work out of the request/response cycle
Built in adapters for multiple queuing backends (Sidekiq, Resque, Delayed-job, etc...)
Built in support for scheduled jobs
Place your worker classes in app/workers:
include Sidekiq::Worker def perform(name, count) puts 'Doing hard work' end end
In your controller action or model, call HardWorker.perform_async:
Finally, start sidekiq from the root directory of your Rails app.
bundle exec sidekiq
To define some work, make a job. Jobs need a work method:
# convert some kind of image here
Next, we need to procrastinate! Let's put your job on the queue:
resque = Resque.new
resque << ImageConversionJob.new
Neat! This unit of work will be stored in Redis. We can spin up a worker to grab some work off of the queue and do the work:
Call .delay.method(params) on any object to process in the background:
# without delayed_job
# with delayed_job
Or you can call #handle_asynchronously after the method declaration:
# long running method
end device = Device.new
Set the queue adapter for Active Job:
ActiveJob::Base.queue_adapter = :inline # default queue adapter
Declare a job like so:
class MyJob < ActiveJob::Base
Enqueue a job like so:
MyJob.perform_later record # Enqueue job to be performed when queueing system is free.
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record) # Enqueue a job to be performed tomorrow at noon.
MyJob.set(wait: 1.week).perform_later(record) # Enqueue a job to be performed 1 week from now.
| | 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_classi| Yes | Yes | No* | 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 | No | No | No | No |
| Active Job I| No | Yes | N/A | N/A | N/A | N/A |
| Active Job | Yes | Yes | Yes | No | No | No |
- Action Mailer #deliver_later method to deliver mail asynchronously
Default :inline job "queue" that doesn't require any additional configuration
- New GlobalID library makes it easy to pass Active Record objects to jobs by serializing them in a generic form.
- Provides a default :inline work queue that just kicks off the work to be done when a job is scheduled and doesn't require any additional configuration.
Like ActiveRecord, but more adequate
Built by @Tenderlove, so you know it's good.
TL;DR: AdequateRecord is a set of patches that
adds cache stuff to make ActiveRecord 2x faster
AdaquateRecord works like so:
Cache the non-dynamic portion of the query and pass in the rest as arguments
This is huge news!
Finds per second haven't improved since 2.3 (possibly longer)
Not every call can benefit from this caching.
Right now the only forms that are supported look like this:
This is because calculating a cache key for these calls is extremely easy. We know these statements don’t do any joins, have any “OR” clauses, etc.
All three of these statements indicate the table to query, the columns to select, and the where clauses right in the Ruby code.
Included in Rails 4.2
Allows you to debug in the browser by visiting /console
Automatically loaded in error pages
Allows team members to debug on hosted projects
Migration generators for adding/removing foreign keys
A ton of bug fixes and minor improvements in ActiveRecord
custom configuration for just about anything via settings and .yml files
Lots, lots more - check out the pre-release notes:
What's New in Rails 4.2