fix(lots errors): fixes to prevent crashes when sql server not connected
This commit is contained in:
@@ -11,18 +11,10 @@ export const getLots = async () => {
|
|||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: "There was an error getting the lots",
|
message: "There was an error getting the lots",
|
||||||
data: lotError,
|
data: [],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (!lotInfo.data.success) {
|
|
||||||
// return {
|
|
||||||
// success: false,
|
|
||||||
// message: "There was an error getting the lots",
|
|
||||||
// data: lotInfo.data.message,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "Current active lots that are technically released.",
|
message: "Current active lots that are technically released.",
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export const updatePrinters = async () => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
if (prodError) {
|
if (prodError || prodPrinters?.data.length > 10000) {
|
||||||
//console.log(prodError);
|
//console.log(prodError);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
@@ -35,7 +35,7 @@ export const updatePrinters = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// do the printer update into our db
|
// do the printer update into our db
|
||||||
const prodPrinterInfo = prodPrinters.data;
|
const prodPrinterInfo = prodPrinters?.data;
|
||||||
|
|
||||||
for (let i = 0; i < prodPrinterInfo.length; i++) {
|
for (let i = 0; i < prodPrinterInfo.length; i++) {
|
||||||
const printerStuff: any = {
|
const printerStuff: any = {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||||
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
|
||||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||||
import { getPrinters } from "../../controller/printers/getPrinters.js";
|
|
||||||
import { getLots } from "../../controller/lots/lots.js";
|
import { getLots } from "../../controller/lots/lots.js";
|
||||||
|
|
||||||
const app = new OpenAPIHono({ strict: false });
|
const app = new OpenAPIHono({ strict: false });
|
||||||
@@ -15,7 +14,7 @@ app.openapi(
|
|||||||
path: "/getlots",
|
path: "/getlots",
|
||||||
responses: responses(),
|
responses: responses(),
|
||||||
}),
|
}),
|
||||||
async (c) => {
|
async (c: any) => {
|
||||||
const { data: lotData, error: lotError } = await tryCatch(getLots());
|
const { data: lotData, error: lotError } = await tryCatch(getLots());
|
||||||
|
|
||||||
if (lotError) {
|
if (lotError) {
|
||||||
@@ -26,9 +25,9 @@ app.openapi(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
success: lotData.success,
|
success: lotData?.success,
|
||||||
message: lotData.message,
|
message: lotData?.message,
|
||||||
data: lotData.data,
|
data: lotData?.data,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -18,6 +18,20 @@ export const assignedPrinters = async () => {
|
|||||||
data: lotError,
|
data: lotError,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (l.data.length === 0 && !l.data.sucess) {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"lst",
|
||||||
|
"ocp",
|
||||||
|
`There was an error getting the lots: ${l?.message}`
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Error getting lots",
|
||||||
|
data: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
const { data: print, error: printerError } = await tryCatch(getPrinters());
|
const { data: print, error: printerError } = await tryCatch(getPrinters());
|
||||||
|
|
||||||
if (printerError) {
|
if (printerError) {
|
||||||
@@ -29,17 +43,11 @@ export const assignedPrinters = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const printers: any = print.data ?? [];
|
const printers: any = print.data ?? [];
|
||||||
const lots: any = l.data.length === 0 ? [] : l.data;
|
const lots: any = l?.data.length === 0 ? null : l?.data;
|
||||||
|
|
||||||
if (!lots) {
|
//console.log(lots);
|
||||||
createLog(
|
|
||||||
"error",
|
return;
|
||||||
"lst",
|
|
||||||
"ocp",
|
|
||||||
`There was an error getting the lots: ${lots.message}`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (let i = 0; i < printers.length; i++) {
|
for (let i = 0; i < printers.length; i++) {
|
||||||
// is the printer assinged in alplalabel online?
|
// is the printer assinged in alplalabel online?
|
||||||
const assigned = lots?.filter(
|
const assigned = lots?.filter(
|
||||||
|
|||||||
@@ -10,189 +10,196 @@ import { checkHostnamePort } from "../../globalUtils/pingServer.js";
|
|||||||
let pool: any;
|
let pool: any;
|
||||||
let connected: boolean = false;
|
let connected: boolean = false;
|
||||||
export const initializeProdPool = async () => {
|
export const initializeProdPool = async () => {
|
||||||
if (!installed) {
|
if (!installed) {
|
||||||
createLog(
|
createLog(
|
||||||
"info",
|
"info",
|
||||||
"lst",
|
"lst",
|
||||||
"sqlProd",
|
"sqlProd",
|
||||||
"The server was not installed will reconnect in 5 seconds"
|
"The server was not installed will reconnect in 5 seconds"
|
||||||
);
|
);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
initializeProdPool();
|
initializeProdPool();
|
||||||
}, 5 * 1000);
|
}, 5 * 1000);
|
||||||
|
|
||||||
return { success: false, message: "The server is not installed." };
|
return { success: false, message: "The server is not installed." };
|
||||||
}
|
}
|
||||||
const dbServer = await db
|
const dbServer = await db
|
||||||
.select()
|
.select()
|
||||||
.from(settings)
|
.from(settings)
|
||||||
.where(eq(settings.name, "dbServer"));
|
.where(eq(settings.name, "dbServer"));
|
||||||
const serverUp = await checkHostnamePort(`${dbServer[0].value}:1433`);
|
const serverUp = await checkHostnamePort(`${dbServer[0].value}:1433`);
|
||||||
|
|
||||||
if (!serverUp) {
|
if (!serverUp) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"lst",
|
"lst",
|
||||||
"server",
|
"server",
|
||||||
`The sql ${dbServer[0].value} is not reachable`
|
`The sql ${dbServer[0].value} is not reachable`
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: `The sql ${dbServer[0].value} is not reachable`,
|
message: `The sql ${dbServer[0].value} is not reachable`,
|
||||||
};
|
data: [],
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// make sure the server is not set to localhost this will prevent some weird issues later but can be localhost on the dev
|
// make sure the server is not set to localhost this will prevent some weird issues later but can be localhost on the dev
|
||||||
const serverLoc = await db
|
const serverLoc = await db
|
||||||
.select()
|
.select()
|
||||||
.from(settings)
|
.from(settings)
|
||||||
.where(eq(settings.name, "dbServer"));
|
.where(eq(settings.name, "dbServer"));
|
||||||
if (
|
if (
|
||||||
serverLoc[0].value === "localhost" &&
|
serverLoc[0].value === "localhost" &&
|
||||||
process.env.NODE_ENV !== "development"
|
process.env.NODE_ENV !== "development"
|
||||||
) {
|
) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"lst",
|
"lst",
|
||||||
"sqlProd",
|
"sqlProd",
|
||||||
"The server is set to localhost, and you are not in development mode."
|
"The server is set to localhost, and you are not in development mode."
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message:
|
message:
|
||||||
"The server is set to localhost, and you are not in development mode.",
|
"The server is set to localhost, and you are not in development mode.",
|
||||||
};
|
data: [],
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// if you were restarting from the endpoint you get this lovely error
|
// if you were restarting from the endpoint you get this lovely error
|
||||||
if (connected) {
|
if (connected) {
|
||||||
createLog("error", "lst", "sqlProd", "There is already a connection.");
|
createLog("error", "lst", "sqlProd", "There is already a connection.");
|
||||||
return { success: false, message: "There is already a connection." };
|
return { success: false, message: "There is already a connection." };
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const config = await prodSqlConfig();
|
const config = await prodSqlConfig();
|
||||||
pool = await sql.connect(config!);
|
pool = await sql.connect(config!);
|
||||||
|
|
||||||
createLog(
|
createLog(
|
||||||
"info",
|
"info",
|
||||||
"lst",
|
"lst",
|
||||||
"sqlProd",
|
"sqlProd",
|
||||||
`Connected to ${config?.server}, and looking at ${config?.database}`
|
`Connected to ${config?.server}, and looking at ${config?.database}`
|
||||||
);
|
);
|
||||||
connected = true;
|
connected = true;
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "The sql server connection has been closed",
|
message: "The sql server connection has been closed",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"lst",
|
"lst",
|
||||||
"sqlProd",
|
"sqlProd",
|
||||||
`${JSON.stringify(error)}, "There was an error connecting to the pool."`
|
`${JSON.stringify(
|
||||||
);
|
error
|
||||||
throw new Error("There was an error closing the sql connection");
|
)}, "There was an error connecting to the pool."`
|
||||||
}
|
);
|
||||||
|
throw new Error("There was an error closing the sql connection");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const closePool = async () => {
|
export const closePool = async () => {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"lst",
|
"lst",
|
||||||
"sqlProd",
|
"sqlProd",
|
||||||
"There is no connection a connection."
|
"There is no connection a connection."
|
||||||
);
|
);
|
||||||
return { success: false, message: "There is already a connection." };
|
return { success: false, message: "There is already a connection." };
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await pool.close();
|
await pool.close();
|
||||||
createLog("info", "lst", "sqlProd", "Connection pool closed");
|
createLog("info", "lst", "sqlProd", "Connection pool closed");
|
||||||
connected = false;
|
connected = false;
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
message: "The sql server connection has been closed",
|
message: "The sql server connection has been closed",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"lst",
|
"lst",
|
||||||
"sqlProd",
|
"sqlProd",
|
||||||
`${JSON.stringify(
|
`${JSON.stringify(
|
||||||
error
|
error
|
||||||
)}, "There was an error closing the sql connection"`
|
)}, "There was an error closing the sql connection"`
|
||||||
);
|
);
|
||||||
throw new Error("There was an error closing the sql connection");
|
throw new Error("There was an error closing the sql connection");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function query(queryToRun: string, name: string) {
|
export async function query(queryToRun: string, name: string) {
|
||||||
/**
|
/**
|
||||||
* Just an extra catch incase someone tried to run a query while we were not connected to the server or sql server
|
* Just an extra catch incase someone tried to run a query while we were not connected to the server or sql server
|
||||||
*/
|
*/
|
||||||
const dbServer = await db
|
const dbServer = await db
|
||||||
.select()
|
.select()
|
||||||
.from(settings)
|
.from(settings)
|
||||||
.where(eq(settings.name, "dbServer"));
|
.where(eq(settings.name, "dbServer"));
|
||||||
const serverUp = await checkHostnamePort(`${dbServer[0].value}:1433`);
|
const serverUp = await checkHostnamePort(`${dbServer[0].value}:1433`);
|
||||||
|
|
||||||
if (!serverUp) {
|
if (!serverUp) {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"lst",
|
"lst",
|
||||||
"server",
|
"server",
|
||||||
`The sql ${dbServer[0].value} is not reachable`
|
`The sql ${dbServer[0].value} is not reachable`
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: `The sql ${dbServer[0].value} is not reachable`,
|
message: `The sql ${dbServer[0].value} is not reachable`,
|
||||||
};
|
data: [],
|
||||||
}
|
};
|
||||||
|
|
||||||
if (!connected) {
|
|
||||||
createLog(
|
|
||||||
"error",
|
|
||||||
"lst",
|
|
||||||
"server",
|
|
||||||
`The sql ${dbServer[0].value} is not connected`
|
|
||||||
);
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
message: `The sql ${dbServer[0].value} is not not connected`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* We no longer need to send over the plant token change as we do it inside the query function.
|
|
||||||
*/
|
|
||||||
const plantToken = await db
|
|
||||||
.select()
|
|
||||||
.from(settings)
|
|
||||||
.where(eq(settings.name, "plantToken"));
|
|
||||||
const query = queryToRun.replaceAll("test1", plantToken[0].value);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await pool.request().query(query);
|
|
||||||
|
|
||||||
return result.recordset;
|
|
||||||
} catch (error: any) {
|
|
||||||
if (error.code === "ETIMEOUT") {
|
|
||||||
createLog(
|
|
||||||
"error",
|
|
||||||
"lst",
|
|
||||||
"sqlProd",
|
|
||||||
`${JSON.stringify(error)}, ${name} did not run due to a timeout.`
|
|
||||||
);
|
|
||||||
throw new Error(`${name} query did not run due to a timeout.`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error.code === "EREQUEST") {
|
if (!connected) {
|
||||||
throw new Error(
|
createLog(
|
||||||
`${name} encoutnered an error ${error.originalError.info.message}`
|
"error",
|
||||||
);
|
"lst",
|
||||||
|
"server",
|
||||||
|
`The sql ${dbServer[0].value} is not connected`
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: `The sql ${dbServer[0].value} is not not connected`,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* We no longer need to send over the plant token change as we do it inside the query function.
|
||||||
|
*/
|
||||||
|
const plantToken = await db
|
||||||
|
.select()
|
||||||
|
.from(settings)
|
||||||
|
.where(eq(settings.name, "plantToken"));
|
||||||
|
const query = queryToRun.replaceAll("test1", plantToken[0].value);
|
||||||
|
|
||||||
//console.log(error.originalError.info.message);
|
try {
|
||||||
//EREQUEST
|
const result = await pool.request().query(query);
|
||||||
//throw new Error(`${name} encoutnered an error ${error.code}`);
|
|
||||||
}
|
return result.recordset;
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.code === "ETIMEOUT") {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"lst",
|
||||||
|
"sqlProd",
|
||||||
|
`${JSON.stringify(
|
||||||
|
error
|
||||||
|
)}, ${name} did not run due to a timeout.`
|
||||||
|
);
|
||||||
|
throw new Error(`${name} query did not run due to a timeout.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.code === "EREQUEST") {
|
||||||
|
throw new Error(
|
||||||
|
`${name} encoutnered an error ${error.originalError.info.message}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//console.log(error.originalError.info.message);
|
||||||
|
//EREQUEST
|
||||||
|
//throw new Error(`${name} encoutnered an error ${error.code}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user