forked from mtusi2026/mtusi2026lab5
Перенесено дерево lab4 целиком: NestJS-приложение с вложениями и S3,
Dockerfile, compose.yaml, infra/{haproxy,minio}, стендовые скрипты,
scripts/lab4, evidence/lab4 и contracts/extensions.openapi.json.
Задание lab5 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,examples},
tests, compose.example.yaml, .github/pull_request_template.md.
Адаптации при переносе:
- docker-проект mtusi-lab4 -> mtusi-lab5 и тег образа mtusi-messenger:lab5,
чтобы тома и контейнеры не пересекались с lab4 (scripts/stop-one.sh:
LAB_PROJECT вместо LAB4_PROJECT);
- Makefile: цели lab5 сохранены, добавлены стендовые (stand-up/down,
wait-ready, init-env, test-e2e, test-dependencies, s3-security,
consistency, bench);
- .env.example: реальные переменные приложения и стенда lab4 (без них
compose.yaml не поднимается) + блок lab5 (BROKER_URL_FILE, WORKER_*).
79 lines
2.3 KiB
TypeScript
79 lines
2.3 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 {
|
|
await this.storage.delete(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 };
|
|
}
|
|
}
|