Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

fix: handle possible hang or freeze with 30s timeout.

towfiqia2b48930357a
diff --git a/backend/src/index.ts b/backend/src/index.ts
index a6e5ad4..0c315c2 100644
--- a/backend/src/index.ts
+++ b/backend/src/index.ts
@@ -21,60 +21,66 @@ import fs from 'fs';
const isMainModule = import.meta.url === `file://${process.argv[1]}` || (process as any).pkg;
const isDevelopment = process.env.NODE_ENV === 'development';
-console.log('isMainModule :', isMainModule, isDevelopment);
-
if (isMainModule || isDevelopment) {
(async () => {
// Run database migrations first
if (process.env.NODE_ENV === 'production') {
- console.log('[CORE] Running database migrations...');
- try {
- const migrationsFolder = process.env.IS_DOCKER === 'true' ? '/app/drizzle' : path.join(path.dirname(process.execPath), 'drizzle');
-
- // Check if migrations exist
- const journalPath = path.join(migrationsFolder, 'meta', '_journal.json');
- if (!fs.existsSync(journalPath)) {
- console.log('[CORE] No migrations found (meta/_journal.json missing), skipping...');
- } else {
- migrate(db, { migrationsFolder });
- console.log('[CORE] Database migrations completed successfully');
- }
- } catch (error: any) {
- // If tables already exist - skip migration
- if (
- error?.cause?.code === 'SQLITE_ERROR' &&
- error?.cause?.message?.includes('already exists')
- ) {
- console.log('[CORE] Database tables already exist, skipping migrations');
- } else {
- console.error('[CORE] Database migration error:', error);
- process.exit(1);
- }
+ console.log('[CORE] Running database migrations...');
+ try {
+ const migrationsFolder =
+ process.env.IS_DOCKER === 'true'
+ ? '/app/drizzle'
+ : path.join(path.dirname(process.execPath), 'drizzle');
+
+ // Check if migrations exist
+ const journalPath = path.join(migrationsFolder, 'meta', '_journal.json');
+ if (!fs.existsSync(journalPath)) {
+ console.log('[CORE] No migrations found (meta/_journal.json missing), skipping...');
+ } else {
+ migrate(db, { migrationsFolder });
+ console.log('[CORE] Database migrations completed successfully');
+ }
+ } catch (error: any) {
+ // If tables already exist - skip migration
+ if (
+ error?.cause?.code === 'SQLITE_ERROR' &&
+ error?.cause?.message?.includes('already exists')
+ ) {
+ console.log('[CORE] Database tables already exist, skipping migrations');
+ } else {
+ console.error('[CORE] Database migration error:', error);
+ process.exit(1);
}
}
+ }
- await initSetup(db as unknown as BetterSQLite3Database);
- const { app } = await createApp();
+ await initSetup(db as unknown as BetterSQLite3Database);
+ const { app } = await createApp();
- const server = app.listen(configService.config.SERVER_PORT || 5173, () => {
- console.log(`[CORE] Server running on port ${configService.config.SERVER_PORT || 5173}`);
- });
+ const server = app.listen(configService.config.SERVER_PORT || 5173, () => {
+ console.log(`[CORE] Server running on port ${configService.config.SERVER_PORT || 5173}`);
+ });
+
+ // Graceful shutdown handling for Docker
+ process.on('SIGTERM', async () => {
+ console.log('[CORE] SIGTERM received, shutting down gracefully...');
+ const forceExit = setTimeout(() => {
+ process.exit(1);
+ }, 30000); // 30 seconds timeout
- // Graceful shutdown handling for Docker
- process.on('SIGTERM', async () => {
- console.log('[CORE] SIGTERM received, shutting down gracefully...');
- server.close(() => {
- console.log('[CORE] Server closed');
- process.exit(0);
- });
+ server.close(() => {
+ clearTimeout(forceExit);
+ console.log('[CORE] Server closed');
+ process.exit(0);
});
+ });
- process.on('SIGINT', async () => {
- console.log('[CORE] SIGINT received, shutting down gracefully...');
- server.close(() => {
- console.log('[CORE] Server closed');
- process.exit(0);
- });
+ process.on('SIGINT', async () => {
+ console.log('[CORE] SIGINT received, shutting down gracefully...');
+ server.close(() => {
+ console.log('[CORE] Server closed');
+ process.exit(0);
});
- })();
+ });
+ })();
}