Skip to content

Commit

Permalink
fix: prune invalid ssz objects (#6875)
Browse files Browse the repository at this point in the history
* fix: prune invalid ssz objects

* fix: handle non-existing invalidSszObjects folder

* Review PR

* fix: expect empty dirs to be removed in unit tests

---------

Co-authored-by: Nico Flaig <nflaig@protonmail.com>
  • Loading branch information
twoeths and nflaig authored Jun 19, 2024
1 parent f37e50f commit 85dc0ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
30 changes: 16 additions & 14 deletions packages/cli/src/cmds/beacon/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,22 @@ export async function beaconHandler(args: BeaconArgs & GlobalArgs): Promise<void
}

// Prune invalid SSZ objects every interval
const {persistInvalidSszObjectsDir} = args;
const pruneInvalidSSZObjectsInterval = persistInvalidSszObjectsDir
? setInterval(() => {
try {
pruneOldFilesInDir(
persistInvalidSszObjectsDir,
(args.persistInvalidSszObjectsRetentionHours ?? DEFAULT_RETENTION_SSZ_OBJECTS_HOURS) * HOURS_TO_MS
);
} catch (e) {
logger.warn("Error pruning invalid SSZ objects", {persistInvalidSszObjectsDir}, e as Error);
}
// Run every ~1 hour
}, HOURS_TO_MS)
: null;
const {persistInvalidSszObjectsDir, persistInvalidSszObjects} = options.chain;
const pruneInvalidSSZObjectsInterval =
persistInvalidSszObjectsDir && persistInvalidSszObjects
? setInterval(() => {
try {
const deletedFileCount = pruneOldFilesInDir(
persistInvalidSszObjectsDir,
(args.persistInvalidSszObjectsRetentionHours ?? DEFAULT_RETENTION_SSZ_OBJECTS_HOURS) * HOURS_TO_MS
);
logger.info("Pruned invalid SSZ objects", {deletedFileCount});
} catch (e) {
logger.warn("Error pruning invalid SSZ objects", {persistInvalidSszObjectsDir}, e as Error);
}
// Run every ~1 hour
}, HOURS_TO_MS)
: null;

// Intercept SIGINT signal, to perform final ops before exiting
onGracefulShutdown(async () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/util/pruneOldFilesInDir.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import fs from "node:fs";
import path from "node:path";

export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): void {
export function pruneOldFilesInDir(dirpath: string, maxAgeMs: number): number {
let deletedFileCount = 0;
for (const entryName of fs.readdirSync(dirpath)) {
const entryPath = path.join(dirpath, entryName);

const stat = fs.statSync(entryPath);
if (stat.isDirectory()) {
pruneOldFilesInDir(entryPath, maxAgeMs);
deletedFileCount += pruneOldFilesInDir(entryPath, maxAgeMs);
} else if (stat.isFile()) {
if (Date.now() - stat.mtimeMs > maxAgeMs) {
fs.unlinkSync(entryPath);
deletedFileCount += 1;
}
}
}

// if all files are deleted, delete the directory
if (fs.readdirSync(dirpath).length === 0) {
fs.rmdirSync(dirpath);
}
return deletedFileCount;
}
4 changes: 2 additions & 2 deletions packages/cli/test/unit/util/pruneOldFilesInDir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("pruneOldFilesInDir", () => {

pruneOldFilesInDir(dataDir, DAYS_TO_MS);

expect(fs.readdirSync(nestedDir)).toHaveLength(0);
expect(fs.existsSync(nestedDir)).toBe(false);
});

it("should handle empty directories", () => {
Expand All @@ -52,7 +52,7 @@ describe("pruneOldFilesInDir", () => {

pruneOldFilesInDir(emptyDir, DAYS_TO_MS);

expect(fs.readdirSync(emptyDir)).toHaveLength(0);
expect(fs.existsSync(emptyDir)).toBe(false);
});

function createFileWithAge(path: string, ageInDays: number): void {
Expand Down

0 comments on commit 85dc0ba

Please sign in to comment.