From a0df601f967a7516190f66d864ac2137862c7415 Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Sat, 9 Nov 2024 15:58:51 +0000 Subject: [PATCH] fix: db compaction --- .../src/cli/commands/HealthCommand/compact.ts | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/pockethost/src/cli/commands/HealthCommand/compact.ts b/packages/pockethost/src/cli/commands/HealthCommand/compact.ts index 356dc9f9..ee9ef46e 100644 --- a/packages/pockethost/src/cli/commands/HealthCommand/compact.ts +++ b/packages/pockethost/src/cli/commands/HealthCommand/compact.ts @@ -1,18 +1,34 @@ -import { exec } from 'child_process' +import { execSync } from 'child_process' import { globSync } from 'glob' import { DATA_ROOT } from '../../../../core' import { logger } from '../../../common/Logger' export const compact = async () => { - const { info } = logger() + const { info, error } = logger() - const files = [`data`, `logs`].flatMap((db) => - globSync(`${DATA_ROOT()}/*/pb_data/${db}.db{-shm,-wal}`), - ) + const files = [ + ...new Set( + [`data`, `logs`].flatMap((db) => + globSync(`${DATA_ROOT()}/*/pb_data/${db}.db{-shm,-wal}`).map((f) => + f.replace(/-(?:shm|wal)$/, ''), + ), + ), + ), + ] - files.map(async (file) => { - info(`Compacting ${file}`) - exec(`sqlite3 ${file} ".tables"`) + info(`Compacting ${files.length} files`, { files }) + + files.forEach((file) => { + const { info, error } = logger().child(file) + const cmd = `sqlite3 ${file} ".tables"` + info(cmd) + try { + execSync(cmd) + info(`Finished compacting`) + } catch (e) { + error(`Error compacting`, e) + } }) + // console.log('Compaction complete') info(`Compaction complete`) }