What's New in Rails 4.2

SF Rails | Nov 20, 2014 | Ryan Clark



Ninefold.com

RubyConf

Hawaii

Buster




Caution: 







Main points: 

ActiveJob

AdequateRecord

WebConsole

More...

http://abstrusegoose.com/432
(you_down_wit_OPC-yeah_you_know_me.jpg)

ActiveJob

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
Sidekiq Setup:

Place your worker classes in app/workers:
# app/workers/hard_worker.rb 
class HardWorker 
include Sidekiq::Worker def perform(name, count) puts 'Doing hard work' end end
In your controller action or model, call HardWorker.perform_async:
HardWorker.perform_async('bob', 5)
Finally, start sidekiq from the root directory of your Rails app.
bundle exec sidekiq


Resque Setup:

To define some work, make a job. Jobs need a work method:
class ImageConversionJob
  def work
    # convert some kind of image here
  end
end
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:
bin/resque work

Delayed_job Setup: 

Call .delay.method(params) on any object to process in the background:
# without delayed_job
@user.activate!(@device)
# with delayed_job
@user.delay.activate!(@device)
Or you can call #handle_asynchronously after the method declaration:
class Device
  def deliver
    # long running method
  end
  handle_asynchronously :deliver
end device = Device.new
device.deliver

ActiveJob Setup: 

Set the queue adapter for Active Job:
ActiveJob::Base.queue_adapter = :inline # default queue adapter
Declare a job like so:
class MyJob < ActiveJob::Base
  queue_as :my_jobs
  def perform(record)
    record.do_work
  end
end
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.
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_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      |
http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html
Addition features:

  • 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.
                                                        



AdequateRecord Pro™: 

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

Performance
This is huge news!
Finds per second haven't improved since 2.3 (possibly longer)


Limitations: 

Not every call can benefit from this caching. 
Right now the only forms that are supported look like this:

Post.find(id)
Post.find_by_name(name)
Post.find_by(name: name)

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.

WebConsole

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




Other goodies in Rails 4.2: 

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:



THANK YOU!


ryan@ninefold.com

@I_am_Ryo

github: IAMRYO

slides.com/iamryo

What's New in Rails 4.2

By iamryo

What's New in Rails 4.2

Just a few of the things I find interesting in the pending release of Rails 4.2

  • 1,236