This commit is contained in:
Archer
2024-07-17 19:06:37 +08:00
committed by GitHub
parent 982325d066
commit 5cb196535f
10 changed files with 522 additions and 334 deletions

View File

@@ -47,7 +47,7 @@ const addCommonMiddleware = (schema: mongoose.Schema) => {
if (duration > 1000) {
addLog.warn(`Slow operation ${duration}ms`, warnLogData);
} else if (duration > 300) {
} else if (duration > 3000) {
addLog.error(`Slow operation ${duration}ms`, warnLogData);
}
}

View File

@@ -1,4 +1,3 @@
import { exit } from 'process';
import { addLog } from '../system/log';
import { connectionMongo } from './index';
import type { Mongoose } from 'mongoose';
@@ -8,19 +7,12 @@ const maxConnecting = Math.max(30, Number(process.env.DB_MAX_LINK || 20));
/**
* connect MongoDB and init data
*/
export async function connectMongo({
beforeHook,
afterHook
}: {
beforeHook?: () => any;
afterHook?: () => Promise<any>;
}): Promise<Mongoose> {
export async function connectMongo(): Promise<Mongoose> {
/* Connecting, connected will return */
if (connectionMongo.connection.readyState !== 0) {
return connectionMongo;
}
beforeHook && beforeHook();
console.log('mongo start connect');
try {
connectionMongo.set('strictQuery', true);
@@ -56,15 +48,5 @@ export async function connectMongo({
addLog.error('mongo connect error', error);
}
try {
if (!global.systemInited) {
global.systemInited = true;
afterHook && (await afterHook());
}
} catch (error) {
addLog.error('Mongo connect after hook error', error);
exit(1);
}
return connectionMongo;
}

View File

@@ -26,5 +26,6 @@ declare global {
callbackMap: Record<string, (e: number) => void>;
}[];
var systemInited: boolean;
var systemLoadedGlobalVariables: boolean;
var systemLoadedGlobalConfig: boolean;
}