fix: prevent loop in case of logs with db down

This commit is contained in:
Rafael Taranto 2024-08-07 21:03:50 +01:00
parent dda217a29e
commit b9b532a633
2 changed files with 31 additions and 20 deletions

View file

@ -7,6 +7,8 @@ const logger = require('./logger')
const eventBus = require('./event-bus')
const { asyncLocalStorage, defaultStore } = require('./async-storage')
const DATABASE_NOT_REACHABLE = 'Database not reachable.'
const stripDefaultDbFuncs = dbCtx => {
return {
ctx: dbCtx.ctx,
@ -78,12 +80,12 @@ const pgp = Pgp({
obj.$task = (opts, cb) => typeof opts === 'function' ? _task(obj, {}, opts) : _task(obj, opts, cb)
},
error: (err, e) => {
if (e.cn) logger.error('Database not reachable.')
if (e.query) {
if (e.cn) logger.error(DATABASE_NOT_REACHABLE)
else if (e.query) {
logger.error(e.query)
e.params && logger.error(e.params)
}
logger.error(err)
else logger.error(err)
}
})
@ -93,6 +95,10 @@ eventBus.subscribe('log', args => {
if (process.env.SKIP_SERVER_LOGS) return
const { level, message, meta } = args
// prevent loop if database is not reachable
if (message === DATABASE_NOT_REACHABLE) return
const msgToSave = message || _.get('message', meta)
const sql = `insert into server_logs
@ -103,6 +109,7 @@ eventBus.subscribe('log', args => {
asyncLocalStorage.run(store, () => {
db.one(sql, [uuid.v4(), '', msgToSave, level, meta])
.then(_.mapKeys(_.camelCase))
.catch(_.noop)
})
})