mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-25 06:14:06 +00:00
4.8.5 test fix (#1862)
* app list ui * feat: photo view * perf: app dataset filter * perf: app dataset filter * fix: chat recently apps * perf: workflow header phone * default templates * default templates * fix: input guide phone * fix: i18n * team chat history * remove code * perf: mongo connection * log level
This commit is contained in:
@@ -3,4 +3,10 @@ import mongoose from 'mongoose';
|
||||
export default mongoose;
|
||||
export * from 'mongoose';
|
||||
|
||||
export const connectionMongo = global.mongodb || mongoose;
|
||||
export const connectionMongo = (() => {
|
||||
if (!global.mongodb) {
|
||||
global.mongodb = mongoose;
|
||||
}
|
||||
|
||||
return global.mongodb;
|
||||
})();
|
||||
|
@@ -1,4 +1,8 @@
|
||||
import mongoose from './index';
|
||||
import { addLog } from '../system/log';
|
||||
import { connectionMongo } from './index';
|
||||
import type { Mongoose } from 'mongoose';
|
||||
|
||||
const maxConnecting = Math.max(30, Number(process.env.DB_MAX_LINK || 20));
|
||||
|
||||
/**
|
||||
* connect MongoDB and init data
|
||||
@@ -8,20 +12,27 @@ export async function connectMongo({
|
||||
afterHook
|
||||
}: {
|
||||
beforeHook?: () => any;
|
||||
afterHook?: () => any;
|
||||
}): Promise<void> {
|
||||
if (global.mongodb) {
|
||||
return;
|
||||
afterHook?: () => Promise<any>;
|
||||
}): Promise<Mongoose> {
|
||||
if (connectionMongo.connection.readyState !== 0) {
|
||||
return connectionMongo;
|
||||
}
|
||||
global.mongodb = mongoose;
|
||||
|
||||
beforeHook && (await beforeHook());
|
||||
beforeHook && beforeHook();
|
||||
|
||||
console.log('mongo start connect');
|
||||
try {
|
||||
mongoose.set('strictQuery', true);
|
||||
const maxConnecting = Math.max(30, Number(process.env.DB_MAX_LINK || 20));
|
||||
await mongoose.connect(process.env.MONGODB_URI as string, {
|
||||
connectionMongo.set('strictQuery', true);
|
||||
|
||||
connectionMongo.connection.on('error', (error) => {
|
||||
console.log('mongo error', error);
|
||||
connectionMongo.disconnect();
|
||||
});
|
||||
connectionMongo.connection.on('disconnected', () => {
|
||||
console.log('mongo disconnected');
|
||||
});
|
||||
|
||||
await connectionMongo.connect(process.env.MONGODB_URI as string, {
|
||||
bufferCommands: true,
|
||||
maxConnecting: maxConnecting,
|
||||
maxPoolSize: maxConnecting,
|
||||
@@ -34,22 +45,17 @@ export async function connectMongo({
|
||||
retryReads: true
|
||||
});
|
||||
|
||||
mongoose.connection.on('error', (error) => {
|
||||
console.log('mongo error', error);
|
||||
global.mongodb?.disconnect();
|
||||
global.mongodb = undefined;
|
||||
});
|
||||
mongoose.connection.on('disconnected', () => {
|
||||
console.log('mongo disconnected');
|
||||
global.mongodb = undefined;
|
||||
});
|
||||
|
||||
console.log('mongo connected');
|
||||
} catch (error) {
|
||||
connectionMongo.disconnect();
|
||||
addLog.error('mongo connect error', error);
|
||||
}
|
||||
|
||||
try {
|
||||
afterHook && (await afterHook());
|
||||
} catch (error) {
|
||||
global.mongodb.disconnect();
|
||||
console.log('error->', 'mongo connect error', error);
|
||||
global.mongodb = undefined;
|
||||
addLog.error('mongo connect after hook error', error);
|
||||
}
|
||||
|
||||
return connectionMongo;
|
||||
}
|
||||
|
Reference in New Issue
Block a user