Skip to content

Commit

Permalink
Add Exception Handling to File Name for Telemetry Caching (#1267)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JacksonWeber committed Feb 2, 2024
1 parent b952bcf commit 9603259
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 81 deletions.
44 changes: 29 additions & 15 deletions Library/FileSystemHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import path = require("path");
import { promisify } from "util";
import Logging = require("./Logging");

export const statAsync = promisify(fs.stat);
export const lstatAsync = promisify(fs.lstat);
Expand Down Expand Up @@ -39,15 +40,19 @@ export const confirmDirExists = async (directory: string): Promise<void> => {
* Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.
*/
export const getShallowDirectorySize = async (directory: string): Promise<number> => {
// Get the directory listing
const files = await readdirAsync(directory);
let totalSize = 0;
// Query all file sizes
for (const file of files) {
const fileStats = await statAsync(path.join(directory, file));
if (fileStats.isFile()) {
totalSize += fileStats.size;
try {
// Get the directory listing
const files = await readdirAsync(directory);
// Query all file sizes
for (const file of files) {
const fileStats = await statAsync(path.join(directory, file));
if (fileStats.isFile()) {
totalSize += fileStats.size;
}
}
} catch {
Logging.warn(`Failed to get directory size for ${directory}`);
}
return totalSize;
};
Expand All @@ -56,21 +61,30 @@ export const getShallowDirectorySize = async (directory: string): Promise<number
* Computes the size (in bytes) of all files in a directory at the root level. Synchronously.
*/
export const getShallowDirectorySizeSync = (directory: string): number => {
let files = fs.readdirSync(directory);
let totalSize = 0;
for (let i = 0; i < files.length; i++) {
totalSize += fs.statSync(path.join(directory, files[i])).size;
try {
let files = fs.readdirSync(directory);
for (let i = 0; i < files.length; i++) {
totalSize += fs.statSync(path.join(directory, files[i])).size;
}
} catch {
Logging.warn(`Failed to get directory size synchronously for ${directory}`)
}
return totalSize;
return totalSize
}

/**
* Computes the size (in bytes) of a file asynchronously.
* Computes the size (in bytes) of a file asynchronously. Returns -1 if the file does not exist.
*/
export const getShallowFileSize = async (filePath: string): Promise<number> => {
const fileStats = await statAsync(filePath);
if (fileStats.isFile()) {
return fileStats.size;
try {
const fileStats = await statAsync(filePath);
if (fileStats.isFile()) {
return fileStats.size;
}
} catch {
Logging.warn(`Failed to get file size for ${filePath}`);
return -1;
}
}

4 changes: 2 additions & 2 deletions Library/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class Sender {
try {
//create file - file name for now is the timestamp, a better approach would be a UUID but that
//would require an external dependency
var fileName = new Date().getTime() + ".ai.json";
var fileName = `${new Date().getTime()}.ai.json`;
var fileFullPath = path.join(this._tempDir, fileName);

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

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

// Mode 600 is w/r for creator and no access for anyone else (only applies on *nix)
Expand Down
1 change: 0 additions & 1 deletion Tests/EndToEnd.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { JsonConfig } from "../Library/JsonConfig";
import { FileAccessControl } from "../Library/FileAccessControl";
import FileSystemHelper = require("../Library/FileSystemHelper");
import AutoCollectHttpRequests = require("../AutoCollection/HttpRequests");

/**
* A fake response class that passes by default
*/
Expand Down

0 comments on commit 9603259

Please sign in to comment.