Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend): improve connection checks and bootstrap resilience
johaven8c140d94fe14
diff --git a/backend/src/app.service.spec.ts b/backend/src/app.service.spec.tsindex aa058769..1dac417b 100644--- a/backend/src/app.service.spec.ts+++ b/backend/src/app.service.spec.ts@@ -30,7 +30,7 @@ describe(AppService.name, () => {configuration.websocket.adapter = 'cluster'configuration.server.restartOnFailure = true- const bootstrap = vi.fn()+ const bootstrap = vi.fn().mockResolvedValue(undefined)// IMPORTANT: do NOT call bootstrap() from fork mockconst fakeWorker = { process: { pid: 1 } } as anydiff --git a/backend/src/app.service.ts b/backend/src/app.service.tsindex 9588898f..611cf5a8 100755--- a/backend/src/app.service.ts+++ b/backend/src/app.service.ts@@ -5,13 +5,15 @@ import { cpus } from 'node:os'import process from 'node:process'import { configuration } from './configuration/config.environment'import { SCHEDULER_ENV, SCHEDULER_STATE } from './infrastructure/scheduler/scheduler.constants'+import { SERVER_NAME } from './common/shared'@Injectable()export class AppService {- private static readonly logger = new Logger(AppService.name)static schedulerPID: number+ private static readonly logger = new Logger('SERVER')- static clusterize(bootstrap: () => void) {+ static clusterize(bootstrap: () => Promise<void>) {+ AppService.logger.localInstance['options']['prefix'] = SERVER_NAMEif (cluster.isPrimary) {if (configuration.websocket.adapter === 'cluster') {// setup connections between the workers@@ -32,7 +34,7 @@ export class AppService {})} else {AppService.logger.log(`[Worker:${process.pid}] started`)- bootstrap()+ bootstrap().catch(() => process.exit(1))}}diff --git a/backend/src/infrastructure/database/database.module.ts b/backend/src/infrastructure/database/database.module.tsindex 9ce45b29..829f3ef6 100644--- a/backend/src/infrastructure/database/database.module.ts+++ b/backend/src/infrastructure/database/database.module.ts@@ -30,12 +30,20 @@ import * as schema from './schema'export class DatabaseModule implements OnModuleInit, BeforeApplicationShutdown {constructor(@Inject(DB_TOKEN_PROVIDER) private readonly db: DBSchema & { session: { client: MySql2Client } }) {}- onModuleInit() {+ async onModuleInit() {const pool: Pool = (this.db as any).$clientpool.on('connection', (conn: Connection) => {// Force UTC timezone for every new MySQL connection.conn.query(`SET time_zone = '+00:00'`)})++ // Ensure MySQL connection is healthy+ try {+ await pool.promise().query('SELECT 1')+ } catch (error) {+ await this.db.session.client.end()+ throw error+ }}async beforeApplicationShutdown() {diff --git a/backend/src/main.ts b/backend/src/main.tsindex 66443472..1cad97da 100755--- a/backend/src/main.ts+++ b/backend/src/main.ts@@ -1,32 +1,41 @@#!/usr/bin/env nodeimport { NestFastifyApplication } from '@nestjs/platform-fastify'import { Logger } from 'nestjs-pino'+import { setTimeout } from 'node:timers/promises'import { appBootstrap } from './app.bootstrap'import { AppService } from './app.service'import { configuration } from './configuration/config.environment'async function bootstrap() {- const app: NestFastifyApplication = await appBootstrap()- const logger: Logger = app.get<Logger>(Logger)- await app.listen(- {- host: configuration.server.host,- port: configuration.server.port- },- (error, address) => {- if (configuration.server.host === '0.0.0.0') {- const url = new URL(address)- url.hostname = '0.0.0.0'- address = url.toString()+ let logger: Logger+ try {+ const app: NestFastifyApplication = await appBootstrap()+ logger = app.get<Logger>(Logger)+ await app.listen(+ {+ host: configuration.server.host,+ port: configuration.server.port+ },+ (error, address) => {+ if (configuration.server.host === '0.0.0.0') {+ const url = new URL(address)+ url.hostname = '0.0.0.0'+ address = url.toString()+ }+ if (error) {+ logger.error(`Server listening error at ${address} : ${error}`, 'HTTP')+ process.exit(1)+ } else {+ logger.log(`Server listening at ${address}`, 'HTTP')+ }}- if (error) {- logger.error(`Server listening error at ${address} : ${error}`, 'HTTP')- process.exit(1)- } else {- logger.log(`Server listening at ${address}`, 'HTTP')- }- }- )+ )+ } catch (e) {+ logger.error(`Bootstrap failed: ${e?.errors || e}`, 'BOOTSTRAP')+ logger.error(`${configuration.server.restartOnFailure ? 'Retrying' : 'Exiting'} ...`, 'BOOTSTRAP')+ await setTimeout(6000)+ throw e+ }}AppService.clusterize(bootstrap)