feat(pockethost): add compaction health check

This commit is contained in:
Ben Allfree 2024-08-11 12:36:55 +00:00
parent daa75028c9
commit 1395412382
3 changed files with 46 additions and 11 deletions

View File

@ -32,9 +32,14 @@ module.exports = {
script: 'pnpm prod:cli pocketbase update',
},
{
name: `health`,
name: `health-check`,
restart_delay: 60 * 1000, // 1 minute
script: 'pnpm prod:cli health',
script: 'pnpm prod:cli health check',
},
{
name: `health-compact`,
restart_delay: 60 * 1000, // 1 minute
script: 'pnpm prod:cli health compact',
},
],
}

View File

@ -0,0 +1,15 @@
import { exec } from 'child_process'
import { globSync } from 'glob'
import { DATA_ROOT } from '../../../../core'
export const compact = async () => {
const files = [`data`, `logs`].flatMap((db) =>
globSync(`${DATA_ROOT()}/*/pb_data/${db}.db{-shm,-wal}`),
)
files.map(async (file) => {
console.log(`Compacting ${file}`)
exec(`sqlite3 ${file} ".tables"`)
})
console.log(`Compaction complete`)
}

View File

@ -1,6 +1,5 @@
import { Command } from 'commander'
import { LoggerService } from '../../../../core'
import { checkHealth } from './checkHealth'
type Options = {
debug: boolean
@ -8,13 +7,29 @@ type Options = {
export const HealthCommand = () => {
const cmd = new Command(`health`)
.description(`Perform a health check on the PocketHost system`)
.action(async (options: Options) => {
const logger = LoggerService().create(`HealthCommand`)
const { dbg, error, info, warn } = logger
info(`Starting`)
await checkHealth()
})
.addCommand(
new Command(`check`)
.description(`Perform a health check on the PocketHost system`)
.action(async (options: Options) => {
const logger = LoggerService().create(`HealthCommand`)
const { dbg, error, info, warn } = logger
info(`Starting`)
const { checkHealth } = await import(`./checkHealth`)
await checkHealth()
}),
)
.addCommand(
new Command(`compact`)
.description(
`Compact SQLite databases by removing old SHM and WAL files`,
)
.action(async (options: Options) => {
const logger = LoggerService().create(`HealthCommand`)
const { dbg, error, info, warn } = logger
info(`Starting`)
const { compact } = await import(`./compact`)
await compact()
}),
)
return cmd
}