yes
This commit is contained in:
@@ -5,7 +5,7 @@ try {
|
||||
if (document || window) {
|
||||
throw new Error("SurrealDB needs a NodeJS environment to run.");
|
||||
}
|
||||
} catch (err) {}
|
||||
} catch (err) { }
|
||||
|
||||
const CONFIG = {
|
||||
url: process.env.SURREAL_URL ?? "",
|
||||
@@ -22,8 +22,10 @@ async function connectDB() {
|
||||
await db.connect(`http://${CONFIG.url}/rpc`);
|
||||
await db.use({ namespace: CONFIG.ns, database: CONFIG.db });
|
||||
await authenticateDB();
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error connecting to SurrealDB:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +33,10 @@ async function authenticateDB() {
|
||||
try {
|
||||
await db.signin({ username: CONFIG.user, password: CONFIG.pass });
|
||||
console.log("🔑 Successfully authenticated with SurrealDB");
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("❌ Authentication failed:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,24 +44,56 @@ async function ensureAuthenticated() {
|
||||
try {
|
||||
console.log("⏳ Ensuring authentication with SurrealDB...");
|
||||
await db.query("RETURN 1");
|
||||
} catch (error) {
|
||||
const e = error as any;
|
||||
if (e.status === 401) {
|
||||
console.warn("⚠️ Token expired. Re-authenticating...");
|
||||
await authenticateDB();
|
||||
} catch (error: any) {
|
||||
if (error.status === 401) {
|
||||
console.warn("⚠️ Token expired. Attempting reconnection...");
|
||||
try {
|
||||
// Full reconnection instead of just re-authentication
|
||||
await db.close();
|
||||
const success = await connectDB();
|
||||
if (success) {
|
||||
console.log("✅ Successfully reconnected to database");
|
||||
} else {
|
||||
console.error("Unexpected database error:", e);
|
||||
console.error("❌ Failed to reconnect to database");
|
||||
}
|
||||
} catch (reconnectError) {
|
||||
console.error("❌ Reconnection failed:", reconnectError);
|
||||
}
|
||||
} else {
|
||||
console.error("Unexpected database error:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const MINS_5 = 5 * 60 * 1000;
|
||||
setInterval(ensureAuthenticated, MINS_5);
|
||||
// 1 minute
|
||||
const CHECK_INTERVAL = 60 * 1000;
|
||||
let intervalId: NodeJS.Timeout;
|
||||
|
||||
await connectDB();
|
||||
async function initializeDB() {
|
||||
const success = await connectDB();
|
||||
if (success) {
|
||||
// Only start the interval if initial connection was successful
|
||||
intervalId = setInterval(ensureAuthenticated, CHECK_INTERVAL);
|
||||
} else {
|
||||
console.error("Failed to initialize database connection");
|
||||
// Optionally implement retry logic here
|
||||
}
|
||||
}
|
||||
|
||||
export function cleanup() {
|
||||
if (intervalId) {
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
return db.close();
|
||||
}
|
||||
|
||||
await initializeDB();
|
||||
|
||||
export const surreal = db as Surreal;
|
||||
|
||||
export function parseToRID(idStr: string) {
|
||||
return new StringRecordId(idStr);
|
||||
}
|
||||
|
||||
process.on("SIGTERM", cleanup);
|
||||
process.on("SIGINT", cleanup);
|
||||
|
||||
Reference in New Issue
Block a user