Przeglądaj źródła

Merge pull request #144 from overleaf/jpa-mongodb-native

[misc] migrate to the native mongo driver
Jakob Ackermann 6 lat temu
rodzic
commit
16da44e56a

+ 24 - 10
services/document-updater/app.js

@@ -17,14 +17,14 @@ const DispatchManager = require('./app/js/DispatchManager')
 const DeleteQueueManager = require('./app/js/DeleteQueueManager')
 const DeleteQueueManager = require('./app/js/DeleteQueueManager')
 const Errors = require('./app/js/Errors')
 const Errors = require('./app/js/Errors')
 const HttpController = require('./app/js/HttpController')
 const HttpController = require('./app/js/HttpController')
-const mongojs = require('./app/js/mongojs')
+const mongodb = require('./app/js/mongodb')
 const async = require('async')
 const async = require('async')
 
 
 const Path = require('path')
 const Path = require('path')
 const bodyParser = require('body-parser')
 const bodyParser = require('body-parser')
 
 
 Metrics.mongodb.monitor(
 Metrics.mongodb.monitor(
-  Path.resolve(__dirname, '/node_modules/mongojs/node_modules/mongodb'),
+  Path.resolve(__dirname, '/node_modules/mongodb'),
   logger
   logger
 )
 )
 Metrics.event_loop.monitor(logger, 100)
 Metrics.event_loop.monitor(logger, 100)
