From 7c34463cc4083932bd52d2ae8cbcba866bd2e12a Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Tue, 2 May 2017 14:05:02 -0700 Subject: [PATCH 1/4] use express-cluster for drop-in cluster support --- package.json | 1 + server.js | 42 +++++++++++++++++++++++------------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index bf00d49..3fed837 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "canvas": "^1.6.2", "chance": "^1.0.4", "express": "^4.14.0", + "express-cluster": "0.0.4", "newrelic": "^1.35.1", "node-gyp": "^3.4.0", "sanitize-filename": "^1.6.1", diff --git a/server.js b/server.js index dcea871..89db84a 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ try { } const express = require('express'); +const cluster = require('express-cluster'); const uuid = require('uuid/v4'); const sanitize = require('sanitize-filename'); const Canvas = require('canvas'); @@ -20,26 +21,29 @@ function composeImage(width, height, seed) { return canvas; }; -const app = express(); - const cacheTimeout = 60 * 60 * 24 * 30; -app.get('/healthcheck', (req, res) => { - res.status(200).end(); -}); +cluster((worker) => { + const app = express(); -app.get('/:width/:seed', (req, res) => { - let width = parseInt(req.params.width) || 400; - if (width > 400) width = 400; - const seed = sanitize(req.params.seed) || uuid(); - const canvas = composeImage(width, width, seed); - const buffer = canvas.toBuffer(); - res.set('Cache-Control', 'max-age=' + cacheTimeout); - res.set('Content-length', buffer.length); - res.type('png'); - res.end(buffer, 'binary'); -}); + app.get('/healthcheck', (req, res) => { + res.status(200).end(); + }); -app.listen(process.env.PORT || 3000, () => { - console.log('listening on http://localhost:3000'); -}); + app.get('/:width/:seed', (req, res) => { + let width = parseInt(req.params.width) || 400; + if (width > 400) width = 400; + const seed = sanitize(req.params.seed) || uuid(); + const canvas = composeImage(width, width, seed); + const buffer = canvas.toBuffer(); + res.set('Cache-Control', 'max-age=' + cacheTimeout); + res.set('Content-length', buffer.length); + res.type('png'); + res.end(buffer, 'binary'); + }); + + activePort = process.env.PORT || 3000; + app.listen(activePort, () => { + console.log('worker ' + worker.id + ' is listening on port ' + activePort); + }); +}) From f5712fef77a25de19d8abe8e2236238425bfe6f0 Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Tue, 2 May 2017 14:37:03 -0700 Subject: [PATCH 2/4] add config --- server.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server.js b/server.js index 89db84a..7db56fc 100644 --- a/server.js +++ b/server.js @@ -46,4 +46,7 @@ cluster((worker) => { app.listen(activePort, () => { console.log('worker ' + worker.id + ' is listening on port ' + activePort); }); +}, { + 'respawn': true, // workers will restart on failure + 'verbose': true, // logs what happens to console }) From a5b32e9d53e395c4bc6ecd25c5ebdd4d1fb35c55 Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Wed, 3 May 2017 16:06:47 -0700 Subject: [PATCH 3/4] factor our cluster into run.js --- run.js | 13 +++++++++++++ server.js | 42 ++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 26 deletions(-) create mode 100644 run.js diff --git a/run.js b/run.js new file mode 100644 index 0000000..52f9f22 --- /dev/null +++ b/run.js @@ -0,0 +1,13 @@ +const cluster = require('express-cluster'); +const app = require('./server.js'); + +activePort = process.env.PORT || 3000; + +cluster((worker) => { + app.listen(activePort, () => { + console.log('worker ' + worker.id + ' is listening on port ' + activePort); + }); +}, { + 'respawn': true, // workers will restart on failure + 'verbose': true, // logs what happens to console +}); diff --git a/server.js b/server.js index 7db56fc..912edb4 100644 --- a/server.js +++ b/server.js @@ -5,7 +5,6 @@ try { } const express = require('express'); -const cluster = require('express-cluster'); const uuid = require('uuid/v4'); const sanitize = require('sanitize-filename'); const Canvas = require('canvas'); @@ -22,31 +21,22 @@ function composeImage(width, height, seed) { }; const cacheTimeout = 60 * 60 * 24 * 30; +const app = express(); -cluster((worker) => { - const app = express(); +app.get('/healthcheck', (req, res) => { + res.status(200).end(); +}); - app.get('/healthcheck', (req, res) => { - res.status(200).end(); - }); +app.get('/:width/:seed', (req, res) => { + let width = parseInt(req.params.width) || 400; + if (width > 400) width = 400; + const seed = sanitize(req.params.seed) || uuid(); + const canvas = composeImage(width, width, seed); + const buffer = canvas.toBuffer(); + res.set('Cache-Control', 'max-age=' + cacheTimeout); + res.set('Content-length', buffer.length); + res.type('png'); + res.end(buffer, 'binary'); +}); - app.get('/:width/:seed', (req, res) => { - let width = parseInt(req.params.width) || 400; - if (width > 400) width = 400; - const seed = sanitize(req.params.seed) || uuid(); - const canvas = composeImage(width, width, seed); - const buffer = canvas.toBuffer(); - res.set('Cache-Control', 'max-age=' + cacheTimeout); - res.set('Content-length', buffer.length); - res.type('png'); - res.end(buffer, 'binary'); - }); - - activePort = process.env.PORT || 3000; - app.listen(activePort, () => { - console.log('worker ' + worker.id + ' is listening on port ' + activePort); - }); -}, { - 'respawn': true, // workers will restart on failure - 'verbose': true, // logs what happens to console -}) +module.exports = app; From c61bbadb22c64860653271a1f7292b9d9ab3d92d Mon Sep 17 00:00:00 2001 From: Jeffrey Sun Date: Wed, 3 May 2017 16:22:31 -0700 Subject: [PATCH 4/4] add const --- run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run.js b/run.js index 52f9f22..b489db8 100644 --- a/run.js +++ b/run.js @@ -1,7 +1,7 @@ const cluster = require('express-cluster'); const app = require('./server.js'); -activePort = process.env.PORT || 3000; +const activePort = process.env.PORT || 3000; cluster((worker) => { app.listen(activePort, () => {