chore: port lab4 solution as lab5 base

This commit is contained in:
2026-11-15 14:42:12 +03:00
parent 276dbdad8a
commit 19dda6e286
90 changed files with 18537 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
import { Controller, Get } from '@nestjs/common';
import { ApiError } from '../common/filters/api-error';
import { PrismaService } from '../db/prisma.service';
import { S3Service } from '../storage/s3.service';
import { isDraining } from './shutdown';
@Controller('health')
export class HealthController {
constructor(
private readonly prisma: PrismaService,
private readonly s3: S3Service,
) {}
@Get('live')
live(): { status: 'ok' } {
return { status: 'ok' };
}
@Get('core')
async core(): Promise<{ status: 'ok' }> {
await this.assertCoreReady();
return { status: 'ok' };
}
@Get('ready')
async ready(): Promise<{ status: 'ok' }> {
await this.assertCoreReady();
if (!(await this.s3.isAvailable())) {
throw new ApiError('unavailable', 'object storage unavailable');
}
return { status: 'ok' };
}
private async assertCoreReady(): Promise<void> {
if (isDraining()) {
throw new ApiError('unavailable', 'shutting down');
}
try {
await this.prisma.$queryRaw`SELECT 1`;
} catch {
throw new ApiError('unavailable', 'database unavailable');
}
}
}
+7
View File
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { HealthController } from './health.controller';
@Module({
controllers: [HealthController],
})
export class HealthModule {}
+26
View File
@@ -0,0 +1,26 @@
import { INestApplication, Logger } from '@nestjs/common';
let draining = false;
export function isDraining(): boolean {
return draining;
}
export function enableGracefulShutdown(app: INestApplication): void {
const delayMs = Number(process.env.SHUTDOWN_DELAY_MS ?? 3000);
const shutdown = (signal: string): void => {
if (draining) {
return;
}
draining = true;
Logger.log(`${signal}: readiness → 503, closing in ${delayMs} ms`, 'Shutdown');
setTimeout(() => {
app.close().then(
() => process.exit(0),
() => process.exit(1),
);
}, delayMs);
};
process.once('SIGTERM', () => shutdown('SIGTERM'));
process.once('SIGINT', () => shutdown('SIGINT'));
}