a day of rest

50
REST in Action: The Live Coverage Platform at the New York Times A Day of REST London, UK - January 28, 2016

Upload: scott-taylor

Post on 14-Feb-2017

2.385 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: A Day of REST

REST in Action: The Live Coverage Platform at the New

York TimesA Day of REST

London, UK - January 28, 2016

Page 2: A Day of REST

Scott Taylor

• Core Developer, WordPressRelease Lead for 4.4

• Sr. Software Engineer, The New York Times

• I love my Wife, Music, NYC, and Mexican food

Page 3: A Day of REST

My Beautiful Wife

• Warrior for Animal Rights at @NYCLASS

• @alliefeldman212 on Twitter

• Mother to our cat…

Page 4: A Day of REST

Willa

Page 5: A Day of REST

WordPress 4.4 “Clifford”• REST API (Phase 1)

• Term Meta

• Responsive Images

• WordPress as oEmbed Provider

• Tons of under the hood stuff (2000+ commits, 400+ contributors)

Page 6: A Day of REST

2013 - Present Me at the New York Times

Page 7: A Day of REST

WordPress at the NYT now• The Live Coverage platform

• some forthcoming International projects

• some “legacy” blogs

• Lens - photography blog

• First Draft

• NYT Co.

• Women of the World

• Times Journeys

• some internal corporate sites

Page 8: A Day of REST

“Blogs Team” at The NYT• NYT used WordPress very early

• NYT was an early investor in Automattic

• Multisite: ~80 blogs at the height of blog mania - (the 00s were the glory days)

• Many blogs used “Live Blogging”

Page 9: A Day of REST

When I arrived: Legacy Blogs Codebase

• Separate from the rest of the NYT’s PHP codebase

• Global NYTimes CSS and JS, CSS for all Blogs, custom CSS per-blog

• A universe that assumed jQuery AND Prototype were loaded on every page in global scope

• Challenging amounts of what could generously be called “technical debt”

Page 10: A Day of REST

At the NYT• No WordPress Comments: There is an entire team that deals

with these for the site globally, in a different system called CRNR

• No Media: There is another CMS at the Times, Scoop, which stores the images, videos, slideshows, etc

• WordPress native post-locking: This only landed in WordPress core in version 3.6 (we have yet to reconcile the differences)

• There is layer for Bylines which is separate from Users: Our users are employees authenticated via LDAP, most post authors don’t actually enter the content themselves

Page 11: A Day of REST

NYT5: The New Frontier

My arrival at the New York Times coincided with the NYT5 project, already in progress

Page 12: A Day of REST
Page 13: A Day of REST
Page 14: A Day of REST
Page 15: A Day of REST

NYT5 Dealbreakers• We can’t just point at WordPress on every request

and have our code figure out routing. Routing happens in Apache in NYT5 - most requests get piped to app.php

• Because PHP Namespaces are used, WP has to load early and outside of them (global scope)

• On the frontend, WP cannot exit prematurely before hitting the framework, which returns the response to the server via Symfony\HttpFoundation

Page 16: A Day of REST

ApacheNYT5: app.php

Route to NYT5 Blogs app

- Load initial files - Bootstrap WP - Capture WP content - WP complete - Initialize app

Page 17: A Day of REST

Blogs in Transition

Page 18: A Day of REST

Overall: Bad News for Blogs• Blogs were duplicating Section Fronts, Columns: Mark Bittman has column in the paper.The column also exists on the web as an article. He contributes to the Diner’s Journal blog. There is a section front for dining. He also has his own NYTimes blog. Why?

• Blogs and WordPress were combined in everyone’s mind. So whenever WordPress was mentioned as a solution for anything, the response was: aren’t blogs going away? #dark

Page 19: A Day of REST

But we still had…

First Draft Lens

Live Coverage

Page 20: A Day of REST
Page 21: A Day of REST
Page 22: A Day of REST

2014 Midterm Elections required new Live Blogging

tools

Page 23: A Day of REST

2008: Live Blogs at the Times• A Blog would create a post and check “Start Live

Blogging”

• the updates related to the post were stored in custom tables in the database

• the APIs for interacting with these tables duplicated tons of WordPress functionality

• Custom Post Types didn’t exist until WordPress 3.0 (June 2010) - the NYT code was never rewritten to leverage them (would have required porting the content as well)

Page 24: A Day of REST

Live (actual) Blogs: Dashboards/Dashblogs

• A Live Blog would be its own blog in the network, its own set of tables

• A special dashboard theme that had hooks to add custom JS/CSS for each individual blog, without baking them into the theme

• Making an entirely new site in the network for a 4-hour event is overkill

• For every 10 or so new blogs that are added, you are adding 100 new database tables - gross!

Page 25: A Day of REST

What if…• Instead of custom tables and

dupe’d API code, new object types: events and updates!

