-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
105 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters