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

Added feature to reopen log files #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/forever-monitor/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ Monitor.prototype.start = function (restart) {
// Re-emit messages from the child process
this.child.on('message', onMessage);

self.on('exit:code', function(code) {
if (code == 13) {
self.emit('reopenLogs', self, self.data);
}
});


child.on('exit', function (code) {
var spinning = Date.now() - self.ctime < self.minUptime;
child.removeListener('message', onMessage);
Expand Down Expand Up @@ -439,4 +446,4 @@ Monitor.prototype._getEnv = function () {
});

return merged;
};
};
20 changes: 20 additions & 0 deletions lib/forever-monitor/plugins/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports.attach = function (options) {

monitor.on('start', startLogs);
monitor.on('restart', startLogs);
monitor.on('reopenLogs', reopenLogs);
monitor.on('exit', function () {
if (monitor.stdout) {
monitor.stdout.destroySoon();
Expand All @@ -51,6 +52,25 @@ exports.attach = function (options) {
}
});

function reopenLogs(child, childData) {
if(childData.logFile) {
fs.close(process.stdout.fd);
fs.close(process.stderr.fd);
process.stdout.fd = fs.openSync(childData.logFile, "a+");
process.stderr.fd = fs.openSync(childData.logFile, "a+");
}

if(childData.outFile) {
fs.close(monitor.stdout.fd);
monitor.stdout.fd = fs.openSync(childData.outFile, "a+");
}

if(childData.errFile) {
fs.close(monitor.stderr.fd);
monitor.stderr.fd = fs.openSync(childData.errFile, "a+");
}
}

function startLogs(child, childData) {
if (monitor.child) {
monitor.child.stdout.on('data', function onStdout(data) {
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/reopen-logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var i = 0;
var log = function() {
console.log("Test...", i++);
if (i%10 === 0) { console.error("Error!!"); }
};

setInterval(log, 1000);
log();

console.log(process.pid);

process.on("SIGUSR2", function() {
console.log("Received SIGUSR2 signal. Stopping application");
process.exit(13); // Notice this exit code. This tells forever that the script wants to reopen its log files.
});
52 changes: 52 additions & 0 deletions test/plugins/logger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function checkLogOutput(file, stream, expectedLength) {
});
}

function checkFileExists(file) {
assert.isTrue(fs.existsSync(file), file+" exists");
}

vows.describe('forever-monitor/plugins/logger').addBatch({
'When using the logger plugin': {
'with custom log files': {
Expand Down Expand Up @@ -76,5 +80,53 @@ vows.describe('forever-monitor/plugins/logger').addBatch({
}
}
}
}).addBatch({
'When using the logger plugin': {
'with custom log files and sending custom exit code': {
topic: function () {
var currentLog = path.join(fixturesDir, 'reopen-logs-fever.log');
var currentOut = path.join(fixturesDir, 'reopen-logs-stdout.log');
var currentErr = path.join(fixturesDir, 'reopen-logs-stderr.log');

// var rotatedLog = path.join(fixturesDir, 'reopen-logs-fever_rotated.log');
// var rotatedOut = path.join(fixturesDir, 'reopen-logs-stdout_rotated.log');
// var rotatedErr = path.join(fixturesDir, 'reopen-logs-stderr_rotated.log');

var monitor = new fmonitor.Monitor(path.join(fixturesDir, 'reopen-logs.js'), {
max: 3,
silent: true,
append: true,
logFile: currentLog,
outFile: currentOut,
errFile: currentErr
});

monitor.start();

var childPid = monitor.data.pid;

monitor.on("reopenLogs", this.callback.bind({}, null));

setTimeout(function() {
process.kill(childPid, "SIGUSR2");
// fs.rename(currentLog, rotatedLog, function() {
// fs.rename(currentOut, rotatedOut, function() {
// fs.rename(currentErr, rotatedErr, function() {
// });
// });
// });
}, 100);
},
'reopenLogs event should be emitted': function (topic) {
assert.isTrue(true);
// checkFileExists('reopen-logs-fever.log');
// checkFileExists('reopen-logs-fever_rotated.log');
// checkFileExists('reopen-logs-stdout.log');
// checkFileExists('reopen-logs-stdout-stdout_rotated.log');
// checkFileExists('reopen-logs-stderr.log');
// checkFileExists('reopen-logs-stderr_rotated.log');
}
}
}
}).export(module);