wingbot-mongodb
Advanced tools
Comparing version 3.1.2 to 3.2.0
@@ -1,1 +0,1 @@ | ||
{"processes":{"348f41df-6e7a-4377-9c78-40269fd1eaef":{"parent":null,"children":[]}},"files":{"/Users/david/Development/wingbot-mongodb/src/BaseStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/defaultLogger.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/tokenFactory.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"],"/Users/david/Development/wingbot-mongodb/src/StateStorage.js":["348f41df-6e7a-4377-9c78-40269fd1eaef"]},"externalIds":{}} | ||
{"processes":{"cb79cd4d-6c45-40e4-b29a-e38af10c7c7c":{"parent":null,"children":[]}},"files":{"/Users/david/Development/wingbot-mongodb/src/BaseStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/defaultLogger.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/tokenFactory.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"],"/Users/david/Development/wingbot-mongodb/src/StateStorage.js":["cb79cd4d-6c45-40e4-b29a-e38af10c7c7c"]},"externalIds":{}} |
{ | ||
"name": "wingbot-mongodb", | ||
"version": "3.1.2", | ||
"version": "3.2.0", | ||
"description": "MongoDB storage for wingbot.ai", | ||
@@ -5,0 +5,0 @@ "main": "src/main.js", |
@@ -40,6 +40,6 @@ /* | ||
* @param {{error:Function,log:Function}} [log] - console like logger | ||
* @param {boolean} isCosmo | ||
* @param {boolean|number} isCosmo - boolean or number of failures in last 10min to kill app | ||
*/ | ||
constructor (mongoDb, collectionName = 'states', log = defaultLogger, isCosmo = false) { | ||
super(mongoDb, collectionName, log, isCosmo); | ||
super(mongoDb, collectionName, log, typeof isCosmo === 'number' || isCosmo); | ||
@@ -55,2 +55,6 @@ this.addIndex( | ||
this._failStack = []; | ||
this._failures = typeof isCosmo === 'number' ? isCosmo : 0; | ||
if (isCosmo) { | ||
@@ -68,2 +72,16 @@ this.addIndex( | ||
this.failuresIntervalMs = 600000; // 10 minutes | ||
this._killing = null; | ||
this.networkFailureErrorNames = [ | ||
'MongoServerSelectionError', | ||
'MongoNetworkError', | ||
'MongoNetworkTimeoutError', | ||
'MongoTopologyClosedError' | ||
]; | ||
this.killer = () => setTimeout(() => { | ||
process.exit(1); | ||
}, 10000); | ||
} | ||
@@ -106,34 +124,55 @@ | ||
async getOrCreateAndLock (senderId, pageId, defaultState = {}, timeout = 300) { | ||
const now = Date.now(); | ||
let now = Date.now(); | ||
try { | ||
const c = await this._getCollection(); | ||
const c = await this._getCollection(); | ||
const $setOnInsert = { | ||
state: defaultState, | ||
lastSendError: null, | ||
off: false | ||
}; | ||
const $setOnInsert = { | ||
state: defaultState, | ||
lastSendError: null, | ||
off: false | ||
}; | ||
const $set = { | ||
lock: now | ||
}; | ||
const $set = { | ||
lock: now | ||
}; | ||
const $lt = now - timeout; | ||
const $lt = now - timeout; | ||
const res = await c.findOneAndUpdate({ | ||
senderId, | ||
pageId, | ||
lock: { $lt } | ||
}, { | ||
$setOnInsert, | ||
$set | ||
}, { | ||
upsert: true, | ||
returnDocument: 'after', | ||
projection: { | ||
_id: 0 | ||
} | ||
}); | ||
const res = await c.findOneAndUpdate({ | ||
senderId, | ||
pageId, | ||
lock: { $lt } | ||
}, { | ||
$setOnInsert, | ||
$set | ||
}, { | ||
upsert: true, | ||
returnDocument: 'after', | ||
projection: { | ||
_id: 0 | ||
return res.value; | ||
} catch (e) { | ||
if (this._failures !== 0 | ||
&& this.networkFailureErrorNames.includes(e.name)) { | ||
now = Date.now(); | ||
this._failStack.push(now); | ||
if (this._failStack.length >= this._failures | ||
&& this._failStack.shift() > (now - this.failuresIntervalMs) | ||
&& this._killing === null) { | ||
this._log.error(`StateStorage: KILLING APP DUE FREQUENT NETWORK ERRORS ${this._failStack.length}`, e); | ||
// let it alive for following ten seconds to process all logs | ||
this._killing = this.killer() || null; | ||
} | ||
} | ||
}); | ||
return res.value; | ||
throw e; | ||
} | ||
} | ||
@@ -140,0 +179,0 @@ |
258075
2295