From 00e59a9afe157aca20079079070a38760fa6c0b1 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Tue, 18 Aug 2020 16:08:01 +0200 Subject: [PATCH] Merge pull request #3118 from overleaf/jpa-delay-mongo-connection-use [scripts] batchedUpdate: try to work around flaky mongo connection setup GitOrigin-RevId: 2bba7117b8f8892a41703569fef3f97d5e8d6835 --- services/web/scripts/helpers/batchedUpdate.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/services/web/scripts/helpers/batchedUpdate.js b/services/web/scripts/helpers/batchedUpdate.js index cadca679d1..3f489519af 100644 --- a/services/web/scripts/helpers/batchedUpdate.js +++ b/services/web/scripts/helpers/batchedUpdate.js @@ -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)