Merge pull request #3118 from overleaf/jpa-delay-mongo-connection-use

[scripts] batchedUpdate: try to work around flaky mongo connection setup

GitOrigin-RevId: 2bba7117b8f8892a41703569fef3f97d5e8d6835
This commit is contained in:
Jakob Ackermann 2020-08-18 16:08:01 +02:00 committed by Copybot
parent 25b9243b3f
commit 00e59a9afe

View file

@ -1,3 +1,4 @@
const { promisify } = require('util')
const { ReadPreference } = require('mongodb')
const { getNativeDb } = require('../../app/src/infrastructure/Mongoose')
@ -21,6 +22,12 @@ async function performUpdate(collection, nextBatch, update) {
}
async function batchedUpdate(collectionName, query, update) {
// Apparently the mongo driver returns the connection too early.
// Some secondary connections are not ready as it returns, leading to
// failing cursor actions with a readPreference set to 'secondary'.
// TODO(das7pad): revisit/remove this delay after the mongo-driver update.
await Promise.all([getNativeDb(), promisify(setTimeout)(10 * 1000)])
const db = await getNativeDb()
const collection = db.collection(collectionName)