From a7517eefcbf9b16ea00268a804685946bdf7425f Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Thu, 31 Oct 2024 13:16:54 +0100 Subject: [PATCH] Merge pull request #21427 from overleaf/jpa-populate-db-on-import [web] populate db with collections on import, ahead of waitForDb() call GitOrigin-RevId: 7eb4cd61c2052187acd9947d7060f54d9822d314 --- services/web/app.mjs | 4 +- .../web/app/src/infrastructure/mongodb.js | 123 ++++++++---------- services/web/migrations/lib/adapter.mjs | 3 +- services/web/migrations/lib/helpers.mjs | 9 +- .../scripts/change-compile-timeout.js | 8 +- .../scripts/check-mongodb.js | 7 +- .../scripts/check-texlive-images.js | 3 +- .../server-ce-scripts/scripts/create-user.js | 4 +- .../server-ce-scripts/scripts/delete-user.js | 3 - .../scripts/migrate-user-emails.js | 2 - .../server-ce-scripts/scripts/rename-tag.js | 4 +- .../scripts/upgrade-user-features.js | 4 +- services/web/scripts/add_feature_override.mjs | 4 +- .../add_salesforce_data_to_subscriptions.mjs | 4 +- .../web/scripts/add_user_count_to_csv.mjs | 3 +- .../sync_group_subscription_memberships.mjs | 3 - .../web/scripts/back_fill_dummy_doc_meta.mjs | 3 - .../web/scripts/back_fill_staff_access.mjs | 2 - ...g_user_personal_and_group_subscription.mjs | 4 +- .../backfill_project_invites_token_hmac.mjs | 3 +- .../web/scripts/check_institution_users.mjs | 2 - services/web/scripts/clear_admin_sessions.mjs | 2 - .../web/scripts/clear_feedback_collection.mjs | 3 +- services/web/scripts/clear_project_tokens.mjs | 2 - .../clear_sessions_set_must_reconfirm.mjs | 4 +- .../web/scripts/confirmed_at_to_dates.mjs | 3 +- services/web/scripts/convert_doc_to_file.mjs | 3 +- .../scripts/count_encrypted_access_tokens.mjs | 3 - .../web/scripts/count_files_in_projects.mjs | 2 - services/web/scripts/count_image_files.mjs | 2 - services/web/scripts/count_project_size.mjs | 3 +- .../create_oauth_personal_access_token.mjs | 2 - services/web/scripts/create_project.mjs | 2 - .../delete_test_dupes.mjs | 3 +- .../delete-orphaned-docs.mjs | 8 +- .../web/scripts/delete_dangling_file_refs.mjs | 3 +- .../delete_orphaned_doc_comment_ranges.mjs | 3 - .../delete_orphaned_docs_online_check.mjs | 3 - services/web/scripts/delete_subscriptions.mjs | 17 +-- services/web/scripts/ensure_affiliations.mjs | 2 - .../web/scripts/export_institution_chat.mjs | 2 - .../web/scripts/find_malformed_filetrees.mjs | 2 - .../fix_group_invite_emails_to_lowercase.mjs | 4 +- .../web/scripts/fix_malformed_filetree.mjs | 4 +- services/web/scripts/fix_oversized_docs.mjs | 3 +- services/web/scripts/force_doc_flush.mjs | 4 +- .../web/scripts/helpers/batchedUpdate.mjs | 2 - .../scripts/history/clean_sl_history_data.mjs | 14 +- .../history/migrate_ranges_support.mjs | 2 - services/web/scripts/invalidate_tokens.mjs | 4 +- .../merge_group_subscription_members.mjs | 4 +- services/web/scripts/migrate_audit_logs.mjs | 3 +- .../scripts/oauth/backfill_hashed_secrets.mjs | 2 - services/web/scripts/oauth/create_token.mjs | 4 +- .../web/scripts/oauth/register_client.mjs | 3 +- services/web/scripts/oauth/remove_client.mjs | 2 - .../web/scripts/recover_docs_from_redis.mjs | 3 +- .../scripts/recurly/resync_subscriptions.mjs | 3 - services/web/scripts/refresh_features.mjs | 3 +- .../web/scripts/refresh_institution_users.mjs | 2 - ...e_deleted_users_from_token_access_refs.mjs | 2 - services/web/scripts/remove_email.mjs | 3 +- .../web/scripts/remove_oauth_application.mjs | 3 - .../web/scripts/restore_orphaned_docs.mjs | 2 - .../web/scripts/restore_soft_deleted_docs.mjs | 2 - services/web/scripts/soft_delete_project.mjs | 3 - .../split_tests_assigned_at_to_dates.mjs | 3 +- .../split_writefull_disabled_from_unset.mjs | 3 +- .../web/scripts/sso_id_migration_check.mjs | 13 +- .../scripts/sso_id_remove_not_migrated.mjs | 2 - services/web/scripts/stress_test.mjs | 2 +- services/web/scripts/ukamf/check-certs.js | 3 +- .../web/scripts/undelete_project_to_user.mjs | 2 - .../web/scripts/unlink_third_party_id.mjs | 3 - .../acceptance/src/helpers/MongoHelper.mjs | 6 +- 75 files changed, 124 insertions(+), 270 deletions(-) diff --git a/services/web/app.mjs b/services/web/app.mjs index 5b2851b687..3f50184025 100644 --- a/services/web/app.mjs +++ b/services/web/app.mjs @@ -59,7 +59,7 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) { PlansLocator.ensurePlansAreSetupCorrectly() - Promise.all([mongodb.waitForDb(), mongoose.connectionPromise]) + Promise.all([mongodb.connectionPromise, mongoose.connectionPromise]) .then(async () => { Server.server.listen(port, host, function () { logger.debug(`web starting up, listening on ${host}:${port}`) @@ -77,7 +77,7 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) { } // initialise site admin tasks -Promise.all([mongodb.waitForDb(), mongoose.connectionPromise]) +Promise.all([mongodb.connectionPromise, mongoose.connectionPromise]) .then(() => SiteAdminHandler.initialise()) .catch(err => { logger.fatal({ err }, 'Cannot connect to mongo. Exiting.') diff --git a/services/web/app/src/infrastructure/mongodb.js b/services/web/app/src/infrastructure/mongodb.js index 62ff5a8f0c..acc7f4781b 100644 --- a/services/web/app/src/infrastructure/mongodb.js +++ b/services/web/app/src/infrastructure/mongodb.js @@ -30,16 +30,6 @@ const READ_PREFERENCE_SECONDARY = Settings.mongo.hasSecondaries ? ReadPreference.secondary.mode : ReadPreference.secondaryPreferred.mode -let setupDbPromise -async function waitForDb() { - if (!setupDbPromise) { - setupDbPromise = setupDb() - } - await setupDbPromise -} - -const db = {} - const mongoClient = new mongodb.MongoClient( Settings.mongo.url, Settings.mongo.options @@ -49,67 +39,60 @@ addConnectionDrainer('mongodb', async () => { await mongoClient.close() }) -async function setupDb() { - const internalDb = mongoClient.db() - - db.contacts = internalDb.collection('contacts') - db.deletedFiles = internalDb.collection('deletedFiles') - db.deletedProjects = internalDb.collection('deletedProjects') - db.deletedSubscriptions = internalDb.collection('deletedSubscriptions') - db.deletedUsers = internalDb.collection('deletedUsers') - db.dropboxEntities = internalDb.collection('dropboxEntities') - db.dropboxProjects = internalDb.collection('dropboxProjects') - db.docHistory = internalDb.collection('docHistory') - db.docHistoryIndex = internalDb.collection('docHistoryIndex') - db.docSnapshots = internalDb.collection('docSnapshots') - db.docs = internalDb.collection('docs') - db.feedbacks = internalDb.collection('feedbacks') - db.githubSyncEntityVersions = internalDb.collection( - 'githubSyncEntityVersions' - ) - db.githubSyncProjectStates = internalDb.collection('githubSyncProjectStates') - db.githubSyncUserCredentials = internalDb.collection( - 'githubSyncUserCredentials' - ) - db.globalMetrics = internalDb.collection('globalMetrics') - db.grouppolicies = internalDb.collection('grouppolicies') - db.institutions = internalDb.collection('institutions') - db.messages = internalDb.collection('messages') - db.migrations = internalDb.collection('migrations') - db.notifications = internalDb.collection('notifications') - db.oauthAccessTokens = internalDb.collection('oauthAccessTokens') - db.oauthApplications = internalDb.collection('oauthApplications') - db.oauthAuthorizationCodes = internalDb.collection('oauthAuthorizationCodes') - db.projectAuditLogEntries = internalDb.collection('projectAuditLogEntries') - db.projectHistoryChunks = internalDb.collection('projectHistoryChunks') - db.projectHistoryFailures = internalDb.collection('projectHistoryFailures') - db.projectHistoryLabels = internalDb.collection('projectHistoryLabels') - db.projectHistoryMetaData = internalDb.collection('projectHistoryMetaData') - db.projectHistorySyncState = internalDb.collection('projectHistorySyncState') - db.projectInvites = internalDb.collection('projectInvites') - db.projects = internalDb.collection('projects') - db.publishers = internalDb.collection('publishers') - db.rooms = internalDb.collection('rooms') - db.samlCache = internalDb.collection('samlCache') - db.samlLogs = internalDb.collection('samlLogs') - db.spellingPreferences = internalDb.collection('spellingPreferences') - db.splittests = internalDb.collection('splittests') - db.ssoConfigs = internalDb.collection('ssoConfigs') - db.subscriptions = internalDb.collection('subscriptions') - db.surveys = internalDb.collection('surveys') - db.systemmessages = internalDb.collection('systemmessages') - db.tags = internalDb.collection('tags') - db.teamInvites = internalDb.collection('teamInvites') - db.tokens = internalDb.collection('tokens') - db.userAuditLogEntries = internalDb.collection('userAuditLogEntries') - db.users = internalDb.collection('users') - db.onboardingDataCollection = internalDb.collection( - 'onboardingDataCollection' - ) - - await mongoClient.connect() +const internalDb = mongoClient.db() +const db = { + contacts: internalDb.collection('contacts'), + deletedFiles: internalDb.collection('deletedFiles'), + deletedProjects: internalDb.collection('deletedProjects'), + deletedSubscriptions: internalDb.collection('deletedSubscriptions'), + deletedUsers: internalDb.collection('deletedUsers'), + dropboxEntities: internalDb.collection('dropboxEntities'), + dropboxProjects: internalDb.collection('dropboxProjects'), + docHistory: internalDb.collection('docHistory'), + docHistoryIndex: internalDb.collection('docHistoryIndex'), + docSnapshots: internalDb.collection('docSnapshots'), + docs: internalDb.collection('docs'), + feedbacks: internalDb.collection('feedbacks'), + githubSyncEntityVersions: internalDb.collection('githubSyncEntityVersions'), + githubSyncProjectStates: internalDb.collection('githubSyncProjectStates'), + githubSyncUserCredentials: internalDb.collection('githubSyncUserCredentials'), + globalMetrics: internalDb.collection('globalMetrics'), + grouppolicies: internalDb.collection('grouppolicies'), + institutions: internalDb.collection('institutions'), + messages: internalDb.collection('messages'), + migrations: internalDb.collection('migrations'), + notifications: internalDb.collection('notifications'), + oauthAccessTokens: internalDb.collection('oauthAccessTokens'), + oauthApplications: internalDb.collection('oauthApplications'), + oauthAuthorizationCodes: internalDb.collection('oauthAuthorizationCodes'), + projectAuditLogEntries: internalDb.collection('projectAuditLogEntries'), + projectHistoryChunks: internalDb.collection('projectHistoryChunks'), + projectHistoryFailures: internalDb.collection('projectHistoryFailures'), + projectHistoryLabels: internalDb.collection('projectHistoryLabels'), + projectHistoryMetaData: internalDb.collection('projectHistoryMetaData'), + projectHistorySyncState: internalDb.collection('projectHistorySyncState'), + projectInvites: internalDb.collection('projectInvites'), + projects: internalDb.collection('projects'), + publishers: internalDb.collection('publishers'), + rooms: internalDb.collection('rooms'), + samlCache: internalDb.collection('samlCache'), + samlLogs: internalDb.collection('samlLogs'), + spellingPreferences: internalDb.collection('spellingPreferences'), + splittests: internalDb.collection('splittests'), + ssoConfigs: internalDb.collection('ssoConfigs'), + subscriptions: internalDb.collection('subscriptions'), + surveys: internalDb.collection('surveys'), + systemmessages: internalDb.collection('systemmessages'), + tags: internalDb.collection('tags'), + teamInvites: internalDb.collection('teamInvites'), + tokens: internalDb.collection('tokens'), + userAuditLogEntries: internalDb.collection('userAuditLogEntries'), + users: internalDb.collection('users'), + onboardingDataCollection: internalDb.collection('onboardingDataCollection'), } +const connectionPromise = mongoClient.connect() + async function getCollectionNames() { const internalDb = mongoClient.db() @@ -142,10 +125,10 @@ async function getCollectionInternal(name) { module.exports = { db, ObjectId, + connectionPromise, getCollectionNames, getCollectionInternal, dropTestDatabase, - waitForDb, READ_PREFERENCE_PRIMARY, READ_PREFERENCE_SECONDARY, } diff --git a/services/web/migrations/lib/adapter.mjs b/services/web/migrations/lib/adapter.mjs index a7628f7e8c..61b22de094 100644 --- a/services/web/migrations/lib/adapter.mjs +++ b/services/web/migrations/lib/adapter.mjs @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url' const __filename = fileURLToPath(import.meta.url) const __dirname = Path.dirname(__filename) -const { db, waitForDb } = mongodb +const { db } = mongodb const { getNativeDb } = Mongoose class Adapter { @@ -26,7 +26,6 @@ class Adapter { } async connect() { - await waitForDb() const nativeDb = await getNativeDb() return { db, nativeDb } } diff --git a/services/web/migrations/lib/helpers.mjs b/services/web/migrations/lib/helpers.mjs index 0176e0c0b8..1870737848 100644 --- a/services/web/migrations/lib/helpers.mjs +++ b/services/web/migrations/lib/helpers.mjs @@ -1,7 +1,10 @@ // @ts-check -import mongodb from '../../app/src/infrastructure/mongodb.js' -const { db, getCollectionNames, getCollectionInternal, waitForDb } = mongodb +import { + db, + getCollectionNames, + getCollectionInternal, +} from '../../app/src/infrastructure/mongodb.js' async function addIndexesToCollection(collection, indexes) { return Promise.all( @@ -29,7 +32,6 @@ async function dropIndexesFromCollection(collection, indexes) { } async function dropCollection(collectionName) { - await waitForDb() if (db[collectionName]) { throw new Error(`blocking drop of an active collection: ${collectionName}`) } @@ -46,7 +48,6 @@ async function dropCollection(collectionName) { * @param {string} migrationName */ async function assertDependency(migrationName) { - await waitForDb() const migrations = await getCollectionInternal('migrations') const migration = await migrations.findOne({ name: migrationName }) if (migration == null) { diff --git a/services/web/modules/server-ce-scripts/scripts/change-compile-timeout.js b/services/web/modules/server-ce-scripts/scripts/change-compile-timeout.js index ca99e6233f..a6e5e403ec 100644 --- a/services/web/modules/server-ce-scripts/scripts/change-compile-timeout.js +++ b/services/web/modules/server-ce-scripts/scripts/change-compile-timeout.js @@ -1,13 +1,7 @@ const minimist = require('minimist') -const { - db, - ObjectId, - waitForDb, -} = require('../../../app/src/infrastructure/mongodb') +const { db, ObjectId } = require('../../../app/src/infrastructure/mongodb') async function main() { - await waitForDb() - const argv = minimist(process.argv.slice(2), { string: ['user-id', 'compile-timeout'], }) diff --git a/services/web/modules/server-ce-scripts/scripts/check-mongodb.js b/services/web/modules/server-ce-scripts/scripts/check-mongodb.js index 3dd9f33b61..f096a46420 100644 --- a/services/web/modules/server-ce-scripts/scripts/check-mongodb.js +++ b/services/web/modules/server-ce-scripts/scripts/check-mongodb.js @@ -1,12 +1,15 @@ const { ObjectId } = require('mongodb-legacy') -const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb') +const { + connectionPromise, + db, +} = require('../../../app/src/infrastructure/mongodb') const { getMongoClient } = require('../../../app/src/infrastructure/Mongoose') const MIN_MONGO_VERSION = [5, 0] async function main() { try { - await waitForDb() + await connectionPromise } catch (err) { console.error('Cannot connect to mongodb') throw err diff --git a/services/web/modules/server-ce-scripts/scripts/check-texlive-images.js b/services/web/modules/server-ce-scripts/scripts/check-texlive-images.js index 4f474650ff..15d53b0ced 100644 --- a/services/web/modules/server-ce-scripts/scripts/check-texlive-images.js +++ b/services/web/modules/server-ce-scripts/scripts/check-texlive-images.js @@ -1,7 +1,6 @@ -const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb') +const { db } = require('../../../app/src/infrastructure/mongodb') async function readImagesInUse() { - await waitForDb() const projectCount = await db.projects.countDocuments() if (projectCount === 0) { return [] diff --git a/services/web/modules/server-ce-scripts/scripts/create-user.js b/services/web/modules/server-ce-scripts/scripts/create-user.js index 031c78fff2..97bc6add4b 100644 --- a/services/web/modules/server-ce-scripts/scripts/create-user.js +++ b/services/web/modules/server-ce-scripts/scripts/create-user.js @@ -1,10 +1,8 @@ const minimist = require('minimist') -const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb') +const { db } = require('../../../app/src/infrastructure/mongodb') const UserRegistrationHandler = require('../../../app/src/Features/User/UserRegistrationHandler') async function main() { - await waitForDb() - const argv = minimist(process.argv.slice(2), { string: ['email'], boolean: ['admin'], diff --git a/services/web/modules/server-ce-scripts/scripts/delete-user.js b/services/web/modules/server-ce-scripts/scripts/delete-user.js index ff1060984a..2d788d3873 100644 --- a/services/web/modules/server-ce-scripts/scripts/delete-user.js +++ b/services/web/modules/server-ce-scripts/scripts/delete-user.js @@ -1,10 +1,7 @@ -const { waitForDb } = require('../../../app/src/infrastructure/mongodb') const UserGetter = require('../../../app/src/Features/User/UserGetter') const UserDeleter = require('../../../app/src/Features/User/UserDeleter') async function main() { - await waitForDb() - const email = (process.argv.slice(2).pop() || '').replace(/^--email=/, '') if (!email) { console.error(`Usage: node ${__filename} --email=joe@example.com`) diff --git a/services/web/modules/server-ce-scripts/scripts/migrate-user-emails.js b/services/web/modules/server-ce-scripts/scripts/migrate-user-emails.js index d266ad46d2..0476e96ce8 100644 --- a/services/web/modules/server-ce-scripts/scripts/migrate-user-emails.js +++ b/services/web/modules/server-ce-scripts/scripts/migrate-user-emails.js @@ -10,7 +10,6 @@ // another. const minimist = require('minimist') -const { waitForDb } = require('../../../app/src/infrastructure/mongodb') const os = require('os') const fs = require('fs') @@ -182,7 +181,6 @@ async function migrateEmails() { const csvFile = fs.readFileSync(csvFilePath, 'utf8') const rows = csv.parse(csvFile) console.log('Number of users to migrate: ', rows.length) - await waitForDb() const emails = filterEmails(rows) const existingUserEmails = await checkEmailsAgainstDb(emails) await doMigration(existingUserEmails) diff --git a/services/web/modules/server-ce-scripts/scripts/rename-tag.js b/services/web/modules/server-ce-scripts/scripts/rename-tag.js index 51fa4377b6..f480fc036b 100644 --- a/services/web/modules/server-ce-scripts/scripts/rename-tag.js +++ b/services/web/modules/server-ce-scripts/scripts/rename-tag.js @@ -1,9 +1,7 @@ const minimist = require('minimist') -const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb') +const { db } = require('../../../app/src/infrastructure/mongodb') async function main() { - await waitForDb() - const argv = minimist(process.argv.slice(2), { string: ['user-id', 'old-name', 'new_name'], }) diff --git a/services/web/modules/server-ce-scripts/scripts/upgrade-user-features.js b/services/web/modules/server-ce-scripts/scripts/upgrade-user-features.js index eb511fc298..2cb09c0592 100644 --- a/services/web/modules/server-ce-scripts/scripts/upgrade-user-features.js +++ b/services/web/modules/server-ce-scripts/scripts/upgrade-user-features.js @@ -1,6 +1,6 @@ const Settings = require('@overleaf/settings') const logger = require('@overleaf/logger') -const { db, waitForDb } = require('../../../app/src/infrastructure/mongodb') +const { db } = require('../../../app/src/infrastructure/mongodb') const { mergeFeatures, compareFeatures, @@ -8,8 +8,6 @@ const { const DRY_RUN = !process.argv.includes('--dry-run=false') async function main(DRY_RUN, defaultFeatures) { - await waitForDb() - logger.info({ defaultFeatures }, 'default features') const cursor = db.users.find( diff --git a/services/web/scripts/add_feature_override.mjs b/services/web/scripts/add_feature_override.mjs index 87c6869865..2ee2d6cb09 100644 --- a/services/web/scripts/add_feature_override.mjs +++ b/services/web/scripts/add_feature_override.mjs @@ -26,7 +26,7 @@ import minimist from 'minimist' import fs from 'fs' -import { ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { ObjectId } from '../app/src/infrastructure/mongodb.js' import pLimit from 'p-limit' import FeaturesUpdater from '../app/src/Features/Subscription/FeaturesUpdater.js' import FeaturesHelper from '../app/src/Features/Subscription/FeaturesHelper.js' @@ -162,8 +162,6 @@ async function processUsers(userIds) { : 'Every user in file will get feature override' ) - await waitForDb() - _validateUserIdList(userIds) console.log(`---Starting to process ${userIds.length} users---`) diff --git a/services/web/scripts/add_salesforce_data_to_subscriptions.mjs b/services/web/scripts/add_salesforce_data_to_subscriptions.mjs index b2584ed584..933cc7ce44 100755 --- a/services/web/scripts/add_salesforce_data_to_subscriptions.mjs +++ b/services/web/scripts/add_salesforce_data_to_subscriptions.mjs @@ -2,7 +2,7 @@ import fs from 'node:fs' import minimist from 'minimist' import { parse } from 'csv' import Stream from 'stream/promises' -import { ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { ObjectId } from '../app/src/infrastructure/mongodb.js' import { Subscription } from '../app/src/models/Subscription.js' function usage() { @@ -162,8 +162,6 @@ async function processRows(rows) { } async function main() { - await waitForDb() - await Stream.pipeline( fs.createReadStream(filename), parse({ diff --git a/services/web/scripts/add_user_count_to_csv.mjs b/services/web/scripts/add_user_count_to_csv.mjs index 494988c3f6..c841dab133 100644 --- a/services/web/scripts/add_user_count_to_csv.mjs +++ b/services/web/scripts/add_user_count_to_csv.mjs @@ -7,7 +7,7 @@ import fs from 'fs' import * as csv from 'csv/sync' import minimist from 'minimist' import UserGetter from '../app/src/Features/User/UserGetter.js' -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import _ from 'lodash' const argv = minimist(process.argv.slice(2), { @@ -45,7 +45,6 @@ if (records.length === 0) { } async function main() { - await waitForDb() for (const record of records) { const domain = record[argv.domain] const { domainUserCount, subdomainUserCount } = await getUserCount(domain, { diff --git a/services/web/scripts/analytics/sync_group_subscription_memberships.mjs b/services/web/scripts/analytics/sync_group_subscription_memberships.mjs index fc448cc2c7..cb41dd03f5 100644 --- a/services/web/scripts/analytics/sync_group_subscription_memberships.mjs +++ b/services/web/scripts/analytics/sync_group_subscription_memberships.mjs @@ -1,6 +1,5 @@ import GoogleBigQueryHelper from './helpers/GoogleBigQueryHelper.mjs' import { Subscription } from '../../app/src/models/Subscription.js' -import { waitForDb } from '../../app/src/infrastructure/mongodb.js' import AnalyticsManager from '../../app/src/Features/Analytics/AnalyticsManager.js' import { DeletedSubscription } from '../../app/src/models/DeletedSubscription.js' import minimist from 'minimist' @@ -12,8 +11,6 @@ const { ObjectId } = mongodb let FETCH_LIMIT, COMMIT, VERBOSE async function main() { - await waitForDb() - console.log('## Syncing group subscription memberships...') const subscriptionsCount = await Subscription.countDocuments({ diff --git a/services/web/scripts/back_fill_dummy_doc_meta.mjs b/services/web/scripts/back_fill_dummy_doc_meta.mjs index 29db777cf0..2a6726e1f6 100644 --- a/services/web/scripts/back_fill_dummy_doc_meta.mjs +++ b/services/web/scripts/back_fill_dummy_doc_meta.mjs @@ -2,7 +2,6 @@ import { promisify } from 'util' import mongodb from 'mongodb-legacy' import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' import _ from 'lodash' @@ -50,8 +49,6 @@ async function main(options) { }) await letUserDoubleCheckInputs(options) - await waitForDb() - let startId = options.firstProjectId let nProcessed = 0 diff --git a/services/web/scripts/back_fill_staff_access.mjs b/services/web/scripts/back_fill_staff_access.mjs index 569e6ad407..c26958e8d9 100644 --- a/services/web/scripts/back_fill_staff_access.mjs +++ b/services/web/scripts/back_fill_staff_access.mjs @@ -1,6 +1,5 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' import UserSessionsManager from '../app/src/Features/User/UserSessionsManager.js' @@ -39,7 +38,6 @@ function formatUser(user) { } async function main() { - await waitForDb() const adminUsers = await db.users .find( { isAdmin: true }, diff --git a/services/web/scripts/back_fill_warning_user_personal_and_group_subscription.mjs b/services/web/scripts/back_fill_warning_user_personal_and_group_subscription.mjs index cd522965b0..65f7b124f1 100644 --- a/services/web/scripts/back_fill_warning_user_personal_and_group_subscription.mjs +++ b/services/web/scripts/back_fill_warning_user_personal_and_group_subscription.mjs @@ -1,5 +1,5 @@ import NotificationsBuilder from '../app/src/Features/Notifications/NotificationsBuilder.js' -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import BatchedUpdateModule from './helpers/batchedUpdate.mjs' const { batchedUpdate } = BatchedUpdateModule @@ -57,8 +57,6 @@ async function processBatch(groupSubscriptionsBatch) { } async function main() { - await waitForDb() - await batchedUpdate('subscriptions', { groupPlan: true }, processBatch, { member_ids: 1, }) diff --git a/services/web/scripts/backfill_project_invites_token_hmac.mjs b/services/web/scripts/backfill_project_invites_token_hmac.mjs index 7423fb8512..6e3a526c88 100644 --- a/services/web/scripts/backfill_project_invites_token_hmac.mjs +++ b/services/web/scripts/backfill_project_invites_token_hmac.mjs @@ -1,4 +1,4 @@ -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import BatchedUpdateModule from './helpers/batchedUpdate.mjs' import minimist from 'minimist' import CollaboratorsInviteHelper from '../app/src/Features/Collaborators/CollaboratorsInviteHelper.js' @@ -53,7 +53,6 @@ async function addTokenHmacField(DRY_RUN) { } async function main(DRY_RUN) { - await waitForDb() await addTokenHmacField(DRY_RUN) } diff --git a/services/web/scripts/check_institution_users.mjs b/services/web/scripts/check_institution_users.mjs index a9c218fc5e..e89c0d114b 100644 --- a/services/web/scripts/check_institution_users.mjs +++ b/services/web/scripts/check_institution_users.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import InstitutionsManager from '../app/src/Features/Institutions/InstitutionsManager.js' import { ensureRunningOnMongoSecondaryWithTimeout } from './helpers/env_variable_helper.mjs' @@ -19,7 +18,6 @@ async function main() { } try { - await waitForDb() await main() } catch (error) { console.error(error) diff --git a/services/web/scripts/clear_admin_sessions.mjs b/services/web/scripts/clear_admin_sessions.mjs index 10a3b679cc..3d5623c864 100644 --- a/services/web/scripts/clear_admin_sessions.mjs +++ b/services/web/scripts/clear_admin_sessions.mjs @@ -1,6 +1,5 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' import UserSessionsManager from '../app/src/Features/User/UserSessionsManager.js' @@ -9,7 +8,6 @@ const COMMIT = process.argv.includes('--commit') const LOG_SESSIONS = !process.argv.includes('--log-sessions=false') async function main() { - await waitForDb() const adminUsers = await db.users .find( { isAdmin: true }, diff --git a/services/web/scripts/clear_feedback_collection.mjs b/services/web/scripts/clear_feedback_collection.mjs index ff886f1cc5..57fd376f01 100644 --- a/services/web/scripts/clear_feedback_collection.mjs +++ b/services/web/scripts/clear_feedback_collection.mjs @@ -5,11 +5,10 @@ * DRY_RUN=false node scripts/clear_feedback_collection.js 2022-11-01 # deletion mode */ -import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db, ObjectId } from '../app/src/infrastructure/mongodb.js' import { fileURLToPath } from 'url' const runScript = async (timestamp, dryRun) => { - await waitForDb() const t = new Date(timestamp) if (isNaN(t)) { throw new Error('invalid date ' + timestamp) diff --git a/services/web/scripts/clear_project_tokens.mjs b/services/web/scripts/clear_project_tokens.mjs index a6c8b4c92c..ab3d3c5a84 100644 --- a/services/web/scripts/clear_project_tokens.mjs +++ b/services/web/scripts/clear_project_tokens.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ProjectDetailsHandler from '../app/src/Features/Project/ProjectDetailsHandler.js' const projectId = process.argv[2] @@ -22,7 +21,6 @@ function main() { } try { - await waitForDb() await main() } catch (error) { console.error(error) diff --git a/services/web/scripts/clear_sessions_set_must_reconfirm.mjs b/services/web/scripts/clear_sessions_set_must_reconfirm.mjs index c1a562523a..9b0ea35e38 100644 --- a/services/web/scripts/clear_sessions_set_must_reconfirm.mjs +++ b/services/web/scripts/clear_sessions_set_must_reconfirm.mjs @@ -1,5 +1,5 @@ import fs from 'fs' -import { ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { ObjectId } from '../app/src/infrastructure/mongodb.js' import async from 'async' import UserUpdater from '../app/src/Features/User/UserUpdater.js' import UserSessionsManager from '../app/src/Features/User/UserSessionsManager.js' @@ -73,8 +73,6 @@ const userIds = usersFile async function processUsers(userIds) { console.log('---Starting set_must_reconfirm script---') - await waitForDb() - _validateUserIdList(userIds) console.log(`---Starting to process ${userIds.length} users---`) await _loopUsers(userIds) diff --git a/services/web/scripts/confirmed_at_to_dates.mjs b/services/web/scripts/confirmed_at_to_dates.mjs index 41dd032044..cd03679577 100644 --- a/services/web/scripts/confirmed_at_to_dates.mjs +++ b/services/web/scripts/confirmed_at_to_dates.mjs @@ -1,8 +1,7 @@ -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import { fileURLToPath } from 'url' async function updateStringDates() { - await waitForDb() const users = await db.users.aggregate([ { $unwind: { path: '$emails' } }, { diff --git a/services/web/scripts/convert_doc_to_file.mjs b/services/web/scripts/convert_doc_to_file.mjs index e5cd6a5583..db4cb8b309 100644 --- a/services/web/scripts/convert_doc_to_file.mjs +++ b/services/web/scripts/convert_doc_to_file.mjs @@ -1,5 +1,5 @@ import minimist from 'minimist' -import { waitForDb, ObjectId } from '../app/src/infrastructure/mongodb.js' +import { ObjectId } from '../app/src/infrastructure/mongodb.js' import ProjectEntityUpdateHandler from '../app/src/Features/Project/ProjectEntityUpdateHandler.js' import Errors from '../app/src/Features/Errors/Errors.js' @@ -16,7 +16,6 @@ async function main() { } console.log(`Converting doc ${projectId}/${docId} as user ${userId}`) - await waitForDb() try { await ProjectEntityUpdateHandler.promises.convertDocToFile( projectId, diff --git a/services/web/scripts/count_encrypted_access_tokens.mjs b/services/web/scripts/count_encrypted_access_tokens.mjs index 929dc49961..003179f3eb 100644 --- a/services/web/scripts/count_encrypted_access_tokens.mjs +++ b/services/web/scripts/count_encrypted_access_tokens.mjs @@ -1,6 +1,5 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' import _ from 'lodash' @@ -56,8 +55,6 @@ async function count(collectionName, paths) { } async function main() { - await waitForDb() - const STATS = {} for (const [collectionName, paths] of Object.entries(CASES)) { const stats = await count(collectionName, paths) diff --git a/services/web/scripts/count_files_in_projects.mjs b/services/web/scripts/count_files_in_projects.mjs index 1279e85dc6..a0e2156853 100644 --- a/services/web/scripts/count_files_in_projects.mjs +++ b/services/web/scripts/count_files_in_projects.mjs @@ -1,5 +1,4 @@ import readline from 'readline' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ProjectEntityHandler from '../app/src/Features/Project/ProjectEntityHandler.js' import ProjectGetter from '../app/src/Features/Project/ProjectGetter.js' import Errors from '../app/src/Features/Errors/Errors.js' @@ -35,7 +34,6 @@ async function countFiles() { } try { - await waitForDb() await countFiles() process.exit(0) } catch (error) { diff --git a/services/web/scripts/count_image_files.mjs b/services/web/scripts/count_image_files.mjs index 74cd5d9235..17ad4b84ab 100644 --- a/services/web/scripts/count_image_files.mjs +++ b/services/web/scripts/count_image_files.mjs @@ -1,6 +1,5 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' import { extname } from 'node:path' @@ -22,7 +21,6 @@ const FILE_TYPES = [ const longestFileType = Math.max(...FILE_TYPES.map(fileType => fileType.length)) async function main() { - await waitForDb() const projects = db.projects.find( {}, { diff --git a/services/web/scripts/count_project_size.mjs b/services/web/scripts/count_project_size.mjs index bdab6f53d2..7a3a7bd1e6 100644 --- a/services/web/scripts/count_project_size.mjs +++ b/services/web/scripts/count_project_size.mjs @@ -1,5 +1,5 @@ import readline from 'readline' -import { waitForDb, ObjectId, db } from '../app/src/infrastructure/mongodb.js' +import { ObjectId, db } from '../app/src/infrastructure/mongodb.js' import ProjectEntityHandler from '../app/src/Features/Project/ProjectEntityHandler.js' import ProjectGetter from '../app/src/Features/Project/ProjectGetter.js' import Errors from '../app/src/Features/Errors/Errors.js' @@ -127,7 +127,6 @@ async function countDocsSizes(docs) { } try { - await waitForDb() await countProjectFiles() process.exit(0) } catch (error) { diff --git a/services/web/scripts/create_oauth_personal_access_token.mjs b/services/web/scripts/create_oauth_personal_access_token.mjs index 25ddbdc4a4..b88b72319d 100644 --- a/services/web/scripts/create_oauth_personal_access_token.mjs +++ b/services/web/scripts/create_oauth_personal_access_token.mjs @@ -4,7 +4,6 @@ import parseArgs from 'minimist' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import OAuthPersonalAccessTokenManager from '../modules/oauth2-server/app/src/OAuthPersonalAccessTokenManager.js' const argv = parseArgs(process.argv.slice(2), { @@ -19,7 +18,6 @@ if (!userId) { } async function createPersonalAccessToken() { - await waitForDb() const accessToken = await OAuthPersonalAccessTokenManager.createToken(userId) console.log('Personal Access Token: ' + accessToken) } diff --git a/services/web/scripts/create_project.mjs b/services/web/scripts/create_project.mjs index 12dc7710a1..2ebe35ad99 100644 --- a/services/web/scripts/create_project.mjs +++ b/services/web/scripts/create_project.mjs @@ -8,7 +8,6 @@ import path from 'path' import _ from 'lodash' import parseArgs from 'minimist' import OError from '@overleaf/o-error' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import { User } from '../app/src/models/User.js' import ProjectCreationHandler from '../app/src/Features/Project/ProjectCreationHandler.js' import ProjectEntityUpdateHandler from '../app/src/Features/Project/ProjectEntityUpdateHandler.js' @@ -207,7 +206,6 @@ async function _applyRandomDocUpdate(ownerId, projectId) { } async function createProject() { - await waitForDb() const user = await User.findById(userId) console.log('Will create project') console.log('user_id:', userId, '=>', user.email) diff --git a/services/web/scripts/delete-duplicate-splittest-versions/delete_test_dupes.mjs b/services/web/scripts/delete-duplicate-splittest-versions/delete_test_dupes.mjs index b81432b3c0..9daabc78e6 100644 --- a/services/web/scripts/delete-duplicate-splittest-versions/delete_test_dupes.mjs +++ b/services/web/scripts/delete-duplicate-splittest-versions/delete_test_dupes.mjs @@ -1,4 +1,4 @@ -import { db, waitForDb } from '../../app/src/infrastructure/mongodb.js' +import { db } from '../../app/src/infrastructure/mongodb.js' import minimist from 'minimist' const argv = minimist(process.argv.slice(2)) @@ -9,7 +9,6 @@ if (!commit) { } async function getDupes(commit) { - await waitForDb() const entries = await db.splittests.aggregate([ { $match: { diff --git a/services/web/scripts/delete-orphaned-docs/delete-orphaned-docs.mjs b/services/web/scripts/delete-orphaned-docs/delete-orphaned-docs.mjs index 47252ef186..e0fa7cd165 100644 --- a/services/web/scripts/delete-orphaned-docs/delete-orphaned-docs.mjs +++ b/services/web/scripts/delete-orphaned-docs/delete-orphaned-docs.mjs @@ -1,11 +1,7 @@ import fs from 'fs' import minimist from 'minimist' import readline from 'readline' -import { - db, - ObjectId, - waitForDb, -} from '../../app/src/infrastructure/mongodb.js' +import { db, ObjectId } from '../../app/src/infrastructure/mongodb.js' import DocstoreManagerModule from '../../app/src/Features/Docstore/DocstoreManager.js' const { promises: DocstoreManager } = DocstoreManagerModule @@ -66,8 +62,6 @@ rl.on('close', async () => { console.log(`Loaded Data for ${docCount} docs in ${projectCount} Projects`) - await waitForDb() - for (const projectId of Object.keys(orphanedDocs)) { await deleteOrphanedDocs(projectId, orphanedDocs[projectId]) } diff --git a/services/web/scripts/delete_dangling_file_refs.mjs b/services/web/scripts/delete_dangling_file_refs.mjs index 37d18a6b4f..262376aa7d 100644 --- a/services/web/scripts/delete_dangling_file_refs.mjs +++ b/services/web/scripts/delete_dangling_file_refs.mjs @@ -5,7 +5,7 @@ import minimist from 'minimist' import mongodb from 'mongodb-legacy' -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import Errors from '../app/src/Features/Errors/Errors.js' import FileStoreHandler from '../app/src/Features/FileStore/FileStoreHandler.js' import ProjectEntityMongoUpdateHandler from '../app/src/Features/Project/ProjectEntityMongoUpdateHandler.js' @@ -30,7 +30,6 @@ function parseArgs() { } async function main() { - await waitForDb() const projects = await getProjects() for (const project of projects) { diff --git a/services/web/scripts/delete_orphaned_doc_comment_ranges.mjs b/services/web/scripts/delete_orphaned_doc_comment_ranges.mjs index c9f80ea648..5b9a39714c 100644 --- a/services/web/scripts/delete_orphaned_doc_comment_ranges.mjs +++ b/services/web/scripts/delete_orphaned_doc_comment_ranges.mjs @@ -1,5 +1,4 @@ import minimist from 'minimist' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ChatApiHandler from '../app/src/Features/Chat/ChatApiHandler.js' import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js' import DocumentUpdaterHandler from '../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js' @@ -14,8 +13,6 @@ const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10 * for more detail. */ async function main() { - await waitForDb() - const argv = minimist(process.argv.slice(2)) const { projectId, docId } = argv diff --git a/services/web/scripts/delete_orphaned_docs_online_check.mjs b/services/web/scripts/delete_orphaned_docs_online_check.mjs index e4eeaa0b04..c571d6bd68 100644 --- a/services/web/scripts/delete_orphaned_docs_online_check.mjs +++ b/services/web/scripts/delete_orphaned_docs_online_check.mjs @@ -3,7 +3,6 @@ import { promisify } from 'util' import mongodb from 'mongodb-legacy' import { db, - waitForDb, READ_PREFERENCE_PRIMARY, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' @@ -41,8 +40,6 @@ function getSecondsFromObjectId(id) { async function main() { await letUserDoubleCheckInputs() - await waitForDb() - let lowerProjectId = BATCH_LAST_ID let nProjectsProcessedTotal = 0 diff --git a/services/web/scripts/delete_subscriptions.mjs b/services/web/scripts/delete_subscriptions.mjs index fddbf3149a..54fe472447 100644 --- a/services/web/scripts/delete_subscriptions.mjs +++ b/services/web/scripts/delete_subscriptions.mjs @@ -1,7 +1,6 @@ import { Subscription } from '../app/src/models/Subscription.js' import SubscriptionUpdater from '../app/src/Features/Subscription/SubscriptionUpdater.js' import minimist from 'minimist' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import mongodb from 'mongodb-legacy' const { ObjectId } = mongodb @@ -39,12 +38,10 @@ const setup = () => { setup() -waitForDb() - .then(run) - .then(() => { - process.exit(0) - }) - .catch(err => { - console.log('Aiee, something went wrong!', err) - process.exit(1) - }) +try { + await run() + process.exit(0) +} catch (err) { + console.error('Aiee, something went wrong!', err) + process.exit(1) +} diff --git a/services/web/scripts/ensure_affiliations.mjs b/services/web/scripts/ensure_affiliations.mjs index 9c986c92e7..f6376614e7 100644 --- a/services/web/scripts/ensure_affiliations.mjs +++ b/services/web/scripts/ensure_affiliations.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import { User } from '../app/src/models/User.js' import UserController from '../app/src/Features/User/UserController.js' import Logger from '@overleaf/logger' @@ -45,7 +44,6 @@ async function run() { } try { - await waitForDb() await run() process.exit() } catch (error) { diff --git a/services/web/scripts/export_institution_chat.mjs b/services/web/scripts/export_institution_chat.mjs index 73eaf2cb64..e201b51670 100644 --- a/services/web/scripts/export_institution_chat.mjs +++ b/services/web/scripts/export_institution_chat.mjs @@ -1,7 +1,6 @@ import { db, READ_PREFERENCE_SECONDARY, - waitForDb, ObjectId, } from '../app/src/infrastructure/mongodb.js' import minimist from 'minimist' @@ -76,7 +75,6 @@ function getUserMappings(affiliations) { async function main() { const args = parseArgs() - await waitForDb() const affiliations = await fetchInstitutionAndAffiliations(args.institutionId) const userMappings = getUserMappings(affiliations) diff --git a/services/web/scripts/find_malformed_filetrees.mjs b/services/web/scripts/find_malformed_filetrees.mjs index da711ba676..2b3901b510 100644 --- a/services/web/scripts/find_malformed_filetrees.mjs +++ b/services/web/scripts/find_malformed_filetrees.mjs @@ -1,11 +1,9 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' async function main() { - await waitForDb() const projects = db.projects.find( {}, { diff --git a/services/web/scripts/fix_group_invite_emails_to_lowercase.mjs b/services/web/scripts/fix_group_invite_emails_to_lowercase.mjs index e70917478f..6c6ce00c85 100644 --- a/services/web/scripts/fix_group_invite_emails_to_lowercase.mjs +++ b/services/web/scripts/fix_group_invite_emails_to_lowercase.mjs @@ -1,4 +1,4 @@ -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import BatchedUpdateModule from './helpers/batchedUpdate.mjs' const { batchedUpdate } = BatchedUpdateModule @@ -46,8 +46,6 @@ async function processBatch(subscriptions) { } async function main() { - await waitForDb() - const projection = { _id: 1, teamInvites: 1, diff --git a/services/web/scripts/fix_malformed_filetree.mjs b/services/web/scripts/fix_malformed_filetree.mjs index 94c052e0de..4182fa8c76 100644 --- a/services/web/scripts/fix_malformed_filetree.mjs +++ b/services/web/scripts/fix_malformed_filetree.mjs @@ -6,15 +6,13 @@ * script. */ import mongodb from 'mongodb-legacy' -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import ProjectLocator from '../app/src/Features/Project/ProjectLocator.js' const { ObjectId } = mongodb async function main() { const { projectId, mongoPath } = parseArgs() - await waitForDb() - let modifiedCount if (isRootFolder(mongoPath)) { modifiedCount = await fixRootFolder(projectId) diff --git a/services/web/scripts/fix_oversized_docs.mjs b/services/web/scripts/fix_oversized_docs.mjs index 4a727dbe12..b3f160d362 100644 --- a/services/web/scripts/fix_oversized_docs.mjs +++ b/services/web/scripts/fix_oversized_docs.mjs @@ -1,6 +1,6 @@ import fs from 'fs' import minimist from 'minimist' -import { waitForDb, ObjectId } from '../app/src/infrastructure/mongodb.js' +import { ObjectId } from '../app/src/infrastructure/mongodb.js' import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js' import FileStoreHandler from '../app/src/Features/FileStore/FileStoreHandler.js' import FileWriter from '../app/src/infrastructure/FileWriter.js' @@ -39,7 +39,6 @@ Options: } async function main() { - await waitForDb() for (const projectId of opts.projectIds) { await processProject(projectId) } diff --git a/services/web/scripts/force_doc_flush.mjs b/services/web/scripts/force_doc_flush.mjs index 25e2f6b6fd..399816e859 100644 --- a/services/web/scripts/force_doc_flush.mjs +++ b/services/web/scripts/force_doc_flush.mjs @@ -1,5 +1,5 @@ import mongodb from 'mongodb-legacy' -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import DocumentUpdaterHandler from '../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js' const { ObjectId } = mongodb @@ -16,8 +16,6 @@ console.log({ }) async function main() { - await waitForDb() - const { lines, version, ranges } = await getDocument() const size = lines.reduce((size, line) => size + line.length + 1, 0) diff --git a/services/web/scripts/helpers/batchedUpdate.mjs b/services/web/scripts/helpers/batchedUpdate.mjs index 76c6fe41df..fb746a7b55 100644 --- a/services/web/scripts/helpers/batchedUpdate.mjs +++ b/services/web/scripts/helpers/batchedUpdate.mjs @@ -1,7 +1,6 @@ import mongodb from 'mongodb-legacy' import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../../app/src/infrastructure/mongodb.js' @@ -126,7 +125,6 @@ async function batchedUpdate( findOptions, batchedUpdateOptions ) { - await waitForDb() const collection = db[collectionName] ID_EDGE_PAST = await getIdEdgePast(collection) if (!ID_EDGE_PAST) { diff --git a/services/web/scripts/history/clean_sl_history_data.mjs b/services/web/scripts/history/clean_sl_history_data.mjs index 2cab756600..0f8e93661a 100644 --- a/services/web/scripts/history/clean_sl_history_data.mjs +++ b/services/web/scripts/history/clean_sl_history_data.mjs @@ -1,4 +1,4 @@ -import { waitForDb, db } from '../../app/src/infrastructure/mongodb.js' +import { db } from '../../app/src/infrastructure/mongodb.js' import { ensureMongoTimeout } from '../helpers/env_variable_helper.mjs' // Ensure default mongo query timeout has been increased 1h if (!process.env.MONGO_SOCKET_TIMEOUT) { @@ -65,9 +65,9 @@ async function gracefullyDropCollection(collection) { console.log(`removing \`${collectionName}\` data - Done`) } -waitForDb() - .then(main) - .catch(err => { - console.error(err) - process.exit(1) - }) +try { + await main() +} catch (err) { + console.error(err) + process.exit(1) +} diff --git a/services/web/scripts/history/migrate_ranges_support.mjs b/services/web/scripts/history/migrate_ranges_support.mjs index a4a769656f..f62628f9ad 100644 --- a/services/web/scripts/history/migrate_ranges_support.mjs +++ b/services/web/scripts/history/migrate_ranges_support.mjs @@ -1,9 +1,7 @@ import HistoryRangesSupportMigration from '../../app/src/Features/History/HistoryRangesSupportMigration.mjs' -import { waitForDb } from '../../app/src/infrastructure/mongodb.js' import minimist from 'minimist' async function main() { - await waitForDb() const { projectIds, ownerIds, diff --git a/services/web/scripts/invalidate_tokens.mjs b/services/web/scripts/invalidate_tokens.mjs index 158f6ad350..d84c320096 100644 --- a/services/web/scripts/invalidate_tokens.mjs +++ b/services/web/scripts/invalidate_tokens.mjs @@ -1,4 +1,4 @@ -import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db, ObjectId } from '../app/src/infrastructure/mongodb.js' import minimist from 'minimist' const argv = minimist(process.argv.slice(2)) const commit = argv.commit !== undefined @@ -11,8 +11,6 @@ if (!commit) { } console.log('checking', projectIds.length, 'projects') -await waitForDb() - const affectedProjects = await db.projects .find( { _id: { $in: projectIds } }, diff --git a/services/web/scripts/merge_group_subscription_members.mjs b/services/web/scripts/merge_group_subscription_members.mjs index da1cbd0f59..0d4d6acb6c 100644 --- a/services/web/scripts/merge_group_subscription_members.mjs +++ b/services/web/scripts/merge_group_subscription_members.mjs @@ -6,7 +6,7 @@ // node scripts/merge_group_subscription_members \ // --target [targetSubscriptionId] --source [sourceSubscriptionId] --commit -import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db, ObjectId } from '../app/src/infrastructure/mongodb.js' import SubscriptionUpdater from '../app/src/Features/Subscription/SubscriptionUpdater.js' import minimist from 'minimist' @@ -43,8 +43,6 @@ async function main() { console.log('Doing dry run without --commit') } - await waitForDb() - const targetSubscription = await getSubscription(new ObjectId(target)) const sourceSubscription = await getSubscription(new ObjectId(source)) diff --git a/services/web/scripts/migrate_audit_logs.mjs b/services/web/scripts/migrate_audit_logs.mjs index a8ca88ac53..f5cf3c819e 100644 --- a/services/web/scripts/migrate_audit_logs.mjs +++ b/services/web/scripts/migrate_audit_logs.mjs @@ -1,6 +1,6 @@ import BatchedUpdateModule from './helpers/batchedUpdate.mjs' import { promiseMapWithLimit, promisify } from '@overleaf/promise-utils' -import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db, ObjectId } from '../app/src/infrastructure/mongodb.js' import _ from 'lodash' import { fileURLToPath } from 'url' @@ -153,7 +153,6 @@ export default main if (fileURLToPath(import.meta.url) === process.argv[1]) { try { - await waitForDb() await main() console.log('Done.') process.exit(0) diff --git a/services/web/scripts/oauth/backfill_hashed_secrets.mjs b/services/web/scripts/oauth/backfill_hashed_secrets.mjs index 7f09cfc7f6..e3352ea932 100644 --- a/services/web/scripts/oauth/backfill_hashed_secrets.mjs +++ b/services/web/scripts/oauth/backfill_hashed_secrets.mjs @@ -1,12 +1,10 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../../app/src/infrastructure/mongodb.js' import { hashSecret } from '../../modules/oauth2-server/app/src/SecretsHelper.js' async function main() { - await waitForDb() console.log('Hashing client secrets...') await hashSecrets(db.oauthApplications, 'clientSecret') console.log('Hashing access tokens...') diff --git a/services/web/scripts/oauth/create_token.mjs b/services/web/scripts/oauth/create_token.mjs index 36dde27395..cf833e1be2 100644 --- a/services/web/scripts/oauth/create_token.mjs +++ b/services/web/scripts/oauth/create_token.mjs @@ -1,11 +1,9 @@ import minimist from 'minimist' -import { waitForDb, db } from '../../app/src/infrastructure/mongodb.js' +import { db } from '../../app/src/infrastructure/mongodb.js' import { hashSecret } from '../../modules/oauth2-server/app/src/SecretsHelper.js' async function main() { const opts = parseArgs() - await waitForDb() - if (opts.accessToken == null) { console.error('Missing --token option') process.exit(1) diff --git a/services/web/scripts/oauth/register_client.mjs b/services/web/scripts/oauth/register_client.mjs index b14e6da659..6f14ae1230 100644 --- a/services/web/scripts/oauth/register_client.mjs +++ b/services/web/scripts/oauth/register_client.mjs @@ -1,13 +1,12 @@ import minimist from 'minimist' import mongodb from 'mongodb-legacy' -import { waitForDb, db } from '../../app/src/infrastructure/mongodb.js' +import { db } from '../../app/src/infrastructure/mongodb.js' import { hashSecret } from '../../modules/oauth2-server/app/src/SecretsHelper.js' const { ObjectId } = mongodb async function main() { const opts = parseArgs() - await waitForDb() const application = await getApplication(opts.id) if (application == null) { console.log( diff --git a/services/web/scripts/oauth/remove_client.mjs b/services/web/scripts/oauth/remove_client.mjs index 3cd3ae7119..700c19ac70 100644 --- a/services/web/scripts/oauth/remove_client.mjs +++ b/services/web/scripts/oauth/remove_client.mjs @@ -1,13 +1,11 @@ import minimist from 'minimist' import { - waitForDb, db, READ_PREFERENCE_SECONDARY, } from '../../app/src/infrastructure/mongodb.js' async function main() { const opts = parseArgs() - await waitForDb() const application = await getApplication(opts.clientId) if (application == null) { console.error(`Client configuration not found: ${opts.clientId}`) diff --git a/services/web/scripts/recover_docs_from_redis.mjs b/services/web/scripts/recover_docs_from_redis.mjs index d52c0a55ec..ad69b5469f 100644 --- a/services/web/scripts/recover_docs_from_redis.mjs +++ b/services/web/scripts/recover_docs_from_redis.mjs @@ -2,7 +2,7 @@ import minimist from 'minimist' -import { db, waitForDb, ObjectId } from '../app/src/infrastructure/mongodb.js' +import { db, ObjectId } from '../app/src/infrastructure/mongodb.js' import ProjectEntityUpdateHandler from '../app/src/Features/Project/ProjectEntityUpdateHandler.js' import ProjectEntityRestoreHandler from '../app/src/Features/Project/ProjectEntityRestoreHandler.js' import RedisWrapper from '@overleaf/redis-wrapper' @@ -30,7 +30,6 @@ function extractObjectId(s) { } async function main() { - await waitForDb() logger.info({ opts }, 'removing deleted docs') let cursor = 0 do { diff --git a/services/web/scripts/recurly/resync_subscriptions.mjs b/services/web/scripts/recurly/resync_subscriptions.mjs index dce61b114d..d97104d526 100644 --- a/services/web/scripts/recurly/resync_subscriptions.mjs +++ b/services/web/scripts/recurly/resync_subscriptions.mjs @@ -7,7 +7,6 @@ import { setTimeout } from 'node:timers/promises' import util from 'util' import pLimit from 'p-limit' -import { waitForDb } from '../../app/src/infrastructure/mongodb.js' util.inspect.defaultOptions.maxArrayLength = null @@ -147,8 +146,6 @@ const loopForSubscriptions = async skipInitial => { let retryCounter = 0 const run = async () => { - await waitForDb() - while (true) { try { await loopForSubscriptions( diff --git a/services/web/scripts/refresh_features.mjs b/services/web/scripts/refresh_features.mjs index 28e3030419..51d8acecc6 100644 --- a/services/web/scripts/refresh_features.mjs +++ b/services/web/scripts/refresh_features.mjs @@ -1,4 +1,4 @@ -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import minimist from 'minimist' import _ from 'lodash' import async from 'async' @@ -191,6 +191,5 @@ const setup = () => { } } -await waitForDb() setup() run() diff --git a/services/web/scripts/refresh_institution_users.mjs b/services/web/scripts/refresh_institution_users.mjs index 330331b7b5..6edb523108 100644 --- a/services/web/scripts/refresh_institution_users.mjs +++ b/services/web/scripts/refresh_institution_users.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import minimist from 'minimist' import InstitutionsManager from '../app/src/Features/Institutions/InstitutionsManager.js' @@ -32,7 +31,6 @@ function main() { } try { - await waitForDb() await main() } catch (error) { console.error(error) diff --git a/services/web/scripts/remove_deleted_users_from_token_access_refs.mjs b/services/web/scripts/remove_deleted_users_from_token_access_refs.mjs index 9213f71962..b515ee48ad 100644 --- a/services/web/scripts/remove_deleted_users_from_token_access_refs.mjs +++ b/services/web/scripts/remove_deleted_users_from_token_access_refs.mjs @@ -1,6 +1,5 @@ import { db, - waitForDb, READ_PREFERENCE_SECONDARY, } from '../app/src/infrastructure/mongodb.js' import BatchedUpdateModule from './helpers/batchedUpdate.mjs' @@ -192,7 +191,6 @@ async function fixProjectsWithInvalidTokenAccessRefsIds( } async function main(DRY_RUN, PROJECTS_LIST) { - await waitForDb() await fixProjectsWithInvalidTokenAccessRefsIds(DRY_RUN, PROJECTS_LIST) } diff --git a/services/web/scripts/remove_email.mjs b/services/web/scripts/remove_email.mjs index 10eb102531..2e3d78421b 100644 --- a/services/web/scripts/remove_email.mjs +++ b/services/web/scripts/remove_email.mjs @@ -1,4 +1,4 @@ -import { ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { ObjectId } from '../app/src/infrastructure/mongodb.js' import UserUpdater from '../app/src/Features/User/UserUpdater.js' import UserGetter from '../app/src/Features/User/UserGetter.js' @@ -53,7 +53,6 @@ async function removeEmail() { ) } try { - await waitForDb() await removeEmail() console.log('Done.') process.exit() diff --git a/services/web/scripts/remove_oauth_application.mjs b/services/web/scripts/remove_oauth_application.mjs index 35af98766d..601a68a171 100644 --- a/services/web/scripts/remove_oauth_application.mjs +++ b/services/web/scripts/remove_oauth_application.mjs @@ -1,14 +1,11 @@ import { OauthApplication } from '../app/src/models/OauthApplication.js' import parseArgs from 'minimist' import OError from '@overleaf/o-error' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' async function _removeOauthApplication(appId) { if (!appId) { throw new OError('No app id supplied') } - console.log('Waiting for db...') - await waitForDb() console.log(`Removing oauthApplication with id=${appId}`) const result = await OauthApplication.deleteOne({ id: appId }) console.log('Result', result) diff --git a/services/web/scripts/restore_orphaned_docs.mjs b/services/web/scripts/restore_orphaned_docs.mjs index ba6ffc690d..ebbe0d5e4b 100644 --- a/services/web/scripts/restore_orphaned_docs.mjs +++ b/services/web/scripts/restore_orphaned_docs.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ProjectEntityRestoreHandler from '../app/src/Features/Project/ProjectEntityRestoreHandler.js' import ProjectEntityHandler from '../app/src/Features/Project/ProjectEntityHandler.js' import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js' @@ -36,7 +35,6 @@ async function main() { } try { - await waitForDb() await main() process.exit(0) } catch (error) { diff --git a/services/web/scripts/restore_soft_deleted_docs.mjs b/services/web/scripts/restore_soft_deleted_docs.mjs index e61c2cab05..3e4575fa2b 100644 --- a/services/web/scripts/restore_soft_deleted_docs.mjs +++ b/services/web/scripts/restore_soft_deleted_docs.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ProjectEntityRestoreHandler from '../app/src/Features/Project/ProjectEntityRestoreHandler.js' import DocstoreManager from '../app/src/Features/Docstore/DocstoreManager.js' @@ -25,7 +24,6 @@ async function main() { } try { - await waitForDb() await main() process.exit(0) } catch (error) { diff --git a/services/web/scripts/soft_delete_project.mjs b/services/web/scripts/soft_delete_project.mjs index 9f2c978e81..801bcc9a70 100644 --- a/services/web/scripts/soft_delete_project.mjs +++ b/services/web/scripts/soft_delete_project.mjs @@ -1,5 +1,4 @@ import minimist from 'minimist' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ProjectDeleter from '../app/src/Features/Project/ProjectDeleter.js' async function main() { @@ -10,8 +9,6 @@ async function main() { throw new Error('set --project-id') } console.log(`Soft deleting project ${projectId}`) - await waitForDb() - // soft delete, project will be permanently deleted after 90 days await ProjectDeleter.promises.deleteProject(projectId) } diff --git a/services/web/scripts/split_tests_assigned_at_to_dates.mjs b/services/web/scripts/split_tests_assigned_at_to_dates.mjs index 1a8a9c7e19..dbdabee23a 100644 --- a/services/web/scripts/split_tests_assigned_at_to_dates.mjs +++ b/services/web/scripts/split_tests_assigned_at_to_dates.mjs @@ -1,8 +1,7 @@ -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import { fileURLToPath } from 'url' async function updateStringDates() { - await waitForDb() const users = db.users.find({ splitTests: { $exists: true }, }) diff --git a/services/web/scripts/split_writefull_disabled_from_unset.mjs b/services/web/scripts/split_writefull_disabled_from_unset.mjs index 2f2fa4e7e5..880f20f134 100644 --- a/services/web/scripts/split_writefull_disabled_from_unset.mjs +++ b/services/web/scripts/split_writefull_disabled_from_unset.mjs @@ -1,4 +1,4 @@ -import { db, waitForDb } from '../app/src/infrastructure/mongodb.js' +import { db } from '../app/src/infrastructure/mongodb.js' import BatchedUpdateModule from './helpers/batchedUpdate.mjs' import mongodb from 'mongodb-legacy' import fs from 'fs' @@ -26,7 +26,6 @@ async function main() { optedOutList = optedOutFile.split('\n').map(id => new ObjectId(id)) console.log(`preserving opt-outs of ${optedOutList.length} users`) - await waitForDb() // update all applicable user models await batchedUpdate( 'users', diff --git a/services/web/scripts/sso_id_migration_check.mjs b/services/web/scripts/sso_id_migration_check.mjs index 5e18611298..8f8f037b49 100644 --- a/services/web/scripts/sso_id_migration_check.mjs +++ b/services/web/scripts/sso_id_migration_check.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import SAMLUserIdMigrationHandler from '../modules/saas-authentication/app/src/SAML/SAMLUserIdMigrationHandler.mjs' import { ensureRunningOnMongoSecondaryWithTimeout } from './helpers/env_variable_helper.mjs' @@ -10,12 +9,12 @@ const emitUsers = process.argv.includes('--emit-users') console.log('Checking SSO user ID migration for institution:', institutionId) -waitForDb() - .then(main) - .catch(error => { - console.error(error) - process.exit(1) - }) +try { + await main() +} catch (error) { + console.error(error) + process.exit(1) +} async function main() { const result = diff --git a/services/web/scripts/sso_id_remove_not_migrated.mjs b/services/web/scripts/sso_id_remove_not_migrated.mjs index 3a4e6df209..cf836f4796 100644 --- a/services/web/scripts/sso_id_remove_not_migrated.mjs +++ b/services/web/scripts/sso_id_remove_not_migrated.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import SAMLUserIdMigrationHandler from '../modules/saas-authentication/app/src/SAML/SAMLUserIdMigrationHandler.mjs' import { ensureMongoTimeout } from './helpers/env_variable_helper.mjs' @@ -31,7 +30,6 @@ async function main() { } try { - await waitForDb() await main() } catch (error) { console.error(error) diff --git a/services/web/scripts/stress_test.mjs b/services/web/scripts/stress_test.mjs index e000e42db7..3b149c1fee 100644 --- a/services/web/scripts/stress_test.mjs +++ b/services/web/scripts/stress_test.mjs @@ -432,7 +432,7 @@ async function run() { } try { - await Promise.all([mongodb.waitForDb(), mongoose.connectionPromise]) + await Promise.all([mongodb.connectionPromise, mongoose.connectionPromise]) await run() log('Completed') process.exit(0) diff --git a/services/web/scripts/ukamf/check-certs.js b/services/web/scripts/ukamf/check-certs.js index 8a85190be9..dbe07ba1d0 100644 --- a/services/web/scripts/ukamf/check-certs.js +++ b/services/web/scripts/ukamf/check-certs.js @@ -11,7 +11,7 @@ import { Certificate } from '@fidm/x509' import UKAMFDB from './ukamf-db.js' import V1ApiModule from '../../app/src/Features/V1/V1Api.js' -import { db, waitForDb } from '../../app/src/infrastructure/mongodb.js' +import { db } from '../../app/src/infrastructure/mongodb.js' import moment from 'moment' const { promises: V1Api } = V1ApiModule @@ -91,7 +91,6 @@ async function getActiveProviderIds() { } try { - await waitForDb() await main() } catch (error) { console.error(error.stack) diff --git a/services/web/scripts/undelete_project_to_user.mjs b/services/web/scripts/undelete_project_to_user.mjs index 4b2a7acce6..ee837a4190 100644 --- a/services/web/scripts/undelete_project_to_user.mjs +++ b/services/web/scripts/undelete_project_to_user.mjs @@ -1,5 +1,4 @@ import minimist from 'minimist' -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import ProjectDeleter from '../app/src/Features/Project/ProjectDeleter.js' async function main() { @@ -11,7 +10,6 @@ async function main() { throw new Error('set --project-id and --user-id') } console.log(`Restoring project ${projectId} to user ${userId}`) - await waitForDb() await ProjectDeleter.promises.undeleteProject(projectId, { userId }) } diff --git a/services/web/scripts/unlink_third_party_id.mjs b/services/web/scripts/unlink_third_party_id.mjs index d04c44b17a..8511deb403 100644 --- a/services/web/scripts/unlink_third_party_id.mjs +++ b/services/web/scripts/unlink_third_party_id.mjs @@ -1,4 +1,3 @@ -import { waitForDb } from '../app/src/infrastructure/mongodb.js' import minimist from 'minimist' import ThirdPartyIdentityManager from '../app/src/Features/User/ThirdPartyIdentityManager.js' import UserGetter from '../app/src/Features/User/UserGetter.js' @@ -42,8 +41,6 @@ async function main() { throw new Error('No --userId argument provided') } - await waitForDb() - const auditLog = { initiatorId: undefined, ipAddress: '0.0.0.0', diff --git a/services/web/test/acceptance/src/helpers/MongoHelper.mjs b/services/web/test/acceptance/src/helpers/MongoHelper.mjs index c60d393881..0e2c716e0e 100644 --- a/services/web/test/acceptance/src/helpers/MongoHelper.mjs +++ b/services/web/test/acceptance/src/helpers/MongoHelper.mjs @@ -1,6 +1,6 @@ import { execFile } from 'child_process' import { - waitForDb, + connectionPromise, db, dropTestDatabase, } from '../../../../app/src/infrastructure/mongodb.js' @@ -10,7 +10,9 @@ const DEFAULT_ENV = 'saas' export default { initialize() { - before('wait for db', waitForDb) + before('wait for db', async function () { + await connectionPromise + }) if (process.env.CLEANUP_MONGO === 'true') { before('drop test database', dropTestDatabase) }