Skip to content

Commit

Permalink
turns into a demon
Browse files Browse the repository at this point in the history
  • Loading branch information
radamus committed Mar 3, 2014
1 parent 54628af commit d582731
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 14 deletions.
14 changes: 0 additions & 14 deletions app.js

This file was deleted.

79 changes: 79 additions & 0 deletions bin/node-awslab-http-daemon
Original file line number Diff line number Diff line change
@@ -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);
23 changes: 23 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit d582731

Please sign in to comment.