Permalink
Browse files

[fix] Ensure pidFile is written to disk (and updated on restart) by b…

…in/monitor
  • Loading branch information...
1 parent 1dfe0d0 commit 9823d133d49c0c089b384b93a9a0ba64506fb96d @indexzero indexzero committed Jul 8, 2012
Showing with 31 additions and 2 deletions.
  1. +31 −2 bin/monitor
View
@@ -1,10 +1,22 @@
-var path = require('path'),
+var fs = require('fs'),
+ path = require('path'),
forever = require(path.resolve(__dirname, '..', 'lib', 'forever')),
started;
//
-// ### @function (options)
+// ### @function (file, pid)
+// #### @file {string} Location of the pid file.
+// #### @pid {number} pid to write to disk.
+// Write the pidFile to disk for later use
+//
+function writePid(file, pid) {
+ fs.writeFileSync(file, pid, 'utf8');
+}
+
+//
+// ### @function start (options)
// #### @options {Object} Options for the `forever.Monitor` instance.
+// Starts the child process and disconnects from the IPC channel.
//
function start(options) {
var script = process.argv[2],
@@ -14,14 +26,31 @@ function start(options) {
monitor.start();
monitor.on('start', function () {
+ //
// This starts an nssocket server, which the forever CLI uses to
// communicate with this monitor process after it's detached.
// Without this, `forever list` won't show the process, even though it
// would still be running in the background unaffected.
+ //
forever.startServer(monitor);
+
+ //
// Disconnect the IPC channel, letting this monitor's parent process know
// that the child has started successfully.
+ //
process.disconnect();
+
+ //
+ // Write the pidFile to disk
+ //
+ writePid(options.pidFile, monitor.child.pid);
+ });
+
+ //
+ // When the monitor restarts update the pid in the pidFile
+ //
+ monitor.on('restart', function () {
+ writePid(options.pidFile, monitor.child.pid);
});
}

0 comments on commit 9823d13

Please sign in to comment.