refactor(sql): some changes to help with sql connection on random disconnect
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
import { returnFunc } from "../utils/return.js";
|
|
||||||
import { connected, pool } from "./prodSqlConnect.js";
|
|
||||||
import { validateEnv } from "../utils/envValidator.js";
|
import { validateEnv } from "../utils/envValidator.js";
|
||||||
|
import { returnFunc } from "../utils/return.js";
|
||||||
|
import {
|
||||||
|
closePool,
|
||||||
|
connected,
|
||||||
|
pool,
|
||||||
|
reconnecting,
|
||||||
|
reconnectToSql,
|
||||||
|
} from "./prodSqlConnect.js";
|
||||||
|
|
||||||
const env = validateEnv(process.env);
|
const env = validateEnv(process.env);
|
||||||
/**
|
/**
|
||||||
@@ -12,6 +18,19 @@ const env = validateEnv(process.env);
|
|||||||
*/
|
*/
|
||||||
export async function prodQuery(queryToRun: string, name: string) {
|
export async function prodQuery(queryToRun: string, name: string) {
|
||||||
if (!connected) {
|
if (!connected) {
|
||||||
|
reconnectToSql();
|
||||||
|
|
||||||
|
if (reconnecting) {
|
||||||
|
return returnFunc({
|
||||||
|
success: false,
|
||||||
|
module: "prodSql",
|
||||||
|
subModule: "query",
|
||||||
|
level: "error",
|
||||||
|
message: `The sql ${env.PROD_PLANT_TOKEN} is trying to reconnect already`,
|
||||||
|
notify: false,
|
||||||
|
data: [],
|
||||||
|
});
|
||||||
|
} else {
|
||||||
return returnFunc({
|
return returnFunc({
|
||||||
success: false,
|
success: false,
|
||||||
module: "prodSql",
|
module: "prodSql",
|
||||||
@@ -22,6 +41,8 @@ export async function prodQuery(queryToRun: string, name: string) {
|
|||||||
data: [],
|
data: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const query = queryToRun.replaceAll("test1", env.PROD_PLANT_TOKEN);
|
const query = queryToRun.replaceAll("test1", env.PROD_PLANT_TOKEN);
|
||||||
try {
|
try {
|
||||||
const result = await pool.request().query(query);
|
const result = await pool.request().query(query);
|
||||||
@@ -33,6 +54,7 @@ export async function prodQuery(queryToRun: string, name: string) {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
if (error.code === "ETIMEOUT") {
|
if (error.code === "ETIMEOUT") {
|
||||||
|
closePool();
|
||||||
return returnFunc({
|
return returnFunc({
|
||||||
success: false,
|
success: false,
|
||||||
module: "prodSql",
|
module: "prodSql",
|
||||||
@@ -45,6 +67,7 @@ export async function prodQuery(queryToRun: string, name: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (error.code === "EREQUEST") {
|
if (error.code === "EREQUEST") {
|
||||||
|
closePool();
|
||||||
return returnFunc({
|
return returnFunc({
|
||||||
success: false,
|
success: false,
|
||||||
module: "prodSql",
|
module: "prodSql",
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
import sql from "mssql";
|
import sql from "mssql";
|
||||||
import { checkHostnamePort } from "../utils/checkHostNamePort.js";
|
|
||||||
import { sqlConfig } from "./prodSqlConfig.js";
|
|
||||||
import { createLogger } from "../logger/logger.js";
|
import { createLogger } from "../logger/logger.js";
|
||||||
import { returnFunc } from "../utils/return.js";
|
import { checkHostnamePort } from "../utils/checkHostNamePort.js";
|
||||||
import { validateEnv } from "../utils/envValidator.js";
|
import { validateEnv } from "../utils/envValidator.js";
|
||||||
|
import { returnFunc } from "../utils/return.js";
|
||||||
|
import { sqlConfig } from "./prodSqlConfig.js";
|
||||||
|
|
||||||
const env = validateEnv(process.env);
|
const env = validateEnv(process.env);
|
||||||
|
|
||||||
export let pool: any;
|
export let pool: any;
|
||||||
export let connected: boolean = false;
|
export let connected: boolean = false;
|
||||||
let reconnecting = false;
|
export let reconnecting = false;
|
||||||
|
|
||||||
export const initializeProdPool = async () => {
|
export const initializeProdPool = async () => {
|
||||||
const log = createLogger({ module: "prodSql" });
|
const log = createLogger({ module: "prodSql" });
|
||||||
@@ -41,21 +41,19 @@ export const initializeProdPool = async () => {
|
|||||||
pool = await sql.connect(sqlConfig);
|
pool = await sql.connect(sqlConfig);
|
||||||
|
|
||||||
log.info(
|
log.info(
|
||||||
`Connected to ${sqlConfig?.server}, using DB: ${sqlConfig?.database}`
|
`Connected to ${sqlConfig?.server}, using DB: ${sqlConfig?.database}`,
|
||||||
);
|
);
|
||||||
connected = true;
|
connected = true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.fatal(
|
log.fatal(
|
||||||
`${JSON.stringify(
|
`${JSON.stringify(error)}, "There was an error connecting to the pool."`,
|
||||||
error
|
|
||||||
)}, "There was an error connecting to the pool."`
|
|
||||||
);
|
);
|
||||||
reconnectToSql();
|
reconnectToSql();
|
||||||
// throw new Error("There was an error closing the sql connection");
|
// throw new Error("There was an error closing the sql connection");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const reconnectToSql = async () => {
|
export const reconnectToSql = async () => {
|
||||||
const log = createLogger({ module: "prodSql" });
|
const log = createLogger({ module: "prodSql" });
|
||||||
if (reconnecting) return;
|
if (reconnecting) return;
|
||||||
reconnecting = true;
|
reconnecting = true;
|
||||||
@@ -67,9 +65,7 @@ const reconnectToSql = async () => {
|
|||||||
while (!connected && attempts < maxAttempts) {
|
while (!connected && attempts < maxAttempts) {
|
||||||
attempts++;
|
attempts++;
|
||||||
log.info(
|
log.info(
|
||||||
`Reconnect attempt ${attempts}/${maxAttempts} in ${
|
`Reconnect attempt ${attempts}/${maxAttempts} in ${delay / 1000}s...`,
|
||||||
delay / 1000
|
|
||||||
}s...`
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await new Promise((res) => setTimeout(res, delay));
|
await new Promise((res) => setTimeout(res, delay));
|
||||||
@@ -85,15 +81,15 @@ const reconnectToSql = async () => {
|
|||||||
pool = sql.connect(sqlConfig);
|
pool = sql.connect(sqlConfig);
|
||||||
|
|
||||||
log.info(
|
log.info(
|
||||||
`Connected to ${sqlConfig?.server}, and looking at ${sqlConfig?.database}`
|
`Connected to ${sqlConfig?.server}, and looking at ${sqlConfig?.database}`,
|
||||||
);
|
);
|
||||||
reconnecting = false;
|
reconnecting = false;
|
||||||
connected = true;
|
connected = true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.fatal(
|
log.fatal(
|
||||||
`${JSON.stringify(
|
`${JSON.stringify(
|
||||||
error
|
error,
|
||||||
)}, "There was an error connecting to the pool."`
|
)}, "There was an error connecting to the pool."`,
|
||||||
);
|
);
|
||||||
delay = Math.min(delay * 2, 30000); // exponential backoff up to 30s
|
delay = Math.min(delay * 2, 30000); // exponential backoff up to 30s
|
||||||
// throw new Error("There was an error closing the sql connection");
|
// throw new Error("There was an error closing the sql connection");
|
||||||
@@ -103,10 +99,10 @@ const reconnectToSql = async () => {
|
|||||||
if (!connected) {
|
if (!connected) {
|
||||||
log.fatal(
|
log.fatal(
|
||||||
{ notify: true },
|
{ notify: true },
|
||||||
"Max reconnect attempts reached on the prodSql server. Stopping retries."
|
"Max reconnect attempts reached on the prodSql server. Stopping retries.",
|
||||||
);
|
);
|
||||||
reconnecting = false;
|
reconnecting = false;
|
||||||
// optional: exit process or alert someone here
|
// exit process or alert someone here
|
||||||
// process.exit(1);
|
// process.exit(1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -126,11 +122,13 @@ export const closePool = async () => {
|
|||||||
message: "The sql server connection has been closed",
|
message: "The sql server connection has been closed",
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.fatal(
|
connected = false;
|
||||||
{ notify: true },
|
log.info(
|
||||||
|
//{ notify: true },
|
||||||
|
{ error: error },
|
||||||
`${JSON.stringify(
|
`${JSON.stringify(
|
||||||
error
|
error,
|
||||||
)}, "There was an error closing the sql connection"`
|
)}, "There was an error closing the sql connection"`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user