Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working Directory, Signals, Respawn and Internals. #37

Closed
wants to merge 10 commits into from
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ when files change in the working directory.
- Easy-to-use CLI interface for development with automatic reloading
upon file changes.
- Gracefully handles reloads with syntax errors during development.
- Auto-spawns new workers upon unexepected exits without cyclical respawn disasters.
- Built on [distribute](http://github.com/learnboost/distribute).

## Setup
Expand Down Expand Up @@ -90,6 +91,17 @@ The `up` command accepts the following options:
- Defaults to `up`.
- The value will be appended with `master` or `worker` (e.g "up master", "up worker").

- `u`/`--uptime`

- The mininum update a worker process should live for in order to be replaced by another worker. It will otherwise be considered a cyclical restart.
- Defaults to 5000 ms.

- `-C`/`--cwd`

- The working directory to change into prior to starting the master process.
- Defaults to the current working directory.
- Note that this will effect the pathing used for `--pidfile` and `<file>`.

### B) JavaScript API

```js
Expand Down
83 changes: 79 additions & 4 deletions bin/up
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ program
.option('-n, --number <workers>', 'Number of workers to spawn.'
, 'development' == process.env.NODE_ENV ? 1 : cpus)
.option('-t, --timeout [ms]', 'Worker timeout.')
.option('-u, --uptime [ms]', 'Minimum worker uptime for exit replacement.')
.option('-C, --cwd <directory>', 'Change working directory.', path.resolve);

/**
* Capture requires.
Expand All @@ -60,6 +62,20 @@ program.on('require', function (file) {

program.parse(process.argv);

/**
* Change to the optional working directory.
*/

if (program.directory) {
try {
process.chdir(program.directory);
debug('working directory set %s', program.directory);
} catch (e) {
error('\n Error changing working directory to "%s".\n %s\n'
, program.directory, e.stack);
}
}

/**
* Helper function to exit with an error.
*
Expand Down Expand Up @@ -148,11 +164,13 @@ var httpServer = http.Server().listen(program.port)
, workerTimeout: workerTimeout
, requires: requires
, title: program.title
, uptimeThreshold: program.uptime
})

/**
* Write pidfile
*/

if (program.pidfile) {
fs.writeFile(program.pidfile, process.pid, function(err) {
if (err) {
Expand All @@ -162,14 +180,69 @@ if (program.pidfile) {
}

/**
* Listen on SIGUSR2 signal.
* Shutdown workers gracefully but keep the master going.
*/

process.on('SIGUSR2', function () {
debug('\033[97mSIGUSR2\033[90m signal detected - reloading');
signal('SIGUSR1', 'shutdown workers', function () {
srv.shutdownWorkers();
});

/**
* Reload load workers gracefully.
*/

signal('SIGUSR2', 'reload', function () {
srv.reload();
});

/**
* Spawn a new worker.
*/

signal('SIGTTIN', 'add worker', function () {
srv.spawnWorker();
});

/**
* Remove a worker.
*/

signal('SIGTTOU', 'remove worker', function () {
srv.removeWorker();
});

/**
* Shutdown workers and master gracefully.
*/

signal('SIGQUIT', 'shutdown workers and master', function () {
srv.shutdown(function () {
process.exit(0);
});
});

/**
* Hard shutdown workers and master.
*/

signal('SIGTERM', 'hard shutdown master and workers', function () {
srv.exit(function () {
process.exit(0);
});
});

/**
* Helper function for signals.
*/

function signal (sig, msg, fn) {
process.on(sig, function() {
debug('\033[97m' + sig +'\033[90m signal detected - ' + msg);
fn && fn.apply(process, arguments);
});
}


/**
* Use the preferred "raw mode" API depending on the node version.
*/
Expand All @@ -193,7 +266,9 @@ if (tty.isatty(0)) {
var key = b.toString('utf8');
switch (key) {
case '\u0003': // Ctrl+C
process.exit();
srv.exit(function () {
process.exit(0);
});
break;

case '\u0012': // Ctrl+R
Expand Down
Loading