Skip to content

Commit

Permalink
Allowing (again) the rotation of files that haven't been written to.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Tupper committed Feb 27, 2016
1 parent 55fdc48 commit 82cc8c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
7 changes: 6 additions & 1 deletion lib/limitedqueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ function LimitedQueue(worker) {
}
}

function isEmpty() {
return queue.length() === 0;
}

function paused() {
return queue.paused;
}
Expand All @@ -130,7 +134,8 @@ function LimitedQueue(worker) {
pause: queue.pause,
resume: queue.resume,
unshift: queue.unshift,
join
join,
isEmpty
}, base);
}

Expand Down
28 changes: 23 additions & 5 deletions lib/rotatingfilestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,34 @@ function RotatingFileStream(options) {

function rotateActual() {

base.once('data', function () {
rotateFunction = rotateActual;
});

rotateFunction = function () {};

rotator.rotate(function (err, newstream, filePath) {
async.parallel([
function waitForWrite(next) {
rotator.once('newfile', function () {
if (writeQueue.isEmpty()) {
// No logs to write, so we're all clear to allow
// rotations again
next();
} else {
// We've got some logs to write, ensure we get at least
// one log record into the file before allowing
// another rotation
base.once('data', function (info) {
next();
});
}
});
},
function doRotation(next) {
rotator.rotate(next);
}
], function allowRotationsAgain(err) {
if (err) {
base.emit('error', err);
}

rotateFunction = rotateActual;
});
}

Expand Down
2 changes: 1 addition & 1 deletion test-perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function throughput(next) {
function (next) { rmdir(name, ignoreMissing(next)); },
function (next) { fx.mkdir(name, next); },
function (next) { runTest (name, {
stream: { path: name + '/test.log' },
stream: { path: name + '/test.log', noCyclesCheck: true },
batch: { iterations: 1000000, size: 1000 }
}, next); },
function (next) {
Expand Down
24 changes: 23 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function runTest(name, options, next) {
var log = bunyan.createLogger({
name: 'foo',
streams: [{
type: 'raw',
stream: rfs
}]
});
Expand Down Expand Up @@ -84,7 +85,7 @@ function basicthreshold(next) {
function (next) { rmdir(name, ignoreMissing(next)); },
function (next) { fx.mkdir(name, next); },
function (next) { runTest (name, {
stream: { path: name + '/test.log', threshold: '1m' },
stream: { path: name + '/test.log', threshold: '1m', fieldOrder: ['pid', 'time'] },
batch: { iterations: 100000 }
}, next); },
function (next) {
Expand Down Expand Up @@ -137,6 +138,26 @@ function timerotation(next) {
], next);
}

function timerotationnologging(next) {
var name = 'testlogs/' + 'timerotationnologging';

async.series([
function (next) { rmdir(name, ignoreMissing(next)); },
function (next) { fx.mkdir(name, next); },
function (next) { runTest (name, {
stream: { path: name + '/test.log', period: '1000ms' },
batch: { size: 0, duration: 9500 }
}, next); },
function (next) {
var files = fs.readdirSync(name);
assert.equal(10, files.length);
console.log(name, 'passed');
next();
},
function (next) { rmdir(name, ignoreMissing(next)); }
], next);
}

function gzippedfiles(next) {
var name = 'testlogs/' + 'gzippedfiles';

Expand Down Expand Up @@ -362,6 +383,7 @@ function checksetlongtimeoutclearnormalperiods(next) {
async.parallel([
basicthreshold,
timerotation,
timerotationnologging,
gzippedfiles,
totalsize,
totalfiles,
Expand Down

0 comments on commit 82cc8c5

Please sign in to comment.