Browse Source

Graceful shutdown of the server

pull/8035/head
Thomas Kaul 2 weeks ago
parent
commit
84378565f1
  1. 10
      apps/api/src/main.ts
  2. 12
      apps/api/src/services/prisma/prisma.service.ts
  3. 2
      docker/docker-compose.yml

10
apps/api/src/main.ts

@ -14,6 +14,7 @@ import {
import { import {
Logger, Logger,
LogLevel, LogLevel,
ShutdownSignal,
ValidationPipe, ValidationPipe,
VersioningType VersioningType
} from '@nestjs/common'; } from '@nestjs/common';
@ -66,9 +67,12 @@ async function bootstrap() {
app.enableCors(); app.enableCors();
// Let the open requests and the active queue jobs finish on SIGTERM before // Let the open requests and the active queue jobs finish on SIGINT and
// the process stops // SIGTERM before the process stops. Exit explicitly because Linux ignores
app.enableShutdownHooks(); // the re-raised signal if the process runs as PID 1
app.enableShutdownHooks([ShutdownSignal.SIGINT, ShutdownSignal.SIGTERM], {
useProcessExit: true
});
app.enableVersioning({ app.enableVersioning({
defaultVersion: '1', defaultVersion: '1',

12
apps/api/src/services/prisma/prisma.service.ts

@ -2,7 +2,7 @@ import {
Injectable, Injectable,
Logger, Logger,
LogLevel, LogLevel,
OnModuleDestroy, OnApplicationShutdown,
OnModuleInit OnModuleInit
} from '@nestjs/common'; } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
@ -12,7 +12,7 @@ import { Prisma, PrismaClient } from '@prisma/client';
@Injectable() @Injectable()
export class PrismaService export class PrismaService
extends PrismaClient extends PrismaClient
implements OnModuleInit, OnModuleDestroy implements OnApplicationShutdown, OnModuleInit
{ {
private readonly logger = new Logger(PrismaService.name); private readonly logger = new Logger(PrismaService.name);
@ -41,6 +41,10 @@ export class PrismaService
}); });
} }
public async onApplicationShutdown() {
await this.$disconnect();
}
public async onModuleInit() { public async onModuleInit() {
try { try {
await this.$connect(); await this.$connect();
@ -48,8 +52,4 @@ export class PrismaService
this.logger.error(error); this.logger.error(error);
} }
} }
public async onModuleDestroy() {
await this.$disconnect();
}
} }

2
docker/docker-compose.yml

@ -5,8 +5,6 @@ services:
container_name: ghostfolio container_name: ghostfolio
restart: unless-stopped restart: unless-stopped
init: true init: true
# Time for the open requests and the active queue jobs to finish before
# the container is killed
stop_grace_period: 1m stop_grace_period: 1m
cap_drop: cap_drop:
- ALL - ALL

Loading…
Cancel
Save