From c764cc044da88f8b6e3ab7aebea24faf8a8a1f2b Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Thu, 13 Jun 2024 23:44:46 -1000 Subject: [PATCH] fix: :ambulance: Sleep process instead of exiting it on error Avoids Docker's auto-restart policy from causing infinite reboots and hanging the system --- drizzle/db.ts | 8 ++++++-- index.ts | 18 ++++++++++++++---- packages/config-manager/index.ts | 2 ++ utils/meilisearch.ts | 3 ++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/drizzle/db.ts b/drizzle/db.ts index 51119b5f..0a8ca67f 100644 --- a/drizzle/db.ts +++ b/drizzle/db.ts @@ -34,7 +34,9 @@ export const setupDatabase = async ( "Database", "Failed to connect to database. Please check your configuration.", ); - process.exit(); + + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } // Migrate the database @@ -52,7 +54,9 @@ export const setupDatabase = async ( "Database", "Failed to migrate database. Please check your configuration.", ); - process.exit(); + + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } info && (await logger.log(LogLevel.Info, "Database", "Database migrated")); diff --git a/index.ts b/index.ts index 5ff5d090..94c8af3c 100644 --- a/index.ts +++ b/index.ts @@ -38,6 +38,10 @@ if (config.meilisearch.enabled) { await connectMeili(dualServerLogger); } +process.on("SIGINT", () => { + process.exit(); +}); + // Check if database is reachable const postCount = await Note.getCount(); @@ -73,7 +77,9 @@ if (isEntry) { "Server", chalk.gray(`${privateKey};${publicKey}`), ); - process.exit(); + + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } // Try and import the key @@ -104,7 +110,9 @@ if (isEntry) { "Server", "The JWT key could not be imported! You may generate a new one by removing the old one from the config and restarting the server (this will invalidate all current JWTs).", ); - process.exit(); + + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } if ( @@ -142,7 +150,8 @@ if (isEntry) { `Generated key: ${chalk.gray(base64)}`, ); - process.exit(); + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } } @@ -245,7 +254,8 @@ if (config.frontend.enabled) { "Server", `Frontend URL is not a valid URL: ${config.frontend.url}`, ); - process.exit(); + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } // Check if frontend is reachable diff --git a/packages/config-manager/index.ts b/packages/config-manager/index.ts index cf0c02a7..3d423988 100644 --- a/packages/config-manager/index.ts +++ b/packages/config-manager/index.ts @@ -24,6 +24,8 @@ const parsed = await configValidator.safeParseAsync(config); if (!parsed.success) { console.error("Invalid config file:"); console.error(fromZodError(parsed.error).message); + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); process.exit(); } diff --git a/utils/meilisearch.ts b/utils/meilisearch.ts index 78d98962..3aa1cf52 100644 --- a/utils/meilisearch.ts +++ b/utils/meilisearch.ts @@ -44,7 +44,8 @@ export const connectMeili = async (logger: MultiLogManager | LogManager) => { "Meilisearch", "Error while connecting to Meilisearch", ); - process.exit(); + // Hang until Ctrl+C is pressed + await Bun.sleep(Number.POSITIVE_INFINITY); } };