Skip to content

Commit ae5eff9

Browse files
committed
Simplify codebase
1 parent 8b341d6 commit ae5eff9

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

index.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const WriteQueue = require('./lib/writeQueue');
22
const PeriodTrigger = require('./lib/periodTrigger');
33
const ThresholdTrigger = require('./lib/thresholdTrigger');
44
const FileRotator = require('./lib/fileRotator');
5-
const {BytesWritten, FileStartTime, Rotate, NewFile} = require('./lib/customEvents');
5+
const {BytesWritten, Rotate, NewFile} = require('./lib/customEvents');
66

77
const fileStreams = [];
88

@@ -20,13 +20,8 @@ class RotatingFileStream {
2020
this._queue = new WriteQueue();
2121
this._triggers = [];
2222
if (config.period) {
23-
const periodTrigger = new PeriodTrigger(config.period);
23+
const periodTrigger = new PeriodTrigger(config.period, config.rotateExisting);
2424
this._triggers.push(periodTrigger);
25-
if (config.rotateExisting) {
26-
this._rotator.once(FileStartTime, (startTime) => {
27-
periodTrigger.setInitialTime(startTime);
28-
});
29-
}
3025
}
3126
if (config.threshold) {
3227
const thresholdTrigger = new ThresholdTrigger(config.threshold);
@@ -39,9 +34,9 @@ class RotatingFileStream {
3934
this._rotate();
4035
});
4136
});
42-
this._rotator.on(NewFile, () => {
37+
this._rotator.on(NewFile, (fileInfo) => {
4338
this._queue.setFileHandle(this._rotator.getCurrentHandle());
44-
this._triggers.forEach(trigger => trigger.newFile());
39+
this._triggers.forEach(trigger => trigger.newFile(fileInfo));
4540
});
4641
this._initialised = this._rotator.initialise();
4742
}

lib/customEvents.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
module.exports = {
22
// Emitted when a new rotation is needed
33
Rotate: Symbol('rotate'),
4-
// Emitted when a new file descriptor is in use, after a rotation
4+
// Emitted when a new file handle is acquired with the file info
55
NewFile: Symbol('newFile'),
6-
// Emitted once with the 'created at' of the initial file
7-
FileStartTime: Symbol('fileStartTime'),
86
// Emitted whenever data is written to the file with the number of bytes
97
BytesWritten: Symbol('bytesWritten')
108
};

lib/fileRotator.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ const path = require('path');
88
const {processSize} = require('../util/configProcessors');
99

1010
const {EventEmitter} = require('events');
11-
const {NewFile, FileStartTime} = require('./customEvents');
12-
13-
// NOTE: Emit NewFile THEN FileStartTime to ensure PeriodTrigger functions correctly
11+
const {NewFile} = require('./customEvents');
1412

1513
class FileRotator extends EventEmitter {
1614
constructor(config) {
@@ -36,8 +34,6 @@ class FileRotator extends EventEmitter {
3634
} else {
3735
// Will open existing rather than immediately rotate
3836
await this._initialiseNewFile();
39-
const fileInfo = await fs.stat(this._getFileName(0, false));
40-
this.emit(FileStartTime, fileInfo.birthtimeMs);
4137
}
4238
}
4339

@@ -69,7 +65,8 @@ class FileRotator extends EventEmitter {
6965

7066
async _initialiseNewFile() {
7167
this._currentHandle = await fs.open(this._getFileName(0, false), 'a');
72-
this.emit(NewFile);
68+
const fileInfo = await this._currentHandle.stat();
69+
this.emit(NewFile, fileInfo);
7370
return this._currentHandle;
7471
}
7572

lib/periodTrigger.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ const getNextRotation = (period, lastRotation) => {
6969
};
7070

7171
class PeriodTrigger extends EventEmitter {
72-
constructor(period) {
72+
constructor(period, rotateExisting) {
7373
super();
7474
this._period = processPeriod(period);
75+
this._rotateExisting = rotateExisting;
7576
this._task = null;
7677
this._rotateAt = null;
7778
}
7879

79-
newFile() {
80-
this.setNextTask();
81-
}
82-
83-
setInitialTime(birthTime) {
84-
this._rotateAt = birthTime;
80+
newFile(fileInfo) {
81+
if (this._rotateExisting) {
82+
this._rotateAt = fileInfo.birthtimeMs;
83+
// Only rotate based on the first file once
84+
this._rotateExisting = false;
85+
}
8586
this.setNextTask();
8687
}
8788

lib/thresholdTrigger.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ class ThresholdTrigger extends EventEmitter {
99
this._totalWritten = 0;
1010
}
1111

12-
newFile() {
13-
this._totalWritten = 0;
12+
newFile(fileInfo) {
13+
this._totalWritten = fileInfo.size;
14+
if (this._totalWritten > this.threshold) {
15+
// Case where initial file is larger than threshold - usually on config change between boots
16+
this.emit(Rotate);
17+
}
1418
}
1519

1620
updateWritten(bytes) {

test/filerotate.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const assert = require('assert');
33
const sandbox = sinon.createSandbox();
44
const bunyan = require('bunyan');
55
const path = require('path');
6-
const mkdirp = require('mkdirp');
76

87
const RotatingFileStream = require('../index');
8+
const fs = require('fs').promises;
99
const testConfig = {
1010
path: 'logs/foo.log',
1111
totalFiles: 10,
@@ -14,9 +14,15 @@ const testConfig = {
1414
gzip: true
1515
};
1616

17+
function delay(ms) {
18+
return new Promise(resolve => setTimeout(resolve, ms));
19+
}
20+
1721
describe('RotatingFileStream', function () {
1822
before(async function () {
19-
await mkdirp(path.parse(testConfig.path).dir);
23+
await fs.mkdir(path.parse(testConfig.path).dir, {
24+
recursive: true
25+
});
2026
});
2127

2228
afterEach(function () {
@@ -40,6 +46,7 @@ describe('RotatingFileStream', function () {
4046
for (let i = 0; i < 100; i++) {
4147
logger.info('Testing ' + i);
4248
}
49+
await delay(0); // immediate delay causes logs to sync
4350
await stream.end();
4451
});
4552

0 commit comments

Comments
 (0)