Asynchronous WordPress

Post on 15-Apr-2017

450 views 0 download

Transcript of Asynchronous WordPress

Aaron Brazell • @technosailor • www.technosailor.com

A S Y N C H R O N O U S W O R D P R E S S

O F F L O A D I N G H O O K E D E V E N T S T O I N C R E A S E PA G E S P E E D

Aaron Brazell • @technosailor • www.technosailor.com

T H E P R O B L E M( D R A M AT I C T H E M E M U S I C )

Aaron Brazell • @technosailor • www.technosailor.com

• Every Time WordPress loads a page, events are fired.

• Plugins typically hook events into init, admin_init, plugins_loaded, or save_post to fire events

• Every event takes time to complete

• Many dozens of events, all waiting to finish running, add overhead to WordPress.

Aaron Brazell • @technosailor • www.technosailor.com

P O S S I B L E S C E N A R I O S( S I D E WAY S G L A N C E )

Aaron Brazell • @technosailor • www.technosailor.com

E X T E R N A L A P I R E Q U E S T

• Every page load, a site is expected to send a request to an external API to get the most up to date information.

• The natural place to make this event happen is on the init hook.

• The third party API is extremely slow or maybe down and WordPress must wait up to 30 seconds for the failure to become known before moving on to the next event.

Aaron Brazell • @technosailor • www.technosailor.com

S AV I N G A P O S T

• Saving posts is potentially one of the most expensive hooks in WordPress

• There are different kinds of saves: Autosaves. Ajax events. Non-content saves (Think attachments)

• Actions fire in chronological order. There is no limit to the number of events that can fire on a hook.

• Virtually any plugin action requiring a “state” update - meta, API requests, etc - fire on save_post.

- S O M E O N E W H O B A D LY M A N G L E D T H E Q U O T E

“Asynchronous Events are not the heroes we want; they are the heroes we need.”

Photo Credit: V Threepio, via Flickr, Creative Commons

T E C H C R U N C H A S Y N C L I B R A R Y

• TechCrunch had extremely long process times, particularly with their CrunchBase API.

• API calls were required to repopulate cached results.

• Eric Mann (@ericmann) and John Bloch (@johnpbloch) built a library to offload tasks to asynchronous equivalents

• Runs on `shutdown` hook

Photo Credit: Charlyn Wee, via Flickr Creative Commons

Aaron Brazell • @technosailor • www.technosailor.com

S T E P S T O I M P L E M E N T

• Install the WP_Async_Task class as a self-contained plugin or bundle with plugin or theme as separate class. It will only ever be included one time regardless.

• Create a subclass that extends WP_Async_Task for each hook that needs to be fired asynchronously.

• Each subclass must contain two methods and a protected variable $action - prepare_data() and run_action(). The $action variable must be the name of the hook you’re providing an alternative for. e.g protected $action = ‘save_post’;

• Hook asynchronous event into wp_async_{action} (e.g. wp_async_save_post) and pass identical parameters.

Aaron Brazell • @technosailor • www.technosailor.com

E X T E N D I N G T H E P R E PA R E _ D ATA ( ) M E T H O D

Aaron Brazell • @technosailor • www.technosailor.com

E X T E N D I N G T H E P R E PA R E _ D ATA ( ) M E T H O D• Method runs first. Receives a numerical array passed via func_get_args(). PROTIP: For insight, print_r() this array to your error log.

• Modify, sanitize and assign scalar values to a return array that includes the arguments expected for the hook you’re implementing. (e.g. save_post takes $post_id and $post, so this method should set these up). Returns an associative array with the expected parameters.

• For example, on save_post, return array( ‘post_id’ => $post_id, ‘post’ => $post_object );

Aaron Brazell • @technosailor • www.technosailor.com

E X T E N D I N G T H E R U N _ A C T I O N ( ) M E T H O D

Aaron Brazell • @technosailor • www.technosailor.com

E X T E N D I N G T H E R U N _ A C T I O N ( ) M E T H O D

• Think of this method as the method that actually runs the processes the formulate the alternative hook.

• Receives the associative array created by the prepare_data() method as a global $_POST array.

• Be sure to include wp_async_$this->action as: do_action( ‘wp_async_’ . $this->action, $variable1, $variable 2); to match the definition of the core WordPress hook.

Aaron Brazell • @technosailor • www.technosailor.com

D E M O N S T R AT I O N

Aaron Brazell • @technosailor • www.technosailor.com

R E F E R E N C E S

• TechCrunch Open Sources its WordPress Async Task Library: http://techcrunch.com/2014/07/31/wp-async-task-our-new-open-source-library/

• Asynchronous WordPress, by 10up: http://techcrunch.com/2014/07/31/wp-async-task-our-new-open-source-library/

• Github: https://github.com/techcrunch/wp-async-task/

• Demo Plugin: https://github.com/technosailor/wcbalt-async-wp

• This Presentation: http://www.slideshare.net/technosailor/asynchronous-wordpress

Aaron Brazell • @technosailor • www.technosailor.com

A A R O N B R A Z E L L

• Sr. Web Engineer, 10up

• We’re Hiring Project Managers, WordPress/PHP Engineers and Front End UX Engineers (http://is10uphiring.com/)

• Twitter: http://technosailor.com/twitter

• Email: aaron.brazell@10up.com

• Github: https://github.com/technosailor

• WP.org Profile: https://profiles.wordpress.org/technosailor

Aaron Brazell • @technosailor • www.technosailor.com