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/run.js b/run.js new file mode 100644 index 0000000..b489db8 --- /dev/null +++ b/run.js @@ -0,0 +1,13 @@ +const cluster = require('express-cluster'); +const app = require('./server.js'); + +const 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 dcea871..912edb4 100644 --- a/server.js +++ b/server.js @@ -20,9 +20,8 @@ function composeImage(width, height, seed) { return canvas; }; -const app = express(); - const cacheTimeout = 60 * 60 * 24 * 30; +const app = express(); app.get('/healthcheck', (req, res) => { res.status(200).end(); @@ -40,6 +39,4 @@ app.get('/:width/:seed', (req, res) => { res.end(buffer, 'binary'); }); -app.listen(process.env.PORT || 3000, () => { - console.log('listening on http://localhost:3000'); -}); +module.exports = app;