forked from mtusi2026/mtusi2026lab6
Перенесено дерево 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.
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import {
|
|
Injectable,
|
|
Logger,
|
|
OnModuleDestroy,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import type { AppConfig } from '../config/app-config.schema.js';
|
|
import { MetricsService } from '../metrics/metrics.service.js';
|
|
import { ObjectStorage } from '../storage/object-storage.js';
|
|
import { AttachmentsRepository } from './attachments.repository.js';
|
|
|
|
const SWEEP_BATCH = 100;
|
|
|
|
/**
|
|
* Removes unfinished uploads (pending past expiry + grace) and rows whose
|
|
* deletion was claimed but never finished. A live upload is never touched:
|
|
* its expiry is a hard request deadline and the grace period is far longer.
|
|
*/
|
|
@Injectable()
|
|
export class AttachmentSweeper implements OnModuleInit, OnModuleDestroy {
|
|
private readonly logger = new Logger(AttachmentSweeper.name);
|
|
private timer: NodeJS.Timeout | undefined;
|
|
private running = false;
|
|
|
|
constructor(
|
|
private readonly config: ConfigService<AppConfig>,
|
|
private readonly repository: AttachmentsRepository,
|
|
private readonly storage: ObjectStorage,
|
|
private readonly metrics: MetricsService,
|
|
) {}
|
|
|
|
onModuleInit(): void {
|
|
const interval = this.config.getOrThrow('ATTACHMENT_SWEEP_INTERVAL_MS');
|
|
if (interval <= 0) return;
|
|
this.timer = setInterval(() => {
|
|
if (this.running) return;
|
|
this.running = true;
|
|
this.sweepOnce()
|
|
.catch((error: unknown) => {
|
|
this.logger.warn(
|
|
`Sweep failed: ${error instanceof Error ? error.message : 'unknown error'}`,
|
|
);
|
|
})
|
|
.finally(() => {
|
|
this.running = false;
|
|
});
|
|
}, interval);
|
|
this.timer.unref();
|
|
}
|
|
|
|
onModuleDestroy(): void {
|
|
if (this.timer) clearInterval(this.timer);
|
|
this.timer = undefined;
|
|
}
|
|
|
|
async sweepOnce(): Promise<{ claimed: number; deleted: number }> {
|
|
const claimed = await this.repository.claimExpired(
|
|
this.config.getOrThrow('ATTACHMENT_SWEEP_GRACE_SECONDS'),
|
|
SWEEP_BATCH,
|
|
);
|
|
let deleted = 0;
|
|
for (const item of claimed) {
|
|
try {
|
|
// New uploads live in staging; lab4 rows have no staging key and
|
|
// their final object is the only copy.
|
|
await this.storage.delete(item.staging_key ?? item.object_key);
|
|
await this.repository.deleteClaimed(item.id);
|
|
deleted += 1;
|
|
this.metrics.increment('attachments.sweep.deleted');
|
|
} catch (error) {
|
|
this.metrics.increment('attachments.sweep.error');
|
|
this.logger.warn(
|
|
`Sweep of attachment ${item.id} failed: ${error instanceof Error ? error.message : 'unknown error'}`,
|
|
);
|
|
}
|
|
}
|
|
return { claimed: claimed.length, deleted };
|
|
}
|
|
}
|