Fixes bug introduced in github issue #132 where file rolling needs to be handled differently for alwaysIncludePattern streams

This commit is contained in:
Issac Goldstand 2013-05-11 23:01:28 +03:00
parent 2da01cc611
commit dc632f4705
2 changed files with 78 additions and 18 deletions

View File

@ -23,13 +23,13 @@ function DateRollingFileStream(filename, pattern, options, now) {
this.pattern = pattern || '.yyyy-MM-dd'; this.pattern = pattern || '.yyyy-MM-dd';
this.now = now || Date.now; this.now = now || Date.now;
this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now())); this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now()));
this.alwaysIncludePattern = false;
this.baseFilename = filename; this.baseFilename = filename;
this.alwaysIncludePattern = false;
if (options) { if (options) {
if (options.alwaysIncludePattern) { if (options.alwaysIncludePattern) {
this.alwaysIncludePattern = true; this.alwaysIncludePattern = true;
filename = filename + this.lastTimeWeWroteSomething; filename = this.baseFilename + this.lastTimeWeWroteSomething;
} }
delete options.alwaysIncludePattern; delete options.alwaysIncludePattern;
if (Object.keys(options).length === 0) { if (Object.keys(options).length === 0) {
@ -55,18 +55,26 @@ DateRollingFileStream.prototype.shouldRoll = function() {
}; };
DateRollingFileStream.prototype.roll = function(filename, callback) { DateRollingFileStream.prototype.roll = function(filename, callback) {
var that = this, var that = this;
newFilename = this.baseFilename + this.previousTime;
debug("Starting roll"); debug("Starting roll");
async.series([ if (this.alwaysIncludePattern) {
this.closeTheStream.bind(this), this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
deleteAnyExistingFile, async.series([
renameTheCurrentFile, this.closeTheStream.bind(this),
this.openTheStream.bind(this) this.openTheStream.bind(this)
], callback); ], callback);
} else {
var newFilename = this.baseFilename + this.previousTime;
async.series([
this.closeTheStream.bind(this),
deleteAnyExistingFile,
renameTheCurrentFile,
this.openTheStream.bind(this)
], callback);
}
function deleteAnyExistingFile(cb) { function deleteAnyExistingFile(cb) {
//on windows, you can get a EEXIST error if you rename a file to an existing file //on windows, you can get a EEXIST error if you rename a file to an existing file
//so, we'll try to delete the file we're renaming to first //so, we'll try to delete the file we're renaming to first
@ -77,13 +85,8 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
} }
function renameTheCurrentFile(cb) { function renameTheCurrentFile(cb) {
if (this.alwaysIncludePattern) { debug("Renaming the " + filename + " -> " + newFilename);
// no-op fs.rename(filename, newFilename, cb);
cb();
} else {
debug("Renaming the " + filename + " -> " + newFilename);
fs.rename(filename, newFilename, cb);
}
} }
}; };

View File

@ -125,6 +125,63 @@ vows.describe('DateRollingFileStream').addBatch({
} }
} }
} }
},
'with alwaysIncludePattern': {
topic: function() {
var that = this,
testTime = new Date(2012, 8, 12, 0, 10, 12),
stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-pattern', '.yyyy-MM-dd', {alwaysIncludePattern: true}, now);
stream.write("First message\n", 'utf8', function() {
that.callback(null, stream);
});
},
teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'),
'should create a file with the pattern set': {
topic: function(stream) {
fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback);
},
'file should contain first message': function(result) {
assert.equal(result.toString(), "First message\n");
}
},
'when the day changes': {
topic: function(stream) {
testTime = new Date(2012, 8, 13, 0, 10, 12);
stream.write("Second message\n", 'utf8', this.callback);
},
teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13'),
'the number of files': {
topic: function() {
fs.readdir(__dirname, this.callback);
},
'should be two': function(files) {
assert.equal(files.filter(function(file) { return file.indexOf('test-date-rolling-file-stream-pattern') > -1; }).length, 2);
}
},
'the file with the later date': {
topic: function() {
fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', this.callback);
},
'should contain the second message': function(contents) {
assert.equal(contents.toString(), "Second message\n");
}
},
'the file with the date': {
topic: function() {
fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback);
},
'should contain the first message': function(contents) {
assert.equal(contents.toString(), "First message\n");
}
}
}
} }
}).exportTo(module); }).exportTo(module);