Skip to content

Commit

Permalink
Add -R option -- wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed May 29, 2012
1 parent 79c79b0 commit 72a2b55
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
13 changes: 13 additions & 0 deletions bin/strata
Expand Up @@ -71,6 +71,17 @@ for (var i = 2; i < process.argv.length; ++i) {
case "--socket":
options.socket = nextArg();
break;
case "-R":
var interval = parseFloat(process.argv[i + 1]);

if (interval) {
i++; // Numeric argument was given.
} else {
interval = 1;
}

options.interval = Math.round(interval * 1000);
break;
case "-v":
case "--version":
console.log(strata.version.join("."));
Expand Down Expand Up @@ -106,6 +117,8 @@ function printUsage() {
" -h, --help Show this help message and exit",
" -k, --key The name of the private key file (HTTPS only)",
" -p, --port The port number to listen on (defaults to 1982)",
" -R [interval] Reloads the appFile in a new V8 instance every",
" interval in seconds. The default interval is 1",
" -s, --socket The unix socket to listen to. If this is given",
" the -a and -p options are ignored",
" -v, --version Show the current version of Strata and exit"
Expand Down
23 changes: 23 additions & 0 deletions lib/child.js
@@ -0,0 +1,23 @@
var strata = require("./index");

process.on("message", function (message, serverHandle) {
if (!serverHandle) {
return;
}

var appFile = message.appFile;
var options = message.options;

// Don't spawn a child inside a child.
delete options.interval;

// Use the server handle from the parent process.
options.socket = serverHandle;

// Force the child to be quiet.
options.quiet = true;

strata.runFile(appFile, options, function () {
process.send({ready: true});
});
});
49 changes: 48 additions & 1 deletion lib/index.js
Expand Up @@ -48,12 +48,59 @@ exports.__defineGetter__("app", function () {
return _defaultApp;
});

var fork = require("child_process").fork;
var childPath = require("path").join(__dirname, "child.js");

/**
* Creates and starts a server for the app in the given `appFile`. If an `opts`
* object is given it may contain any properties used to create and start the
* server (see `createServer` and `startServer`).
* server (see `createServer` and `startServer`), as well as the following:
*
* - interval The interval (in milliseconds) at which the server should
* reload the given `appFile` in a new V8 instance. This is
* useful in development when you want to see changes to your
* application reflected without manually restarting the server
*/
function runFile(appFile, opts, listeningListener) {
if (opts.interval) {
var server = createServer(opts);

var child;
server.on("listening", function () {
// setInterval(function () {
var prevChild = child;

console.log('fork')

// Fork a new V8 instance to run the app in.
child = fork(childPath, null, {
cwd: process.cwd(),
env: process.env
});

// Once the new child is ready, kill the previous child.
child.on("message", function (message) {
console.log('ready')
if (message.ready && prevChild) {
console.log('kill')
prevChild.kill("SIGTERM");
}
});

// Tell the child to run the app using the same server handle
// as the parent process.
child.send({
appFile: appFile,
options: opts
}, server._handle);
// }, opts.interval);
});

startServer(server, opts, listeningListener);

return server;
}

return run(require(appFile), opts, listeningListener);
}

Expand Down

0 comments on commit 72a2b55

Please sign in to comment.