Skip to content
This repository has been archived by the owner on Feb 23, 2021. It is now read-only.

Commit

Permalink
Introduced bin/angel
Browse files Browse the repository at this point in the history
  • Loading branch information
mash committed Apr 24, 2014
1 parent 3d88bf7 commit ae6dc71
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 13 deletions.
112 changes: 112 additions & 0 deletions bin/angel
@@ -0,0 +1,112 @@
#!/usr/bin/env node

var angel = require("../")
, path = require("path")
, argv = require("minimist")(process.argv.slice(2), {
"boolean": [ "h", "t" ]
})
, here = require("here").here
;

var usage = here(/*
\\\\\\ angel ///
Usage:
angel [options] app.js
Options:
--port port
-p port
Port number to listen on.
Either port or path is required.
--path path
Path of unix domain socket to listen on.
Either port or path is required.
--host host
Optional.
Host to listen on.
--workers number-of-workers
Optional. Defaults to number of CPUs.
Number of worker processes to fork.
--pidfile filename
Optional. Defaults to "angel.pid".
Filename for a file containing angel's master process pid.
--interval seconds
Optional. Defaults to 1 second.
Interval from when master was HUPed til old workers close.
--max-requests-per-child number-of-requests
Optional. Defaults to 0.
Worker processes max-requests-per-child number of requests and gracefully dies while master forks new worker.
0 means don't die = process requests forever.
--refresh-modules-regexp regexp
Optional. Defaults to none.
If set, master will reload node modules matching this regexp when received HUP signal.
Use this to refresh code without downtime.
When require-ing matching node modules fails, master won't restart processes.
--test
-t
Test options.
Exit code will be non 0 if something's wrong.
--help
-h
This.
app.js
Your net.Server application.
Export net.Server instance, without listening.
angel does that for you.
var server = require("http").createServer( function(req, res) {
res.writeHead(200);
res.end("Hello, World!");
});
module.exports = server;
*/).valueOf();

var options = {}, app;
var argv_to_options = {
"p" : "port", // shortcut
"port" : "port",
"path" : "path",
"host" : "host",
"workers" : "workers",
"pidfile" : "pidfile",
"interval" : "interval",
"max-requests-per-child" : "max_requests_per_child",
"refresh-modules-regexp" : "refresh_modules_regexp"
};
Object.keys(argv_to_options).forEach( function(key) {
if (typeof(argv[key]) !== "undefined") {
options[ argv_to_options[key] ] = argv[ key ];
}
});
app = argv._[ 0 ];

if (argv.t) {
console.log("Options: %j",options);
console.log("App: %j",app);
angel.validate(options, app, function (ok, messages) { // sync
console.log(messages.join("\n"));
if (ok) {
console.log("Arguments looks fine");
process.exit(0);
}
});
process.exit(1);
}
if (argv.h || (argv._.length === 0)) {
console.log(usage);
process.exit(1);
}

angel( require(path.resolve(app)), options );
2 changes: 1 addition & 1 deletion eg/server.js
@@ -1,4 +1,4 @@
var angel = require('../angel')
var angel = require('..')
, app = require('./app');

// run
Expand Down
2 changes: 1 addition & 1 deletion eg/server.unix.js
@@ -1,4 +1,4 @@
var angel = require('../angel')
var angel = require('..')
, app = require('./app')
, fs = require('fs')
, oldmask
Expand Down
23 changes: 23 additions & 0 deletions angel.js → index.js
@@ -1,4 +1,5 @@
var fs = require("fs")
, path = require("path")
, cluster = require("cluster")
, numCPUs = require("os").cpus().length
, isNode06 = process.version.match( /v0\.6/ )
Expand Down Expand Up @@ -228,6 +229,27 @@ function startServer (server, options) {
}
}

function validate (options, app, callback) {
var ok = true, messages = [];
if ( (typeof(options.port) === "undefined") &&
(typeof(options.path) === "undefined") ) {
messages.push("Provide port or path");
ok = false;
}
else if ( (typeof(options.port) !== "undefined") &&
(typeof(options.port) !== "number") ) {
messages.push("Port is invalid: "+options.port);
ok = false;
}
try {
require( path.resolve(app) );
} catch (e) {
messages.push("Invalid app.js:" , e);
ok = false;
}
callback( ok, messages );
}

function angel (server, options_) {
if ( (typeof options_.port === "undefined") &&
(typeof options_.path === "undefined") ) {
Expand All @@ -249,5 +271,6 @@ function angel (server, options_) {

startServer( server, options );
}
angel.validate = validate;

module.exports = angel;
20 changes: 13 additions & 7 deletions package.json
Expand Up @@ -8,26 +8,32 @@
"fork",
"max_requests_per_child"
],
"version": "0.2.1",
"version": "0.3.0",
"repository": {
"type": "git",
"url": "git://github.com/mash/node-angel.git"
},
"author": "Masakazu Ohtsuka <o.masakazu@gmail.com> (http://maaash.jp/)",
"contributors": [],
"main": "./angel.js",
"main": "./index.js",
"bin": {
"angel": "bin/angel"
},
"scripts": {
"test": "make test"
},
"engines": {
"node": "*"
},
"dependencies": {},
"dependencies": {
"minimist" : ">= 0.0",
"here" : ">= 0.0"
},
"devDependencies": {
"async": "*",
"grunt": "~0.4.1",
"grunt-release": "~0.3.5",
"grunt-contrib-clean": "~0.4.1"
"async" : "*",
"grunt" : "~0.4.1",
"grunt-release" : "~0.3.5",
"grunt-contrib-clean" : "~0.4.1"
},
"readmeFilename": "README.markdown",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion test/01_restart_app.js
Expand Up @@ -2,7 +2,7 @@ var fs = require('fs'),
assert = require('assert'),
http = require('http'),
cluster = require('cluster'),
angel = require('../angel'),
angel = require('..'),
app = require('./_app.js');

// this will be our test app's response body
Expand Down
2 changes: 1 addition & 1 deletion test/02_accidental_death.js
Expand Up @@ -2,7 +2,7 @@ var fs = require('fs'),
assert = require('assert'),
http = require('http'),
cluster = require('cluster'),
angel = require('../angel'),
angel = require('..'),
app = require('./_suicide_app.js');

// this will be our test app's response body
Expand Down
4 changes: 2 additions & 2 deletions test/03_max_requests_per_child.js
Expand Up @@ -3,7 +3,7 @@ assert = require('assert'),
http = require('http'),
cluster = require('cluster'),
async = require('async'),
angel = require('../angel'),
angel = require('..'),
app = require('./_app.js');

// this will be our test app's response body
Expand Down Expand Up @@ -42,7 +42,7 @@ function runTest (port) {
});
}
async.series( jobs, function (err, results) {
assert( err === null, 'no errors' );
assert( ! err, 'no errors' );

var worker_pids = {};
results.forEach( function( result ) {
Expand Down

0 comments on commit ae6dc71

Please sign in to comment.