新手有個問題想問問大家
我的discord bot 會連線到本地的mongodb
但不知是什麼原因(可能太多讀寫?)
在晚上高峰時間, 會突然出現下面的錯誤信息,
就連不上mongodb
然後discord中的那一個shard就會死掉,
但其他shard還是正常狀態,
如果把整個Discord bot 重啓後就可以正常了
有方法可以讓discord bot就算連不上, 也可以讓discord bot不會掉線的方法嗎?
Discord Unhandled promise rejection:
MongooseServerSelectionError: connection timed out
at Function.Model.$wrapCallback (/node_modules/mongoose/lib/model.js:5087:32) at /node_modules/mongoose/lib/query.js:4510:21 at /node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5 at new Promise (<anonymous>) at promiseOrCallback (/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10) at model.Query.exec (/node_modules/mongoose/lib/query.js:4509:10) at model.Query.Query.then (/node_modules/mongoose/lib/query.js:4592:15) at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5) { reason: TopologyDescription { type: 'Single', setName: null, maxSetVersion: null, maxElectionId: null, servers: Map(1) { 'localhost:27069' => [ServerDescription] }, stale: false, compatible: true, compatibilityError: null, logicalSessionTimeoutMinutes: null, heartbeatFrequencyMS: 10000, localThresholdMS: 15, commonWireVersion: 9 } }
現在會像下面這樣,先連到mongodb 才開始連上discord.
mongodb也應該正常的,至少其餘的shard也可連上,就是會有一個連不上,令到一個shard offline ...
這架在一個VPS的linux上。
感謝大家
"use strict";
if (!process.env.mongoURL) return;
const mongoose = require('mongoose');
(async () => {
try {
await mongoose.connect(process.env.mongoURL, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true
});
} catch (err) {
console.error('DB CONNECT GET ERROR: ' + err)
}
})();
const db = mongoose.connection;
db.on('error', console.error.bind('mlab connection error:', console));
db.once('open', function () {
console.log('mlab connected!');
require('fs').readdirSync(__dirname).forEach(function (file) {
if (file.match(/\.js$/) && file.match(/^core-/)) {
var name = file.replace('.js', '');
exports[name] = require('./' + file);
}
});
});
module.exports = {
mongoose
```