refactor: 🚨 Remove process.exit usage

This commit is contained in:
Jesse Wierzbinski 2024-10-03 11:59:26 +02:00
parent b1d8595a7c
commit 076e930369
No known key found for this signature in database
8 changed files with 22 additions and 45 deletions

6
app.ts
View file

@ -133,11 +133,7 @@ export const appFactory = async () => {
serverLogger.fatal`Put your configuration at ${chalk.blueBright(
"plugins.<plugin-name>",
)}`;
serverLogger.fatal`Press Ctrl+C to exit`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
process.exit();
throw new Error("Plugin configuration is invalid");
}
// biome-ignore lint/complexity/useLiteralKeys: AddToApp is a private method

View file

@ -29,7 +29,7 @@ await Bun.build({
}).then((output) => {
if (!output.success) {
console.error(output.logs);
process.exit(1);
throw new Error("Build failed");
}
});

View file

@ -52,7 +52,6 @@ export const setupDatabase = async (info = true) => {
return;
}
logger.fatal`${e}`;
logger.fatal`Failed to connect to database ${chalk.bold(
// Index of the database in the array
replicas.indexOf(dbPool) === -1
@ -60,8 +59,7 @@ export const setupDatabase = async (info = true) => {
: `replica-${replicas.indexOf(dbPool)}`,
)}. Please check your configuration.`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
throw e;
}
}
@ -73,11 +71,9 @@ export const setupDatabase = async (info = true) => {
migrationsFolder: "./drizzle/migrations",
});
} catch (e) {
logger.fatal`${e}`;
logger.fatal`Failed to migrate database. Please check your configuration.`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
throw e;
}
info && logger.info`Database migrated`;

View file

@ -4,6 +4,10 @@ import { createServer } from "@/server";
import { appFactory } from "~/app";
import { config } from "~/packages/config-manager/index";
process.on("SIGINT", () => {
process.exit();
});
if (cluster.isPrimary) {
for (let i = 0; i < Number(process.env.NUM_CPUS ?? 1); i++) {
cluster.fork();

View file

@ -150,6 +150,10 @@ export const configValidator = z.object({
enabled: false,
address: "",
})
.refine(
(arg) => !arg.enabled || !!arg.address,
"When proxy is enabled, address must be set",
)
.transform((arg) => ({
...arg,
address: arg.enabled ? arg.address : undefined,

View file

@ -23,10 +23,7 @@ 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();
throw fromZodError(parsed.error).message;
}
const exportedConfig = parsed.data;

View file

@ -20,10 +20,6 @@ if (config.sonic.enabled) {
await searchManager.connect();
}
process.on("SIGINT", () => {
process.exit();
});
// Check if database is reachable
const postCount = await Note.getCount();
@ -34,12 +30,6 @@ serverLogger.info`Versia Server started at ${config.http.bind}:${config.http.bin
serverLogger.info`Database is online, now serving ${postCount} posts`;
if (config.frontend.enabled) {
if (!URL.canParse(config.frontend.url)) {
serverLogger.error`Frontend URL is not a valid URL: ${config.frontend.url}`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
}
// Check if frontend is reachable
const response = await fetch(new URL("/", config.frontend.url))
.then((res) => res.ok)

View file

@ -17,13 +17,6 @@ const checkHttpProxyConfig = async (config: Config) => {
const logger = getLogger("server");
if (config.http.proxy.enabled) {
if (!config.http.proxy.address) {
logger.fatal`The HTTP proxy is enabled, but the proxy address is not set in the config`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
}
logger.info`HTTP proxy enabled at ${chalk.gray(config.http.proxy.address)}, testing...`;
// Test the proxy
@ -37,10 +30,9 @@ const checkHttpProxyConfig = async (config: Config) => {
logger.info`Your IPv4 address is ${chalk.gray(ip)}`;
if (!response.ok) {
logger.fatal`The HTTP proxy is enabled, but the proxy address is not reachable`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
throw new Error(
"The HTTP proxy is enabled, but the proxy address is not reachable",
);
}
}
};
@ -127,10 +119,9 @@ const checkOidcConfig = async (config: Config) => {
.catch((e) => e as Error);
if (privateKey instanceof Error || publicKey instanceof Error) {
logger.fatal`The OpenID keys could not be imported! You may generate a new one by removing the old ones from config and restarting the server (this will invalidate all current JWTs).`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
throw new Error(
"The OpenID keys could not be imported! You may generate a new one by removing the old ones from config and restarting the server (this will invalidate all current JWTs).",
);
}
};
@ -174,9 +165,8 @@ const checkFederationConfig = async (config: Config) => {
.catch((e) => e as Error);
if (privateKey instanceof Error || publicKey instanceof Error) {
logger.fatal`The federation keys could not be imported! You may generate new ones by removing the old ones from the config and restarting the server.`;
// Hang until Ctrl+C is pressed
await Bun.sleep(Number.POSITIVE_INFINITY);
throw new Error(
"The federation keys could not be imported! You may generate new ones by removing the old ones from the config and restarting the server.",
);
}
};