Files
mtusi2026lab6/app/src/attachments/attachments-sweep.cli.ts
T
ilya.tyunin 6889e77ab4 lab6: базовый коммит из lab5 (0790599)
Перенесено дерево lab5 целиком: NestJS-приложение (API + worker,
RabbitMQ, outbox, S3), Dockerfile, compose.yaml, infra/{haproxy,minio,
rabbitmq}, стендовые скрипты, scripts/lab4, scripts/lab5,
evidence/lab4, evidence/lab5 и contracts/extensions.openapi.json.

Задание lab6 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,
examples,observability}, tests, compose.example.yaml, infra/otel,
.github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab5 -> mtusi-lab6 и тег образа
  mtusi-messenger:lab6 (compose.yaml, scripts/stop-one.sh, .env.example,
  infra/README.md), чтобы тома и контейнеры не пересекались с lab5;
- Makefile: цели lab5 сохранены целиком (skeleton lab6 не отличается
  от skeleton lab5);
- .env.example: переменные приложения и стенда lab5 + блок lab6
  (OTEL_*) из skeleton.
2026-11-09 16:44:21 +03:00

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();