From d582731f204b77ffad4ac427229d0544a531f8e9 Mon Sep 17 00:00:00 2001 From: Radek Adamus Date: Mon, 3 Mar 2014 11:56:12 +0100 Subject: [PATCH] turns into a demon --- app.js | 14 ------ bin/node-awslab-http-daemon | 79 ++++++++++++++++++++++++++++++++ lib/app.js | 23 ++++++++++ example_1.js => lib/example_1.js | 0 helpers.js => lib/helpers.js | 0 index.html => lib/index.html | 0 lab1_1.js => lib/lab1_1.js | 0 service.js => lib/service.js | 0 package.json | 3 ++ 9 files changed, 105 insertions(+), 14 deletions(-) delete mode 100644 app.js create mode 100644 bin/node-awslab-http-daemon create mode 100644 lib/app.js rename example_1.js => lib/example_1.js (100%) rename helpers.js => lib/helpers.js (100%) rename index.html => lib/index.html (100%) rename lab1_1.js => lib/lab1_1.js (100%) rename service.js => lib/service.js (100%) diff --git a/app.js b/app.js deleted file mode 100644 index b64c946..0000000 --- a/app.js +++ /dev/null @@ -1,14 +0,0 @@ -var lab1_1 = require("./lab1_1").lab - -var example_1 = require("./example_1").lab - - -var urlMap = [ - {path: "/", action:__dirname + "/index.html"}, - {path: "/digest", action: lab1_1}, - {path: "/example_1", action: example_1}, - ]; - -var service = require("./service").http(urlMap); - -service(8080); \ No newline at end of file diff --git a/bin/node-awslab-http-daemon b/bin/node-awslab-http-daemon new file mode 100644 index 0000000..222294b --- /dev/null +++ b/bin/node-awslab-http-daemon @@ -0,0 +1,79 @@ +#!/usr/bin/env node + +/** + * bin/node-awslab-http-daemon + */ + + require('daemon')(); + + var cluster = require('cluster'); + +// Number of CPUs +var numCPUs = require('os').cpus().length; + +/** + * Creates a new worker when running as cluster master. + * Runs the HTTP server otherwise. + */ +function createWorker() { + if (cluster.isMaster) { + // Fork a worker if running as cluster master + var child = cluster.fork(); + + // Respawn the child process after exit + // (ex. in case of an uncaught exception) + child.on('exit', function (code, signal) { + createWorker(); + }); + } else { + // Run the HTTP server if running as worker + require('../lib/app'); + } +} + +/** + * Creates the specified number of workers. + * @param {Number} n Number of workers to create. + */ +function createWorkers(n) { + while (n-- > 0) { + createWorker(); + } +} + +/** + * Kills all workers with the given signal. + * Also removes all event listeners from workers before sending the signal + * to prevent respawning. + * @param {Number} signal + */ +function killAllWorkers(signal) { + var uniqueID, + worker; + + for (uniqueID in cluster.workers) { + if (cluster.workers.hasOwnProperty(uniqueID)) { + worker = cluster.workers[uniqueID]; + worker.removeAllListeners(); + worker.process.kill(signal); + } + } +} + +/** + * Restarts the workers. + */ +process.on('SIGHUP', function () { + killAllWorkers('SIGTERM'); + createWorkers(numCPUs * 2); +}); + +/** + * Gracefully Shuts down the workers. + */ +process.on('SIGTERM', function () { + killAllWorkers('SIGTERM'); +}); + +// Create two children for each CPU +createWorkers(numCPUs * 2); \ No newline at end of file diff --git a/lib/app.js b/lib/app.js new file mode 100644 index 0000000..2ead3dc --- /dev/null +++ b/lib/app.js @@ -0,0 +1,23 @@ +var lab1_1 = require("./lab1_1").lab +var example_1 = require("./example_1").lab; + +var PORT = 8080; + + +var urlMap = [ + {path: "/", action:__dirname + "/index.html"}, + {path: "/digest", action: lab1_1}, + {path: "/example_1", action: example_1}, + ]; + +var service = require("./service").http(urlMap); + +service(PORT); + +process.on('SIGTERM', function () { + if (server === undefined) return; + server.close(function () { + // Disconnect from cluster master + process.disconnect && process.disconnect(); + }); +}); \ No newline at end of file diff --git a/example_1.js b/lib/example_1.js similarity index 100% rename from example_1.js rename to lib/example_1.js diff --git a/helpers.js b/lib/helpers.js similarity index 100% rename from helpers.js rename to lib/helpers.js diff --git a/index.html b/lib/index.html similarity index 100% rename from index.html rename to lib/index.html diff --git a/lab1_1.js b/lib/lab1_1.js similarity index 100% rename from lab1_1.js rename to lib/lab1_1.js diff --git a/service.js b/lib/service.js similarity index 100% rename from service.js rename to lib/service.js diff --git a/package.json b/package.json index 931c340..bedc271 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,9 @@ { "name": "aws_lab", "version": "0.0.1", + "scripts": { + "start": "node app.js" + }, "dependencies": { "express": "~3.4.8", "aws-sdk": "~2.0.0-rc9"