Skip to content

Commit 9603259

Browse files
authored
Add Exception Handling to File Name for Telemetry Caching (#1267)
* Add process ID to created file name for logs caching. * Test * Update file cleanup to only clean files linked to the PID. * Update EndToEnd.tests.ts * Update error handling in the file system helper. * Update FileSystemHelper.ts * Address PR comments. * Remove PID. * Update EndToEnd.tests.ts
1 parent b952bcf commit 9603259

File tree

4 files changed

+131
-81
lines changed

4 files changed

+131
-81
lines changed

Library/FileSystemHelper.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from "fs";
22
import path = require("path");
33
import { promisify } from "util";
4+
import Logging = require("./Logging");
45

56
export const statAsync = promisify(fs.stat);
67
export const lstatAsync = promisify(fs.lstat);
@@ -39,15 +40,19 @@ export const confirmDirExists = async (directory: string): Promise<void> => {
3940
* Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.
4041
*/
4142
export const getShallowDirectorySize = async (directory: string): Promise<number> => {
42-
// Get the directory listing
43-
const files = await readdirAsync(directory);
4443
let totalSize = 0;
45-
// Query all file sizes
46-
for (const file of files) {
47-
const fileStats = await statAsync(path.join(directory, file));
48-
if (fileStats.isFile()) {
49-
totalSize += fileStats.size;
44+
try {
45+
// Get the directory listing
46+
const files = await readdirAsync(directory);
47+
// Query all file sizes
48+
for (const file of files) {
49+
const fileStats = await statAsync(path.join(directory, file));
50+
if (fileStats.isFile()) {
51+
totalSize += fileStats.size;
52+
}
5053
}
54+
} catch {
55+
Logging.warn(`Failed to get directory size for ${directory}`);
5156
}
5257
return totalSize;
5358
};
@@ -56,21 +61,30 @@ export const getShallowDirectorySize = async (directory: string): Promise<number
5661
* Computes the size (in bytes) of all files in a directory at the root level. Synchronously.
5762
*/
5863
export const getShallowDirectorySizeSync = (directory: string): number => {
59-
let files = fs.readdirSync(directory);
6064
let totalSize = 0;
61-
for (let i = 0; i < files.length; i++) {
62-
totalSize += fs.statSync(path.join(directory, files[i])).size;
65+
try {
66+
let files = fs.readdirSync(directory);
67+
for (let i = 0; i < files.length; i++) {
68+
totalSize += fs.statSync(path.join(directory, files[i])).size;
69+
}
70+
} catch {
71+
Logging.warn(`Failed to get directory size synchronously for ${directory}`)
6372
}
64-
return totalSize;
73+
return totalSize
6574
}
6675

6776
/**
68-
* Computes the size (in bytes) of a file asynchronously.
77+
* Computes the size (in bytes) of a file asynchronously. Returns -1 if the file does not exist.
6978
*/
7079
export const getShallowFileSize = async (filePath: string): Promise<number> => {
71-
const fileStats = await statAsync(filePath);
72-
if (fileStats.isFile()) {
73-
return fileStats.size;
80+
try {
81+
const fileStats = await statAsync(filePath);
82+
if (fileStats.isFile()) {
83+
return fileStats.size;
84+
}
85+
} catch {
86+
Logging.warn(`Failed to get file size for ${filePath}`);
87+
return -1;
7488
}
7589
}
7690

Library/Sender.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ class Sender {
428428
try {
429429
//create file - file name for now is the timestamp, a better approach would be a UUID but that
430430
//would require an external dependency
431-
var fileName = new Date().getTime() + ".ai.json";
431+
var fileName = `${new Date().getTime()}.ai.json`;
432432
var fileFullPath = path.join(this._tempDir, fileName);
433433

434434
// Mode 600 is w/r for creator and no read access for others (only applies on *nix)
@@ -465,7 +465,7 @@ class Sender {
465465

466466
//create file - file name for now is the timestamp, a better approach would be a UUID but that
467467
//would require an external dependency
468-
var fileName = new Date().getTime() + ".ai.json";
468+
var fileName = `${new Date().getTime()}.ai.json`;
469469
var fileFullPath = path.join(this._tempDir, fileName);
470470

471471
// Mode 600 is w/r for creator and no access for anyone else (only applies on *nix)

Tests/EndToEnd.tests.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { JsonConfig } from "../Library/JsonConfig";
2323
import { FileAccessControl } from "../Library/FileAccessControl";
2424
import FileSystemHelper = require("../Library/FileSystemHelper");
2525
import AutoCollectHttpRequests = require("../AutoCollection/HttpRequests");
26-
2726
/**
2827
* A fake response class that passes by default
2928
*/

0 commit comments

Comments
 (0)