Merge pull request #2644 from github/robertbrignull/fix_alert_384
Fix alert by rewriting query history scrubbed to do fewer file operations
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { pathExists, stat, remove, readFile } from "fs-extra";
|
||||
import { pathExists, remove, readFile } from "fs-extra";
|
||||
import { EOL } from "os";
|
||||
import { join } from "path";
|
||||
import { Disposable, ExtensionContext } from "vscode";
|
||||
@@ -6,6 +6,7 @@ import { extLogger } from "../common/logging/vscode";
|
||||
import { readDirFullPaths } from "../common/files";
|
||||
import { QueryHistoryDirs } from "./query-history-dirs";
|
||||
import { QueryHistoryManager } from "./query-history-manager";
|
||||
import { getErrorMessage } from "../common/helpers-pure";
|
||||
|
||||
const LAST_SCRUB_TIME_KEY = "lastScrubTime";
|
||||
|
||||
@@ -132,46 +133,55 @@ async function scrubDirectory(
|
||||
errorMsg?: string;
|
||||
deleted: boolean;
|
||||
}> {
|
||||
const timestampFile = join(dir, "timestamp");
|
||||
try {
|
||||
let deleted = true;
|
||||
if (!(await stat(dir)).isDirectory()) {
|
||||
void extLogger.log(` ${dir} is not a directory. Deleting.`);
|
||||
await remove(dir);
|
||||
} else if (!(await pathExists(timestampFile))) {
|
||||
void extLogger.log(` ${dir} has no timestamp file. Deleting.`);
|
||||
await remove(dir);
|
||||
} else if (!(await stat(timestampFile)).isFile()) {
|
||||
void extLogger.log(` ${timestampFile} is not a file. Deleting.`);
|
||||
if (await shouldScrubDirectory(dir, now, maxQueryTime)) {
|
||||
await remove(dir);
|
||||
return { deleted: true };
|
||||
} else {
|
||||
const timestampText = await readFile(timestampFile, "utf8");
|
||||
const timestamp = parseInt(timestampText, 10);
|
||||
|
||||
if (Number.isNaN(timestamp)) {
|
||||
void extLogger.log(
|
||||
` ${dir} has invalid timestamp '${timestampText}'. Deleting.`,
|
||||
);
|
||||
await remove(dir);
|
||||
} else if (now - timestamp > maxQueryTime) {
|
||||
void extLogger.log(
|
||||
` ${dir} is older than ${maxQueryTime / 1000} seconds. Deleting.`,
|
||||
);
|
||||
await remove(dir);
|
||||
} else {
|
||||
void extLogger.log(
|
||||
` ${dir} is not older than ${maxQueryTime / 1000} seconds. Keeping.`,
|
||||
);
|
||||
deleted = false;
|
||||
}
|
||||
return { deleted: false };
|
||||
}
|
||||
return {
|
||||
deleted,
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
errorMsg: ` Could not delete '${dir}': ${err}`,
|
||||
errorMsg: ` Could not delete '${dir}': ${getErrorMessage(err)}`,
|
||||
deleted: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function shouldScrubDirectory(
|
||||
dir: string,
|
||||
now: number,
|
||||
maxQueryTime: number,
|
||||
): Promise<boolean> {
|
||||
const timestamp = await getTimestamp(join(dir, "timestamp"));
|
||||
if (timestamp === undefined || Number.isNaN(timestamp)) {
|
||||
void extLogger.log(` ${dir} timestamp is missing or invalid. Deleting.`);
|
||||
return true;
|
||||
} else if (now - timestamp > maxQueryTime) {
|
||||
void extLogger.log(
|
||||
` ${dir} is older than ${maxQueryTime / 1000} seconds. Deleting.`,
|
||||
);
|
||||
return true;
|
||||
} else {
|
||||
void extLogger.log(
|
||||
` ${dir} is not older than ${maxQueryTime / 1000} seconds. Keeping.`,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function getTimestamp(
|
||||
timestampFile: string,
|
||||
): Promise<number | undefined> {
|
||||
try {
|
||||
const timestampText = await readFile(timestampFile, "utf8");
|
||||
return parseInt(timestampText, 10);
|
||||
} catch (err) {
|
||||
void extLogger.log(
|
||||
` Could not read timestamp file '${timestampFile}': ${getErrorMessage(
|
||||
err,
|
||||
)}`,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user