feat(prodsqlconnection): added in prod connection with restart attempts and fail with notify

This commit is contained in:
2025-09-01 16:46:29 -05:00
parent bfb62df445
commit 083f38a079
11 changed files with 315 additions and 44 deletions

View File

@@ -6,22 +6,23 @@ import { printers } from "./internal/ocp/printers/printers.js";
import path, { dirname, join } from "path";
import { fileURLToPath } from "url";
import { db } from "./pkg/db/db.js";
import { settings } from "./pkg/db/schema/settings.js";
import { settings, type Setting } from "./pkg/db/schema/settings.js";
import { env } from "./pkg/utils/envValidator.js";
import { createLogger } from "./pkg/logger/logger.js";
import { returnFunc } from "./pkg/utils/return.js";
import { initializeProdPool } from "./pkg/prodSql/prodSqlConnect.js";
import { tryCatch } from "./pkg/utils/tryCatch.js";
const PORT = Number(env.VITE_PORT) || 4200;
const main = async () => {
//create the logger
const log = createLogger({ module: "system", subModule: "main start" });
// base path
let basePath: string = "";
if (
process.env.NODE_ENV?.trim() !== "production" &&
!env.RUNNING_IN_DOCKER
) {
if (process.env.NODE_ENV?.trim() !== "production") {
basePath = "/lst";
}
@@ -29,18 +30,34 @@ const main = async () => {
const __dirname = dirname(__filename);
// Db connection stuff
try {
const set = await db.select().from(settings);
const res = await tryCatch(db.select().from(settings));
if (set.length === 0) {
return log.fatal(
{ notify: true },
"Seems like the DB is not setup yet the app will close now"
);
}
} catch (error) {
console.error("Error getting settings", error);
if (res.error) {
return returnFunc({
success: false,
module: "system",
level: "fatal",
message: `Database lookup failed`,
notify: false,
data: [],
});
}
if (res.data.length === 0) {
//return
returnFunc({
success: false,
module: "system",
level: "fatal",
message: `This seems to be the first time you have started the app please validate the settings have been intiated`,
notify: false,
data: [],
});
}
// connect to the prod sql
await initializeProdPool();
// express app
const app = express();
@@ -82,10 +99,42 @@ const main = async () => {
}, on http://0.0.0.0:${PORT}${basePath}`
)
);
// Handle app exit signals
process.on("SIGINT", async () => {
console.log("\nGracefully shutting down...");
//await closePool();
process.exit(0);
});
process.on("SIGTERM", async () => {
console.log("Received termination signal, closing database...");
//await closePool();
process.exit(0);
});
process.on("uncaughtException", async (err) => {
console.log("Uncaught Exception:", err);
//await closePool();
// const emailData = {
// email: "blake.matthes@alpla.com", // should be moved to the db so it can be reused.
// subject: `${os.hostname()} has just encountered a crash.`,
// template: "serverCrash",
// context: {
// error: err,
// plant: `${os.hostname()}`,
// },
// };
// await sendEmail(emailData);
process.exit(1);
});
process.on("beforeExit", async () => {
console.log("Process is about to exit...");
//await closePool();
process.exit(0);
});
};
main().catch((err) => {
const log = createLogger({ module: "system", subModule: "main start" });
log.fatal("Startup error:", err);
process.exit(1);
});
main();