From 84378565f1a1c2c6b9ecf118ced822190f5d73a9 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 4 Oct 2026 11:01:02 +0200 Subject: [PATCH] Graceful shutdown of the server --- apps/api/src/main.ts | 10 +++++++--- apps/api/src/services/prisma/prisma.service.ts | 12 ++++++------ docker/docker-compose.yml | 2 -- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 4fa764632e..1282304c33 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -14,6 +14,7 @@ import { import { Logger, LogLevel, + ShutdownSignal, ValidationPipe, VersioningType } from '@nestjs/common'; @@ -66,9 +67,12 @@ async function bootstrap() { app.enableCors(); - // Let the open requests and the active queue jobs finish on SIGTERM before - // the process stops - app.enableShutdownHooks(); + // Let the open requests and the active queue jobs finish on SIGINT and + // SIGTERM before the process stops. Exit explicitly because Linux ignores + // the re-raised signal if the process runs as PID 1 + app.enableShutdownHooks([ShutdownSignal.SIGINT, ShutdownSignal.SIGTERM], { + useProcessExit: true + }); app.enableVersioning({ defaultVersion: '1', diff --git a/apps/api/src/services/prisma/prisma.service.ts b/apps/api/src/services/prisma/prisma.service.ts index df55e45f7c..fcf7c6e5ea 100644 --- a/apps/api/src/services/prisma/prisma.service.ts +++ b/apps/api/src/services/prisma/prisma.service.ts @@ -2,7 +2,7 @@ import { Injectable, Logger, LogLevel, - OnModuleDestroy, + OnApplicationShutdown, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @@ -12,7 +12,7 @@ import { Prisma, PrismaClient } from '@prisma/client'; @Injectable() export class PrismaService extends PrismaClient - implements OnModuleInit, OnModuleDestroy + implements OnApplicationShutdown, OnModuleInit { private readonly logger = new Logger(PrismaService.name); @@ -41,6 +41,10 @@ export class PrismaService }); } + public async onApplicationShutdown() { + await this.$disconnect(); + } + public async onModuleInit() { try { await this.$connect(); @@ -48,8 +52,4 @@ export class PrismaService this.logger.error(error); } } - - public async onModuleDestroy() { - await this.$disconnect(); - } } diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 091cbd04d6..cf58ea0143 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -5,8 +5,6 @@ services: container_name: ghostfolio restart: unless-stopped init: true - # Time for the open requests and the active queue jobs to finish before - # the container is killed stop_grace_period: 1m cap_drop: - ALL