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`) }