Skip to content

Commit a14c0ac

Browse files
committed
Fix a bug in telemetry flush and clean up
1 parent afa6cec commit a14c0ac

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

‎apps/rush/src/actions/RebuildAction.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export default class RebuildAction extends BaseRushAction {
142142
console.log(colors.green(`rush ${this.options.actionVerb} (${stopwatch.toString()})`));
143143
this._collectTelemetry(stopwatch, true);
144144
this.eventHooksManager.handle(Event.postRushBuild, this._parser.isDebug);
145+
this._parser.flushTelemetry();
145146
},
146147
() => {
147148
stopwatch.stop();

‎apps/rush/src/actions/RushCommandLineParser.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class RushCommandLineParser extends CommandLineParser {
5050

5151
public exitWithError(): void {
5252
try {
53-
this._flushTelemetry();
53+
this.flushTelemetry();
5454
} finally {
5555
process.exit(1);
5656
}
@@ -60,6 +60,12 @@ export default class RushCommandLineParser extends CommandLineParser {
6060
return this._debugParameter.value;
6161
}
6262

63+
public flushTelemetry(): void {
64+
if (this.telemetry) {
65+
this.telemetry.flush();
66+
}
67+
}
68+
6369
protected onDefineParameters(): void {
6470
this._debugParameter = this.defineFlagParameter({
6571
parameterLongName: '--debug',
@@ -84,13 +90,7 @@ export default class RushCommandLineParser extends CommandLineParser {
8490
private _execute(): void {
8591
this.telemetry = new Telemetry(RushConfiguration.loadFromDefaultLocation());
8692
super.onExecute();
87-
this._flushTelemetry();
88-
}
89-
90-
private _flushTelemetry(): void {
91-
if (this.telemetry) {
92-
this.telemetry.flush();
93-
}
93+
this.flushTelemetry();
9494
}
9595

9696
private _populateActions(): void {

‎apps/rush/src/utilities/Telemetry.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ export interface ITelemetryData {
2020
extraData?: { [key: string]: string };
2121
}
2222

23+
const MAX_FILE_COUNT: number = 100;
24+
2325
export default class Telemetry {
2426
private _enabled: boolean;
2527
private _store: ITelemetryData[];
2628
private _dataFolder: string;
27-
private readonly _MAX_FILE_COUNT: number = 100;
2829

2930
public constructor(private _rushConfiguration: RushConfiguration) {
3031
this._enabled = this._rushConfiguration.telemetryEnabled;
@@ -49,12 +50,12 @@ export default class Telemetry {
4950
if (!this._enabled || this._store.length === 0) {
5051
return;
5152
}
52-
this._cleanUp();
5353

5454
const fullPath: string = this._getFilePath();
5555
fsx.ensureDirSync(this._dataFolder);
5656
writeFile(fullPath, JSON.stringify(this._store));
5757
this._store = [];
58+
this._cleanUp();
5859
}
5960

6061
public get store(): ITelemetryData[] {
@@ -65,22 +66,29 @@ export default class Telemetry {
6566
* When there are too many log files, delete the old ones.
6667
*/
6768
private _cleanUp(): void {
68-
if (fsx.exists(this._dataFolder)) {
69+
if (fsx.existsSync(this._dataFolder)) {
6970
const files: string[] = fsx.readdirSync(this._dataFolder);
70-
const filesToDelete: number = files.length - this._MAX_FILE_COUNT + 1;
71-
if (filesToDelete > 0) {
72-
const sortedFiles: string[] = files.map(filePath => {
71+
if (files.length > MAX_FILE_COUNT) {
72+
const sortedFiles: string[] = files.map(fileName => {
73+
const filePath: string = path.join(this._dataFolder, fileName);
74+
const stats: fsx.Stats = fsx.statSync(filePath);
7375
return {
7476
filePath: filePath,
75-
modifiedTime: fsx.statSync(filePath).mtime.getTime()
77+
modifiedTime: stats.mtime.getTime(),
78+
isFile: stats.isFile()
7679
};
7780
})
81+
.filter(value => {
82+
// Only delete files
83+
return value.isFile;
84+
})
7885
.sort((a, b) => {
7986
return a.modifiedTime - b.modifiedTime;
8087
})
8188
.map(s => {
8289
return s.filePath;
8390
});
91+
const filesToDelete: number = sortedFiles.length - MAX_FILE_COUNT;
8492
for (let i: number = 0; i < filesToDelete; i++) {
8593
fsx.unlinkSync(sortedFiles[i]);
8694
}

0 commit comments

Comments
 (0)