@@ -158,7 +158,7 @@ app.get('/health_check', (req, res, next) => {
         })
         })
       },
       },
       (cb) => {
       (cb) => {
-        mongojs.healthCheck((error) => {
+        mongodb.healthCheck((error) => {
           if (error) {
           if (error) {
             logger.err({ err: error }, 'failed mongo health check')
             logger.err({ err: error }, 'failed mongo health check')
           }
           }
@@ -219,13 +219,27 @@ const host = Settings.internal.documentupdater.host || 'localhost'
 
 
 if (!module.parent) {
 if (!module.parent) {
   // Called directly
   // Called directly
-  app.listen(port, host, () => {
-    logger.info(`Document-updater starting up, listening on ${host}:${port}`)
-    if (Settings.continuousBackgroundFlush) {
-      logger.info('Starting continuous background flush')
-      DeleteQueueManager.startBackgroundFlush()
-    }
-  })
+  mongodb
+    .waitForDb()
+    .then(() => {
+      app.listen(port, host, function (err) {
+        if (err) {
+          logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
+          process.exit(1)
+        }
+        logger.info(
+          `Document-updater starting up, listening on ${host}:${port}`
+        )
+        if (Settings.continuousBackgroundFlush) {
+          logger.info('Starting continuous background flush')
+          DeleteQueueManager.startBackgroundFlush()
+        }
+      })
+    })
+    .catch((err) => {
+      logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
+      process.exit(1)
+    })
 }
 }
 
 
 module.exports = app
 module.exports = app

+ 2 - 2
services/document-updater/app/js/SnapshotManager.js

@@ -12,7 +12,7 @@
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
  */
 let SnapshotManager
 let SnapshotManager
-const { db, ObjectId } = require('./mongojs')
+const { db, ObjectId } = require('./mongodb')
 
 
 module.exports = SnapshotManager = {
 module.exports = SnapshotManager = {
   recordSnapshot(
   recordSnapshot(
@@ -30,7 +30,7 @@ module.exports = SnapshotManager = {
     } catch (error) {
     } catch (error) {
       return callback(error)
       return callback(error)
     }
     }
-    return db.docSnapshots.insert(
+    db.docSnapshots.insertOne(
       {
       {
         project_id,
         project_id,
         doc_id,
         doc_id,

+ 37 - 0
services/document-updater/app/js/mongodb.js

@@ -0,0 +1,37 @@
+const Settings = require('settings-sharelatex')
+const { MongoClient, ObjectId } = require('mongodb')
+
+const clientPromise = MongoClient.connect(
+  Settings.mongo.url,
+  Settings.mongo.options
+)
+
+async function healthCheck() {
+  const internalDb = (await clientPromise).db()
+  const res = await internalDb.command({ ping: 1 })
+  if (!res.ok) {
+    throw new Error('failed mongo ping')
+  }
+}
+
+let setupDbPromise
+async function waitForDb() {
+  if (!setupDbPromise) {
+    setupDbPromise = setupDb()
+  }
+  await setupDbPromise
+}
+
+const db = {}
+async function setupDb() {
+  const internalDb = (await clientPromise).db()
+
+  db.docSnapshots = internalDb.collection('docSnapshots')
+}
+
+module.exports = {
+  db,
+  ObjectId,
+  healthCheck: require('util').callbackify(healthCheck),
+  waitForDb
+}

+ 0 - 27
services/document-updater/app/js/mongojs.js

@@ -1,27 +0,0 @@
-// TODO: This file was created by bulk-decaffeinate.
-// Sanity-check the conversion and remove this comment.
-/*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
-const Settings = require('settings-sharelatex')
-const mongojs = require('mongojs')
-const db = mongojs(Settings.mongo.url, ['docSnapshots'])
-
-module.exports = {
-  db,
-  ObjectId: mongojs.ObjectId,
-  healthCheck(callback) {
-    return db.runCommand({ ping: 1 }, function (err, res) {
-      if (err != null) {
-        return callback(err)
-      }
-      if (!res.ok) {
-        return callback(new Error('failed mongo ping'))
-      }
-      return callback()
-    })
-  }
-}

+ 4 - 0
services/document-updater/config/settings.defaults.js

@@ -174,6 +174,10 @@ module.exports = {
   dispatcherCount: process.env.DISPATCHER_COUNT,
   dispatcherCount: process.env.DISPATCHER_COUNT,
 
 
   mongo: {
   mongo: {
+    options: {
+      useUnifiedTopology:
+        (process.env.MONGO_USE_UNIFIED_TOPOLOGY || 'true') === 'true'
+    },
     url:
     url:
       process.env.MONGO_CONNECTION_STRING ||
       process.env.MONGO_CONNECTION_STRING ||
       `mongodb://${process.env.MONGO_HOST || '127.0.0.1'}/sharelatex`
       `mongodb://${process.env.MONGO_HOST || '127.0.0.1'}/sharelatex`

Plik diff jest za duży
+ 78 - 451
services/document-updater/package-lock.json


+ 1 - 1
services/document-updater/package.json

@@ -25,7 +25,7 @@
     "lodash": "^4.17.13",
     "lodash": "^4.17.13",
     "logger-sharelatex": "^2.2.0",
     "logger-sharelatex": "^2.2.0",
     "metrics-sharelatex": "^2.6.2",
     "metrics-sharelatex": "^2.6.2",
-    "mongojs": "^3.1.0",
+    "mongodb": "^3.6.0",
     "redis-sharelatex": "^1.0.13",
     "redis-sharelatex": "^1.0.13",
     "request": "^2.88.2",
     "request": "^2.88.2",
     "requestretry": "^4.1.0",
     "requestretry": "^4.1.0",

+ 6 - 7
services/document-updater/test/acceptance/js/RangesTests.js

@@ -17,7 +17,7 @@ chai.should()
 const { expect } = chai
 const { expect } = chai
 const async = require('async')
 const async = require('async')
 
 
-const { db, ObjectId } = require('../../../app/js/mongojs')
+const { db, ObjectId } = require('../../../app/js/mongodb')
 const MockWebApi = require('./helpers/MockWebApi')
 const MockWebApi = require('./helpers/MockWebApi')
 const DocUpdaterClient = require('./helpers/DocUpdaterClient')
 const DocUpdaterClient = require('./helpers/DocUpdaterClient')
 const DocUpdaterApp = require('./helpers/DocUpdaterApp')
 const DocUpdaterApp = require('./helpers/DocUpdaterApp')
@@ -668,12 +668,12 @@ describe('Ranges', function () {
           if (error != null) {
           if (error != null) {
             return done(error)
             return done(error)
           }
           }
-          return db.docSnapshots.find(
-            {
+          db.docSnapshots
+            .find({
               project_id: ObjectId(this.project_id),
               project_id: ObjectId(this.project_id),
               doc_id: ObjectId(this.doc_id)
               doc_id: ObjectId(this.doc_id)
-            },
-            (error, docSnapshots) => {
+            })
+            .toArray((error, docSnapshots) => {
               if (error != null) {
               if (error != null) {
                 return done(error)
                 return done(error)
               }
               }
@@ -686,8 +686,7 @@ describe('Ranges', function () {
                 tid: this.tid
                 tid: this.tid
               })
               })
               return done()
               return done()
-            }
-          )
+            })
         }
         }
       )
       )
     })
     })

+ 6 - 4
services/document-updater/test/acceptance/js/helpers/DocUpdaterApp.js

@@ -12,6 +12,7 @@
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  */
  */
 const app = require('../../../../app')
 const app = require('../../../../app')
+const { waitForDb } = require('../../../../app/js/mongodb')
 require('logger-sharelatex').logger.level('fatal')
 require('logger-sharelatex').logger.level('fatal')
 
 
 module.exports = {
 module.exports = {
@@ -26,9 +27,10 @@ module.exports = {
       return callback()
       return callback()
     } else if (this.initing) {
     } else if (this.initing) {
       return this.callbacks.push(callback)
       return this.callbacks.push(callback)
-    } else {
-      this.initing = true
-      this.callbacks.push(callback)
+    }
+    this.initing = true
+    this.callbacks.push(callback)
+    waitForDb().then(() => {
       return app.listen(3003, 'localhost', (error) => {
       return app.listen(3003, 'localhost', (error) => {
         if (error != null) {
         if (error != null) {
           throw error
           throw error
@@ -42,6 +44,6 @@ module.exports = {
           return result
           return result
         })()
         })()
       })
       })
-    }
+    })
   }
   }
 }
 }

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików