From 41a65ee41e14af28b8daf5eb85e9698301382572 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Tue, 29 Sep 2020 12:22:22 +0100 Subject: [PATCH 1/2] [PackWorker] wait for the db setup before starting to process updates --- services/track-changes/app/js/PackWorker.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/services/track-changes/app/js/PackWorker.js b/services/track-changes/app/js/PackWorker.js index 74b5368368..500556cd79 100644 --- a/services/track-changes/app/js/PackWorker.js +++ b/services/track-changes/app/js/PackWorker.js @@ -18,7 +18,7 @@ let project_id, doc_id const Settings = require('settings-sharelatex') const async = require('async') const _ = require('underscore') -const { db, ObjectId } = require('./mongodb') +const { db, ObjectId, waitForDb } = require('./mongodb') const fs = require('fs') const Metrics = require('metrics-sharelatex') Metrics.initialize('track-changes') @@ -160,10 +160,21 @@ const ObjectIdFromDate = function (date) { // find packs to be marked as finalised:true, those which have a newer pack present // then only consider finalised:true packs for archiving -if (pending != null) { - logger.log(`got ${pending.length} entries from ${source}`) - processUpdates(pending) -} else { +waitForDb() + .then(() => { + if (pending != null) { + logger.log(`got ${pending.length} entries from ${source}`) + processUpdates(pending) + } else { + processFromOneWeekAgo() + } + }) + .catch((err) => { + logger.fatal({ err }, 'cannot connect to mongo, exiting') + process.exit(1) + }) + +function processFromOneWeekAgo() { const oneWeekAgo = new Date(Date.now() - 7 * DAYS) db.docHistory .find( From 2c8916f2e03d642d39b2df1fb7723c7cd3491e4b Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Tue, 29 Sep 2020 12:23:18 +0100 Subject: [PATCH 2/2] [PackWorker] fix the db cleanup process -- we do not expose db.close --- services/track-changes/app/js/PackWorker.js | 5 +++-- services/track-changes/app/js/mongodb.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/services/track-changes/app/js/PackWorker.js b/services/track-changes/app/js/PackWorker.js index 500556cd79..232d7b24ed 100644 --- a/services/track-changes/app/js/PackWorker.js +++ b/services/track-changes/app/js/PackWorker.js @@ -15,10 +15,11 @@ */ let LIMIT, pending let project_id, doc_id +const { callbackify } = require('util') const Settings = require('settings-sharelatex') const async = require('async') const _ = require('underscore') -const { db, ObjectId, waitForDb } = require('./mongodb') +const { db, ObjectId, waitForDb, closeDb } = require('./mongodb') const fs = require('fs') const Metrics = require('metrics-sharelatex') Metrics.initialize('track-changes') @@ -84,7 +85,7 @@ const finish = function () { clearTimeout(shutDownTimer) } logger.log('closing db') - return db.close(function () { + callbackify(closeDb)(function () { logger.log('closing LockManager Redis Connection') return LockManager.close(function () { logger.log( diff --git a/services/track-changes/app/js/mongodb.js b/services/track-changes/app/js/mongodb.js index 38ebe0462a..e0d50a165b 100644 --- a/services/track-changes/app/js/mongodb.js +++ b/services/track-changes/app/js/mongodb.js @@ -23,8 +23,20 @@ async function setupDb() { db.projectHistoryMetaData = internalDb.collection('projectHistoryMetaData') } +async function closeDb() { + let client + try { + client = await clientPromise + } catch (e) { + // there is nothing to close + return + } + return client.close() +} + module.exports = { db, ObjectId, + closeDb, waitForDb }