Last active
May 24, 2021 16:37
-
-
Save charles-dr/fd00ae11713bb2312ed0db290fdb605e to your computer and use it in GitHub Desktop.
MongoDBConnectionTimeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NodeCache = require('node-cache'); | |
const axios = require('axios'); | |
const { exec } = require('child_process'); | |
const cache = new NodeCache(); | |
const cacheId = 'MONGODB_TIMEDOUT_MONITOR'; | |
const discordWebhook = 'WEBOOK_URL'; | |
const regExp = new RegExp('Server selection timed out after (.*) ms', 'gi'); | |
const activity = { | |
checkUnderRestarting: () => { | |
const restarting = cache.get(cacheId); | |
return restarting; | |
}, | |
markAsRestarting: () => { | |
cache.set(cacheId, true); | |
}, | |
notify2Discord: ({ message, title, description }) => { | |
// notify to discord. | |
}, | |
restartServer: () => { | |
return exec('pm2 restart server'); | |
}, | |
}; | |
module.exports = async (error) => { | |
if (error.extensions.code === 'INTERNAL_SERVER_ERROR') { | |
const errors = error.extensions.exception.errors; | |
const targetErrors = errors.filter((err) => { | |
const matched = err.message.match(regExp); | |
return !!matched; | |
}); | |
if (targetErrors.length > 0) { | |
if (activity.checkUnderRestarting()) return true; | |
activity.markAsRestarting(); | |
// restart pm2 | |
await activity.notify2Discord({ | |
message: 'Critical error occured!', | |
title: error.extensions.code, | |
description: targetErrors[0].message, | |
}); | |
activity.restartServer(); | |
} | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ApolloServer } = require('apollo-server-express'); | |
const path = require('path'); | |
const config = require(path.resolve('config')); | |
const logger = require(path.resolve('config/logger')); | |
const createSchema = require('./schema'); | |
const checkMongoTimedout = require('./checkTimeout.js'); | |
module.exports = ({ repository }) => new ApolloServer({ | |
schema: createSchema(), | |
formatError: (error) => { | |
checkMongoTimedout(error); | |
// some additional operation for errors. | |
return error; | |
}, | |
context: async (request) => { | |
// describe about your context. | |
}, | |
subscriptions: { | |
keepAlive: 10000, | |
onConnect: async (connectionParams, webSocket, context) => { | |
// logic on socket connection. | |
}, | |
onDisconnect: async (webSocket, context) => { | |
// logic on socket disconnection | |
}, | |
}, | |
introspection: true, | |
engine: { | |
apiKey: config.env === 'production' ? config.apolloEngineApiKey : null, | |
useUnifiedTopology: true, | |
}, | |
dataSources: () => ({ something }), | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment