Skip to content

Commit ec56be5

Browse files
authored
Merge pull request microsoft#371 from Microsoft/qz2017/telemetry_fix
Fix some issues in rush telemetry flush and cleanup
2 parents 9efb243 + 0d66406 commit ec56be5

4 files changed

Lines changed: 74 additions & 18 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,16 @@ export default class RebuildAction extends BaseRushAction {
141141
stopwatch.stop();
142142
console.log(colors.green(`rush ${this.options.actionVerb} (${stopwatch.toString()})`));
143143
this._collectTelemetry(stopwatch, true);
144+
this._parser.flushTelemetry();
145+
this.eventHooksManager.handle(Event.postRushBuild, this._parser.isDebug);
144146
},
145147
() => {
146148
stopwatch.stop();
147149
console.log(colors.red(`rush ${this.options.actionVerb} - Errors! (${stopwatch.toString()})`));
148150
this._collectTelemetry(stopwatch, false);
151+
this._parser.flushTelemetry();
152+
this.eventHooksManager.handle(Event.postRushBuild, this._parser.isDebug);
149153
this._parser.exitWithError();
150-
})
151-
.then(() => {
152-
this.eventHooksManager.handle(Event.postRushBuild);
153154
});
154155
}
155156

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,22 @@ 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
}
5757
}
5858

59+
public get isDebug(): boolean {
60+
return this._debugParameter.value;
61+
}
62+
63+
public flushTelemetry(): void {
64+
if (this.telemetry) {
65+
this.telemetry.flush();
66+
}
67+
}
68+
5969
protected onDefineParameters(): void {
6070
this._debugParameter = this.defineFlagParameter({
6171
parameterLongName: '--debug',
@@ -80,13 +90,7 @@ export default class RushCommandLineParser extends CommandLineParser {
8090
private _execute(): void {
8191
this.telemetry = new Telemetry(RushConfiguration.loadFromDefaultLocation());
8292
super.onExecute();
83-
this._flushTelemetry();
84-
}
85-
86-
private _flushTelemetry(): void {
87-
if (this.telemetry) {
88-
this.telemetry.flush();
89-
}
93+
this.flushTelemetry();
9094
}
9195

9296
private _populateActions(): void {

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,39 @@ import * as colors from 'colors';
77
import {
88
EventHooks,
99
Utilities,
10-
Event
10+
Event,
11+
Stopwatch
1112
} from '@microsoft/rush-lib';
1213

1314
export default class EventHooksManager {
1415
public constructor(private _eventHooks: EventHooks) {
1516
}
1617

17-
public handle(event: Event): void {
18+
public handle(event: Event, isDebug: boolean = false): void {
1819
if (!this._eventHooks) {
1920
return;
2021
}
2122
const scripts: string[] = this._eventHooks.get(event);
2223
if (scripts.length > 0) {
24+
const stopwatch: Stopwatch = Stopwatch.start();
2325
console.log(os.EOL + colors.green(`Executing event hooks for ${Event[event]}`));
2426
scripts.forEach((script) => {
25-
Utilities.executeShellCommand(script,
26-
process.cwd(),
27-
process.env);
27+
try {
28+
Utilities.executeShellCommand(script,
29+
process.cwd(),
30+
process.env,
31+
true
32+
);
33+
} catch (error) {
34+
console.error(`${os.EOL} Event hook "${script}" failed. Run "rush" with --debug` +
35+
` to see detailed error information.`);
36+
if (isDebug) {
37+
console.error(os.EOL + error.message);
38+
}
39+
}
2840
});
29-
console.log(os.EOL + colors.green(`Event hooks finished successfully`));
41+
stopwatch.stop();
42+
console.log(os.EOL + colors.green(`Event hooks finished. (${stopwatch.toString()})`));
3043
}
3144
}
3245
}

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ 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[];
@@ -45,19 +47,55 @@ export default class Telemetry {
4547
}
4648

4749
public flush(writeFile: (file: string, data: string) => void = fsx.writeFileSync): void {
48-
if (!this._enabled) {
50+
if (!this._enabled || this._store.length === 0) {
4951
return;
5052
}
53+
5154
const fullPath: string = this._getFilePath();
5255
fsx.ensureDirSync(this._dataFolder);
5356
writeFile(fullPath, JSON.stringify(this._store));
5457
this._store = [];
58+
this._cleanUp();
5559
}
5660

5761
public get store(): ITelemetryData[] {
5862
return this._store;
5963
}
6064

65+
/**
66+
* When there are too many log files, delete the old ones.
67+
*/
68+
private _cleanUp(): void {
69+
if (fsx.existsSync(this._dataFolder)) {
70+
const files: string[] = fsx.readdirSync(this._dataFolder);
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);
75+
return {
76+
filePath: filePath,
77+
modifiedTime: stats.mtime.getTime(),
78+
isFile: stats.isFile()
79+
};
80+
})
81+
.filter(value => {
82+
// Only delete files
83+
return value.isFile;
84+
})
85+
.sort((a, b) => {
86+
return a.modifiedTime - b.modifiedTime;
87+
})
88+
.map(s => {
89+
return s.filePath;
90+
});
91+
const filesToDelete: number = sortedFiles.length - MAX_FILE_COUNT;
92+
for (let i: number = 0; i < filesToDelete; i++) {
93+
fsx.unlinkSync(sortedFiles[i]);
94+
}
95+
}
96+
}
97+
}
98+
6199
private _getFilePath(): string {
62100
let fileName: string = `telemetry_${new Date().toISOString()}`;
63101
fileName = fileName.replace(/[\-\:\.]/g, '_') + '.json';

0 commit comments

Comments
 (0)