43
Marko Heijnen CODEKITCHEN Node.js to the rescue Let Node.js do things when WordPress/PHP isn’t enough

Node.js to the rescue

Embed Size (px)

Citation preview

Page 1: Node.js to the rescue

Marko Heijnen CODEKITCHEN

Node.js to the rescue

Let Node.js do things when WordPress/PHP isn’t enough

Page 2: Node.js to the rescue

Marko Heijnen• Founder of CodeKitchen

• Lead developer of GlotPress

• Core contributor for WordPress

• Plugin developer

• Organizer for WordCamp Belgrade 2015

• Technologist

Page 3: Node.js to the rescue

Why something else?

Page 4: Node.js to the rescue

Yes, you can build almost everything in WordPress

Page 5: Node.js to the rescue

But is that the smart thing to do?

Page 6: Node.js to the rescue

Can you trust on WordPress to be always stable?

Page 7: Node.js to the rescue

This summer I started moving things away from WordPress

Page 8: Node.js to the rescue

WP Central

Page 9: Node.js to the rescue

WP Central• Showing download history

• Showing version usage history

• List all locales and their current state (need update)

• Showing contributors data (currently API only)

• Collects history of locale progress

• Getting checksums for plugins & themes

Page 10: Node.js to the rescue

How it works• A lot of data handling by wp_remote_get

• Scrapping profiles.WordPress.org to read data

• Multiple API calls to api.WordPress.org

• Combine data so it can be presented

Page 11: Node.js to the rescue

The problem• Most things happened through WP Cron

• Some things happens on the front end

• Resulting a load on the webserver that could and should be prevented

Page 12: Node.js to the rescue

New server setup

Page 13: Node.js to the rescue

Loadbalancer

Memcached

Elasticsearch

MariaDB

New server setup

Micro services

Webserver 1

Webserver 2

Thumbor

Public Private

Page 14: Node.js to the rescue

Service server• MariaDB as database

• Memcached as object cache

• Moving to Redis when the PHP7 version is out

• Elasticsearch to make search better/faster

Page 15: Node.js to the rescue

The microservices server• Handles all cronjobs for the network site

• Node.js services running for WP Central

• Like getting checksums for plugins/themes

• Soon merging other WP cronjob calls for getting all the stats

Page 16: Node.js to the rescue

Microservices

Page 17: Node.js to the rescue

Microservices• Microservices are small, autonomous services that

work together

• Small, and Focused on Doing One Thing Well

Page 18: Node.js to the rescue

Benefits• Different services can use different programming

languages

• High level separation

• If WordPress breaks, the services still keep running

• Ease of Deployment

• Scale services that require more resources

Page 19: Node.js to the rescue

Benefits• In general they have an (REST) API

• Reusable

• Other microservices could call the service to run a task

Page 20: Node.js to the rescue

Node.js

Page 21: Node.js to the rescue

What is Node.js• JavaScript platform

• Uses an event-driven, non-blocking I/O model

• Lightweight and efficient

• Ideal for real time application

• Lot’s of modules you can use

• Manage with NPM - https://www.npmjs.org

Page 22: Node.js to the rescue

Why to use it• Internal webserver

• No configuration needed outside it’s code base

• You could use nginx as a proxy but not needed

• You get what you see approach

Page 23: Node.js to the rescue

Who is using it• Netflix

Page 24: Node.js to the rescue

Need to know modules• Express / Restify -> Webserver

• Socket.io -> Real time

• Request -> Doing internet requests

• async -> async calls with callback

• mysql -> MySQL driver with Pool support

• node-cmd -> Command line

Page 25: Node.js to the rescue

Checksums for plugins/themes

Page 26: Node.js to the rescue

What is does• Request checksum of a certain version from a

plugin or theme

• Download the zip and unzips it.

• Reads it in memory and get the checksum per entry

• After everything is retrieved stores it in MySQL

Page 27: Node.js to the rescue

Modules used• Build in modules

FSCrypto

• NPM ModulesExpress MySQLRequestYauzl for unzipping

• And build a little queue class

Page 28: Node.js to the rescue

Calling the API• http://wpcentral.io/api/checksums/plugin/tabify-

edit-screen/0.8.3 (REST API)

• Calls nginx by IP (10.10.10.10) which handles as a fallback when the node.js application is down

• nginx calls then internally the node.js application like proxy_pass http://127.0.0.1:8080

Page 29: Node.js to the rescue

API calls• /plugin/:slug/:version

http://10.10.10.10/checksums/plugin/:slug/:version http://wpcentral.io/api/checksums/plugin/tabify-edit-screen/0.8.3

• /theme/:slug/:version http://10.10.10.10/checksums/theme/:slug/:version http://wpcentral.io/api/checksums/theme/twentyfourteen/1.2

Page 30: Node.js to the rescue

nginx ruleserror_page 404 @404;error_page 500 @500;error_page 502 @502;

location @404 { internal; add_header Content-Type application/json always; return 404 '{ "status": "Route Not Found" }'; }

return 500 '{ "status": "Service is down" }';return 502 '{ "status": "Service is down" }';

Page 31: Node.js to the rescue

Going over the code

Page 32: Node.js to the rescue

Basic setup// set variables for environment var express = require('express'), app = express(), mysql = require('mysql'), request = require('request'), fs = require('fs'), crypto = require('crypto'), yauzl = require("yauzl");

Page 33: Node.js to the rescue

MySQL connectionvar pool = mysql.createPool({ connectionLimit : 10, host : ’10.10.10.11’, user : 'checksums', password : 'checksums', database : 'checksums' });

pool.on('enqueue', function () { log_error('Waiting for available connection slot'); });

Page 34: Node.js to the rescue

Serverapp.listen(4000);

app.get( '/plugin/:slug/:version', function(req, res) { if ( ! queue.add( 'plugin', req.params.slug, req.params.version, res ) ) {

res.json({ 'success': false, 'error': 'Generating checksums’

}); }

});

Page 35: Node.js to the rescue

Server 404app.use(function(req, res, next) {

res.status(404).json({ 'success': false, 'error': "Route doesn;'t exist”

}); });

Page 36: Node.js to the rescue

Lets check the rest

Page 37: Node.js to the rescue

Starting the server• The default way is: node server.js

• The production server way could be: pm2 start server.js -u www-data --name “Cool service”

Page 38: Node.js to the rescue

The new situation

Page 39: Node.js to the rescue

The new situation• No more unneeded logic in WordPress

• WordPress simple pipes the calls

• Small services that replacing it

• APIs can easily be reused

• Pushing new updates becomes easier

• Currently no caching but easily added

Page 40: Node.js to the rescue

Other things you could use node.js for

Page 41: Node.js to the rescue

See my presentation: Extending WordPress as a pro

Describing an idea of using Socket.io with WordPress

Page 42: Node.js to the rescue

Other ideas• Scheduling tasks or url

calls

• Build a central cache point for external sources like getting tweets

• Real time support

• git2svn sync

• Backup service

• Real time logger

• Perform heavy tasks

Page 43: Node.js to the rescue

Thank you for listening

Questions?@markoheijnen

markoheijnen.com

codekitchen.eu