Skip to content

Commit

Permalink
ability to pass command line arguments to node
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Oct 16, 2012
1 parent bfccbfa commit 6e57573
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ CLI:
--stderr stderr.log
--max-log-size 10485760
--cwd .
--node-args ''


naught stop [options] [ipc-file]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"rimraf": "~2.0.2"
},
"dependencies": {
"async": "~0.1.22",
"mkdirp": "~0.3.4"
"mkdirp": "~0.3.4",
"async": "git://github.com/superjoe30/async.git#515c7dfe26135a1961ae9d71c5c"
}
}
13 changes: 11 additions & 2 deletions src/daemon.co
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const
log_stdout_path = argv.shift()
max_log_size = parseInt(argv.shift())
script = argv.shift()
node_args_str = argv.shift()

logs = null
socket = null
Expand Down Expand Up @@ -81,11 +82,13 @@ function workerCountsFromMsg (msg)
cb()

!function spawnMaster
master := spawn(process.execPath, [
node_args = splitCmdLine(node_args_str)
console.error "node_args", node_args
master := spawn(process.execPath, node_args.concat([
path.join(__dirname, "master.js"),
worker_count,
script,
].concat(argv), {
]).concat(argv), {
env: process.env
stdio: [process.stdin, 'pipe', 'pipe', 'ipc']
cwd: process.cwd()
Expand All @@ -104,6 +107,12 @@ function workerCountsFromMsg (msg)
new_online: 0
server.close()

function splitCmdLine (str)
if str.length is 0
[]
else
str.split(/\s+/)

createLogsAndIpcServer !(err) ->
assert.ifError(err)
spawnMaster()
3 changes: 3 additions & 0 deletions src/main.co
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function getDaemonMessages (socket_path, cbs)
path.resolve(CWD, options.stdout),
options.'max-log-size',
path.resolve(CWD, script),
options.'node-args',
].concat(argv), {
env: process.env
stdio: [\ignore \ignore \ignore \ipc]
Expand Down Expand Up @@ -229,6 +230,7 @@ cmds =
--stderr stderr.log
--max-log-size 10485760
--cwd #CWD
--node-args ''
"""
fn: (argv) ->
options =
Expand All @@ -239,6 +241,7 @@ cmds =
'stderr': 'stderr.log'
'max-log-size': '10485760'
'cwd': CWD
'node-args': ''
[err, script] = chompArgv(options, argv)
if not err and script?
options.'worker-count' = parseInt(options.'worker-count')
Expand Down
20 changes: 20 additions & 0 deletions test/server5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var http;

http = require("http");

console.log("server5 attempting to listen");
http.createServer(function(req, resp) {
if (req.url === "/hi") {
resp.statusCode = 200;
resp.setHeader('content-type', 'text/plain');
let blah = "server5 says hi";
for (let blah = 0; blah < 3; blah++) {
resp.write(blah + "\n");
}
resp.write(blah + "\n");
resp.end();
}
}).listen(process.env.PORT, function() {
console.error("server5 listening");
process.send("online");
});
116 changes: 78 additions & 38 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,25 @@ function get(info, url, expected_resp) {
path: url,
}, function (res) {
var body;
assert.strictEqual(res.statusCode, 200);
assertEqual(res.statusCode, 200);
body = ""
res.on('data', function(data) {
body += data;
});
res.on('end', function() {
assert.strictEqual(body, expected_resp);
assertEqual(body, expected_resp);
cb();
});
}).end();
},
};
}

function assertEqual(actual, expected, msg) {
msg = msg || "";
assert(actual === expected, "actual:\n" + actual + "\nexpected:\n" + expected + "\n" + msg);
}

steps = [
use("server1.js"),
{
Expand All @@ -161,11 +166,11 @@ steps = [
PORT: port,
hi: "sup dawg",
}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"Bootup. booting: 1, online: 0, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 0, online: 1, dying: 0, new_booting: 0, new_online: 0\n");
assert.strictEqual(stdout, "workers online: 1\n")
assert.strictEqual(code, 0)
assertEqual(stdout, "workers online: 1\n")
assertEqual(code, 0)
cb();
});
},
Expand All @@ -174,9 +179,9 @@ steps = [
info: "starting a server twice prints the status of the running server",
fn: function (cb) {
naught_exec(["start", "server.js"], {}, function(stdout, stderr, code) {
assert.strictEqual(stderr, "");
assert.strictEqual(stdout, "workers online: 1\n");
assert.strictEqual(code, 1)
assertEqual(stderr, "");
assertEqual(stdout, "workers online: 1\n");
assertEqual(code, 1)
cb();
});
},
Expand All @@ -185,9 +190,9 @@ steps = [
info: "ability to query status of a running server",
fn: function (cb) {
naught_exec(["status"], {}, function(stdout, stderr, code) {
assert.strictEqual(stderr, "");
assert.strictEqual(stdout, "workers online: 1\n");
assert.strictEqual(code, 0)
assertEqual(stderr, "");
assertEqual(stdout, "workers online: 1\n");
assertEqual(code, 0)
cb();
});
},
Expand All @@ -198,14 +203,14 @@ steps = [
info: "ability to deploy to a running server",
fn: function (cb) {
naught_exec(["deploy"], {hi: "hola"}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"SpawnNew. booting: 0, online: 1, dying: 0, new_booting: 1, new_online: 0\n" +
"NewOnline. booting: 0, online: 1, dying: 0, new_booting: 0, new_online: 1\n" +
"ShutdownOld. booting: 0, online: 0, dying: 1, new_booting: 0, new_online: 1\n" +
"OldExit. booting: 0, online: 0, dying: 0, new_booting: 0, new_online: 1\n" +
"done\n");
assert.strictEqual(stdout, "");
assert.strictEqual(code, 0)
assertEqual(stdout, "");
assertEqual(code, 0)
cb();
});
},
Expand All @@ -215,11 +220,11 @@ steps = [
info: "ability to stop a running server",
fn: function (cb) {
naught_exec(["stop"], {}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"ShutdownOld. booting: 0, online: 0, dying: 1, new_booting: 0, new_online: 0\n" +
"OldExit. booting: 0, online: 0, dying: 0, new_booting: 0, new_online: 0\n");
assert.strictEqual(stdout, "");
assert.strictEqual(code, 0)
assertEqual(stdout, "");
assertEqual(code, 0)
cb();
});
},
Expand All @@ -228,9 +233,9 @@ steps = [
info: "stopping a server twice prints helpful output",
fn: function (cb) {
naught_exec(["stop"], {}, function(stdout, stderr, code) {
assert.strictEqual(stdout, "");
assert.strictEqual(stderr, "server not running\n");
assert.strictEqual(code, 1)
assertEqual(stdout, "");
assertEqual(stderr, "server not running\n");
assertEqual(code, 1)
cb();
});
},
Expand All @@ -239,7 +244,7 @@ steps = [
info: "redirect stdout to log file",
fn: function (cb) {
fs.readFile(path.join(test_root, "stdout.log"), "utf8", function (err, contents) {
assert.strictEqual(contents, "server1 attempting to listen\n" +
assertEqual(contents, "server1 attempting to listen\n" +
"server2 attempting to listen\n");
cb();
});
Expand All @@ -249,7 +254,7 @@ steps = [
info: "redirect stderr to log file",
fn: function (cb) {
fs.readFile(path.join(test_root, "stderr.log"), "utf8", function (err, contents) {
assert.strictEqual(contents, "server1 listening\n" +
assertEqual(contents, "server1 listening\n" +
"server2 listening\n");
cb();
});
Expand All @@ -259,7 +264,7 @@ steps = [
info: "naught log contains events",
fn: function (cb) {
fs.readFile(path.join(test_root, "naught.log"), "utf8", function (err, contents) {
assert.strictEqual(contents,
assertEqual(contents,
"Bootup. booting: 1, online: 0, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 0, online: 1, dying: 0, new_booting: 0, new_online: 0\n" +
"Ready. booting: 0, online: 1, dying: 0, new_booting: 0, new_online: 0\n" +
Expand Down Expand Up @@ -298,15 +303,15 @@ steps = [
], {
PORT: port,
}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"Bootup. booting: 5, online: 0, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 4, online: 1, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 3, online: 2, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 2, online: 3, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 1, online: 4, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 0, online: 5, dying: 0, new_booting: 0, new_online: 0\n");
assert.strictEqual(stdout, "workers online: 5\n")
assert.strictEqual(code, 0)
assertEqual(stdout, "workers online: 5\n")
assertEqual(code, 0)
cb();
});
},
Expand All @@ -320,7 +325,7 @@ steps = [
info: "ability to stop a running server with multiple workers",
fn: function (cb) {
naught_exec(["stop", "some/dir/ipc"], {}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"ShutdownOld. booting: 0, online: 4, dying: 1, new_booting: 0, new_online: 0\n" +
"ShutdownOld. booting: 0, online: 3, dying: 2, new_booting: 0, new_online: 0\n" +
"ShutdownOld. booting: 0, online: 2, dying: 3, new_booting: 0, new_online: 0\n" +
Expand All @@ -331,8 +336,8 @@ steps = [
"OldExit. booting: 0, online: 0, dying: 2, new_booting: 0, new_online: 0\n" +
"OldExit. booting: 0, online: 0, dying: 1, new_booting: 0, new_online: 0\n" +
"OldExit. booting: 0, online: 0, dying: 0, new_booting: 0, new_online: 0\n");
assert.strictEqual(stdout, "");
assert.strictEqual(code, 0)
assertEqual(stdout, "");
assertEqual(code, 0)
cb();
});
},
Expand All @@ -343,7 +348,7 @@ steps = [
collectLogFiles("log/naught", function (err, files, data) {
if (err) return cb(err)
assert.ok(files.length > 1);
assert.strictEqual(data,
assertEqual(data,
"Bootup. booting: 5, online: 0, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 4, online: 1, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 3, online: 2, dying: 0, new_booting: 0, new_online: 0\n" +
Expand Down Expand Up @@ -372,7 +377,7 @@ steps = [
collectLogFiles("log/stderr", function (err, files, data) {
if (err) return cb(err)
assert.ok(files.length > 1);
assert.strictEqual(data,
assertEqual(data,
"server3 listening\n" +
"server3 listening\n" +
"server3 listening\n" +
Expand All @@ -394,7 +399,7 @@ steps = [
collectLogFiles("log/stdout", function (err, files, data) {
if (err) return cb(err)
assert.ok(files.length > 1);
assert.strictEqual(data,
assertEqual(data,
"server3 attempting to listen\n" +
"server3 attempting to listen\n" +
"server3 attempting to listen\n" +
Expand All @@ -418,12 +423,12 @@ steps = [
naught_exec(["start", "--worker-count", "2", "server.js"], {
PORT: port,
}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"Bootup. booting: 2, online: 0, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 1, online: 1, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 0, online: 2, dying: 0, new_booting: 0, new_online: 0\n");
assert.strictEqual(stdout, "workers online: 2\n")
assert.strictEqual(code, 0)
assertEqual(stdout, "workers online: 2\n")
assertEqual(code, 0)
cb();
});
},
Expand All @@ -432,15 +437,50 @@ steps = [
info: "ability to stop a hanging server with a timeout",
fn: function (cb) {
naught_exec(["stop", "--timeout", "0.3"], {}, function(stdout, stderr, code) {
assert.strictEqual(stderr,
assertEqual(stderr,
"ShutdownOld. booting: 0, online: 1, dying: 1, new_booting: 0, new_online: 0\n" +
"ShutdownOld. booting: 0, online: 0, dying: 2, new_booting: 0, new_online: 0\n" +
"DestroyOld. booting: 0, online: 0, dying: 2, new_booting: 0, new_online: 0\n" +
"DestroyOld. booting: 0, online: 0, dying: 2, new_booting: 0, new_online: 0\n" +
"OldExit. booting: 0, online: 0, dying: 1, new_booting: 0, new_online: 0\n" +
"OldExit. booting: 0, online: 0, dying: 0, new_booting: 0, new_online: 0\n");
assert.strictEqual(stdout, "");
assert.strictEqual(code, 0)
assertEqual(stdout, "");
assertEqual(code, 0)
cb();
});
},
},
rm(["naught.log", "stderr.log", "stdout.log", "server.js"]),
use("server5.js"),
{
info: "ability to pass command line arguments to node",
fn: function (cb) {
naught_exec([
"start",
"--node-args", "--harmony --use-strict",
"server.js",
], {
PORT: port,
}, function(stdout, stderr, code) {
assertEqual(stderr,
"Bootup. booting: 1, online: 0, dying: 0, new_booting: 0, new_online: 0\n" +
"WorkerOnline. booting: 0, online: 1, dying: 0, new_booting: 0, new_online: 0\n");
assertEqual(stdout, "workers online: 1\n")
assertEqual(code, 0)
cb();
});
},
},
get("make sure --harmony --use-strict worked", "/hi", "0\n1\n2\nserver5 says hi\n"),
{
info: "(test setup) stopping server",
fn: function (cb) {
naught_exec(["stop"], {}, function(stdout, stderr, code) {
assertEqual(stderr,
"ShutdownOld. booting: 0, online: 0, dying: 1, new_booting: 0, new_online: 0\n" +
"OldExit. booting: 0, online: 0, dying: 0, new_booting: 0, new_online: 0\n");
assertEqual(stdout, "");
assertEqual(code, 0)
cb();
});
},
Expand Down

0 comments on commit 6e57573

Please sign in to comment.