• To create a new “Live Blog”: create an event, then go to a Backbone-powered screen to add updates

• If WP isn’t desired for the front end, it could be the backend for anything that wants a JSON feed for live event data

• Using custom post types, building a Live Event UI that looks like the NYT5 theme would be nominal

Page 26: A Day of REST
Page 27: A Day of REST

• Built an admin interface with Backbone to quickly produce content - which in turn could be read from JSON feeds

• When saving, the updates post into a service we have called Invisible City (wraps Redis/Pusher)

• Our first real foray into using the REST API

• Our plan was just to be an admin to produce data via self-service URLs

What we did

Page 28: A Day of REST

Live Events, the new Live Blogs: Complete Rewrite of 2008 code

• nytimes.com/live/{event} and nytimes.com/live/{event}/{update}

• Brand new admin interface: Backbone app that uses the REST API. Constantly updated filterable stream - Backbone collections that re-fetch on Heartbeat tick

• Custom REST endpoints that handle processes that need to happen on save

• Front end served by WordPress for SEO, but data is received by web socket from Invisible City and rendered via React

Page 29: A Day of REST
Page 30: A Day of REST
Page 31: A Day of REST

Responsive on Mobile

Page 32: A Day of REST

An “Interactive Promo” on an article page

Page 33: A Day of REST

2015: “Blogs” team becomes “WordPress”

team, joins the Interactive News team

Page 34: A Day of REST

• Would rather use Docker instead of Vagrant

• PSR-0 is now PSR-4

• Grunt is now eschewed in favor of Gulp

• RequireJS is ok, but I’d rather use Browserify

• PHP is cool, but why don’t we use Node and React?

Page 35: A Day of REST

What is a Live Coverage platform?

Page 36: A Day of REST

nytimes.com/live/{event}Request is served by WordPress,

PHP generates markup

React wraps the "posts" area

JS listens to Web Socket

Updates are added on the backend (OR via SLACK!)

React updates the content

Page 37: A Day of REST

the elections code became useful for

Breaking News

Page 38: A Day of REST

WordPress + REST

Page 39: A Day of REST

Register a route:

register_rest_route( ‘nyt/v1’, ‘/live-events/(?P<post_id>\d+)’, [ ‘callback’ => [ $this, ‘liveEventRoute’ ], ‘methods’ => \WP_Rest_Server::READABLE ] );

Page 40: A Day of REST

Handle the route:public function liveEventRoute( $request ) { $post_id = $request['post_id'];

$response = new \WP_REST_Response( [ 'results' => . . ., ] );

return $response; }

Page 41: A Day of REST

Some REST API gotchas…

Page 42: A Day of REST

Most plugins only handle POST

• WP-API and Backbone speak REST

• REST will send you requests via GET, PUT, DELETE, POST and friends

Page 43: A Day of REST

$hook = add_menu_page( ... );add_action( "load-$hook", 'callback' );

function old_custom_load() { if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) { return; } ...

}

function new_custom_load() { if ( 'GET' === $_SERVER['REQUEST_METHOD'] ) { return; } ...

}

Page 44: A Day of REST

WordPress becomes a web service

• Monolithic mindset needs to transition into how to make it into a bare metal service provider

• The serving of requests should be loosely coupled from objects like WP_Query

• WordPress needs to become supportive of concurrency

Page 45: A Day of REST

Custom REST Endpoints for GET

• We do not hit these endpoints on the front-end

• We have a storage mount that is fronted via Varnish and Akamai

• JSON feeds can show up on the homepage of the NYT to dynamically render “promos” - these have to massively scale

Page 46: A Day of REST

HTTP is time-consuming• It is easy to lose track of how many things are

happening on the 'save_post' hook

• Admin needs to be fast

• The front end is typically cached, but page generation shouldn’t be bogged down by HTTP requests

• Anything which is time-consuming should be offloaded to a separate “process” or request who response you don’t need to handle

Page 47: A Day of REST

wp_remote_post( $url, wp_parse_args( ['timeout' => 0.01,'blocking' => false

], $args ) );

Fire and Forget*

* Stolen from Mark Jaquith’s nginx cache invalidation technique:

wp_remote_get( $url, [ 'timeout' => 0.01, 'blocking' => false, 'headers' => [ 'X-Nginx-Cache-Purge' => '1' ]

] );

Page 48: A Day of REST

Custom REST Endpoints for POST

• Use fire-and-forget technique on 'save_post', instead of waiting for responses inline. You can still log/handle/re-try responses in the separate request.

• Most things that happen on 'save_post' only need to know $post_id for context, the endpoint handler can call get_post() from there

Page 49: A Day of REST

Trigger the process:

NYT\Admin\REST::async_request( '/varnish-invalidation', [ 'urls' => $urls

] );

Page 50: A Day of REST

Questions?