43 lines
1.7 KiB
TypeScript
43 lines
1.7 KiB
TypeScript
import { fileURLToPath } from 'node:url';
|
|
import { parseAppConfig } from '../config/app-config.schema.js';
|
|
import { DatabaseClient } from '../database/database.client.js';
|
|
import { MetricsService } from '../metrics/metrics.service.js';
|
|
import { ObjectStorage } from '../storage/object-storage.js';
|
|
import { AttachmentSweeper } from './attachment-sweeper.service.js';
|
|
import { AttachmentsRepository } from './attachments.repository.js';
|
|
|
|
// Manual operator pass over unfinished uploads (the API also sweeps in the
|
|
// background). Never touches rows before expiry + ATTACHMENT_SWEEP_GRACE_SECONDS.
|
|
// in the stand: docker compose exec api node dist/attachments/attachments-sweep.cli.js
|
|
// on the host: npm run attachments:sweep (needs reachable DATABASE_URL and S3)
|
|
async function main(): Promise<void> {
|
|
const config = parseAppConfig(process.env as Record<string, unknown>);
|
|
const configService = {
|
|
getOrThrow: (key: keyof typeof config) => config[key],
|
|
} as never;
|
|
const metrics = new MetricsService();
|
|
const database = new DatabaseClient(configService, metrics);
|
|
try {
|
|
const sweeper = new AttachmentSweeper(
|
|
configService,
|
|
new AttachmentsRepository(database),
|
|
new ObjectStorage(configService),
|
|
metrics,
|
|
);
|
|
let total = 0;
|
|
for (;;) {
|
|
const { claimed, deleted } = await sweeper.sweepOnce();
|
|
total += deleted;
|
|
// A pass that claimed rows but could not delete any would loop forever.
|
|
if (claimed === 0 || deleted === 0) break;
|
|
}
|
|
process.stdout.write(
|
|
`Swept ${total} attachment(s) older than ${config.ATTACHMENT_SWEEP_GRACE_SECONDS} seconds past expiry\n`,
|
|
);
|
|
} finally {
|
|
await database.onModuleDestroy();
|
|
}
|
|
}
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url)) await main();
|