Merge pull request #54 from overleaf/jpa-wait-for-mongo

[misc] complete the connection setup before starting the http server
This commit is contained in:
Jakob Ackermann 2020-08-20 17:06:12 +02:00 committed by GitHub
commit d345911730
16 changed files with 263 additions and 868 deletions

View file

@ -8,6 +8,7 @@
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const settings = require('settings-sharelatex') const settings = require('settings-sharelatex')
const mongodb = require('./app/js/mongodb')
const Server = require('./app/js/server') const Server = require('./app/js/server')
if (!module.parent) { if (!module.parent) {
@ -22,12 +23,20 @@ if (!module.parent) {
settings.internal != null ? settings.internal.chat : undefined, settings.internal != null ? settings.internal.chat : undefined,
(x1) => x1.host (x1) => x1.host
) || 'localhost' ) || 'localhost'
Server.server.listen(port, host, function (error) { mongodb.clientConnecting
if (error != null) { .then(() => {
throw error Server.server.listen(port, host, function (err) {
} if (err) {
return logger.info(`Chat starting up, listening on ${host}:${port}`) logger.fatal({ err }, `Cannot bind to ${host}:${port}. Exiting.`)
}) process.exit(1)
}
return logger.info(`Chat starting up, listening on ${host}:${port}`)
})
})
.catch((err) => {
logger.fatal({ err }, 'Cannot connect to mongo. Exiting.')
process.exit(1)
})
} }
module.exports = Server.server module.exports = Server.server

View file

@ -17,7 +17,7 @@ const metrics = require('metrics-sharelatex')
const MessageManager = require('./MessageManager') const MessageManager = require('./MessageManager')
const MessageFormatter = require('./MessageFormatter') const MessageFormatter = require('./MessageFormatter')
const ThreadManager = require('../Threads/ThreadManager') const ThreadManager = require('../Threads/ThreadManager')
const { ObjectId } = require('../../mongojs') const { ObjectId } = require('../../mongodb')
module.exports = MessageHttpController = { module.exports = MessageHttpController = {
DEFAULT_MESSAGE_LIMIT: 50, DEFAULT_MESSAGE_LIMIT: 50,

View file

@ -13,13 +13,12 @@
* 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 MessageManager let MessageManager
const mongojs = require('../../mongojs') const { ObjectId, getCollection } = require('../../mongodb')
const { db } = mongojs
const { ObjectId } = mongojs
const async = require('async')
const metrics = require('metrics-sharelatex') const metrics = require('metrics-sharelatex')
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const messagesCollectionPromise = getCollection('messages')
module.exports = MessageManager = { module.exports = MessageManager = {
createMessage(room_id, user_id, content, timestamp, callback) { createMessage(room_id, user_id, content, timestamp, callback) {
if (callback == null) { if (callback == null) {
@ -32,7 +31,15 @@ module.exports = MessageManager = {
timestamp timestamp
} }
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts) newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
return db.messages.save(newMessageOpts, callback) messagesCollectionPromise.then((messages) =>
messages.insertOne(newMessageOpts, function (error, confirmation) {
if (error) {
return callback(error)
}
newMessageOpts._id = confirmation.insertedId
callback(null, newMessageOpts)
})
)
}, },
getMessages(room_id, limit, before, callback) { getMessages(room_id, limit, before, callback) {
@ -44,19 +51,25 @@ module.exports = MessageManager = {
query.timestamp = { $lt: before } query.timestamp = { $lt: before }
} }
query = this._ensureIdsAreObjectIds(query) query = this._ensureIdsAreObjectIds(query)
const cursor = db.messages.find(query).sort({ timestamp: -1 }).limit(limit) messagesCollectionPromise.then((messages) =>
return cursor.toArray(callback) messages
.find(query)
.sort({ timestamp: -1 })
.limit(limit)
.toArray(callback)
)
}, },
findAllMessagesInRooms(room_ids, callback) { findAllMessagesInRooms(room_ids, callback) {
if (callback == null) { if (callback == null) {
callback = function (error, messages) {} callback = function (error, messages) {}
} }
return db.messages.find( messagesCollectionPromise.then((messages) =>
{ messages
room_id: { $in: room_ids } .find({
}, room_id: { $in: room_ids }
callback })
.toArray(callback)
) )
}, },
@ -64,11 +77,13 @@ module.exports = MessageManager = {
if (callback == null) { if (callback == null) {
callback = function (error) {} callback = function (error) {}
} }
return db.messages.remove( messagesCollectionPromise.then((messages) =>
{ messages.deleteMany(
room_id {
}, room_id
callback },
callback
)
) )
}, },
@ -80,20 +95,17 @@ module.exports = MessageManager = {
_id: message_id, _id: message_id,
room_id room_id
}) })
return db.messages.update( messagesCollectionPromise.then((messages) =>
query, messages.updateOne(
{ query,
$set: { {
content, $set: {
edited_at: timestamp content,
} edited_at: timestamp
}, }
function (error) { },
if (error != null) { callback
return callback(error) )
}
return callback()
}
) )
}, },
@ -105,12 +117,9 @@ module.exports = MessageManager = {
_id: message_id, _id: message_id,
room_id room_id
}) })
return db.messages.remove(query, function (error) { messagesCollectionPromise.then((messages) =>
if (error != null) { messages.deleteOne(query, callback)
return callback(error) )
}
return callback()
})
}, },
_ensureIdsAreObjectIds(query) { _ensureIdsAreObjectIds(query) {

View file

@ -12,12 +12,12 @@
* 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 ThreadManager let ThreadManager
const mongojs = require('../../mongojs') const { ObjectId, getCollection } = require('../../mongodb')
const { db } = mongojs
const { ObjectId } = mongojs
const logger = require('logger-sharelatex') const logger = require('logger-sharelatex')
const metrics = require('metrics-sharelatex') const metrics = require('metrics-sharelatex')
const roomsCollectionPromise = getCollection('rooms')
module.exports = ThreadManager = { module.exports = ThreadManager = {
GLOBAL_THREAD: 'GLOBAL', GLOBAL_THREAD: 'GLOBAL',
@ -50,38 +50,35 @@ module.exports = ThreadManager = {
} }
} }
return db.rooms.update(query, { $set: update }, { upsert: true }, function ( roomsCollectionPromise.then((rooms) =>
error rooms.updateOne(query, { $set: update }, { upsert: true }, function (
) { error
if (error != null) { ) {
return callback(error)
}
return db.rooms.find(query, function (error, rooms) {
if (rooms == null) {
rooms = []
}
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
return callback(null, rooms[0]) rooms.findOne(query, callback)
}) })
}) )
}, },
findAllThreadRooms(project_id, callback) { findAllThreadRooms(project_id, callback) {
if (callback == null) { if (callback == null) {
callback = function (error, rooms) {} callback = function (error, rooms) {}
} }
return db.rooms.find( roomsCollectionPromise.then((rooms) =>
{ rooms
project_id: ObjectId(project_id.toString()), .find(
thread_id: { $exists: true } {
}, project_id: ObjectId(project_id.toString()),
{ thread_id: { $exists: true }
thread_id: 1, },
resolved: 1 {
}, thread_id: 1,
callback resolved: 1
}
)
.toArray(callback)
) )
}, },
@ -89,20 +86,22 @@ module.exports = ThreadManager = {
if (callback == null) { if (callback == null) {
callback = function (error) {} callback = function (error) {}
} }
return db.rooms.update( roomsCollectionPromise.then((rooms) =>
{ rooms.updateOne(
project_id: ObjectId(project_id.toString()), {
thread_id: ObjectId(thread_id.toString()) project_id: ObjectId(project_id.toString()),
}, thread_id: ObjectId(thread_id.toString())
{ },
$set: { {
resolved: { $set: {
user_id, resolved: {
ts: new Date() user_id,
ts: new Date()
}
} }
} },
}, callback
callback )
) )
}, },
@ -110,17 +109,19 @@ module.exports = ThreadManager = {
if (callback == null) { if (callback == null) {
callback = function (error) {} callback = function (error) {}
} }
return db.rooms.update( roomsCollectionPromise.then((rooms) =>
{ rooms.updateOne(
project_id: ObjectId(project_id.toString()), {
thread_id: ObjectId(thread_id.toString()) project_id: ObjectId(project_id.toString()),
}, thread_id: ObjectId(thread_id.toString())
{ },
$unset: { {
resolved: true $unset: {
} resolved: true
}, }
callback },
callback
)
) )
}, },
@ -135,16 +136,18 @@ module.exports = ThreadManager = {
if (error != null) { if (error != null) {
return callback(error) return callback(error)
} }
return db.rooms.remove( roomsCollectionPromise.then((rooms) =>
{ rooms.deleteOne(
_id: room._id {
}, _id: room._id
function (error) { },
if (error != null) { function (error) {
return callback(error) if (error != null) {
return callback(error)
}
return callback(null, room._id)
} }
return callback(null, room._id) )
}
) )
}) })
} }

View file

@ -0,0 +1,15 @@
const Settings = require('settings-sharelatex')
const { MongoClient, ObjectId } = require('mongodb')
const clientConnecting = MongoClient.connect(Settings.mongo.url)
const dbPromise = clientConnecting.then((client) => client.db())
async function getCollection(name) {
return (await dbPromise).collection(name)
}
module.exports = {
clientConnecting,
ObjectId,
getCollection
}

View file

@ -1,9 +0,0 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
const Settings = require('settings-sharelatex')
const mongojs = require('mongojs')
const db = mongojs(Settings.mongo.url, ['rooms', 'messages'])
module.exports = {
db,
ObjectId: mongojs.ObjectId
}

View file

@ -12,7 +12,7 @@
*/ */
let Router let Router
const MessageHttpController = require('./Features/Messages/MessageHttpController') const MessageHttpController = require('./Features/Messages/MessageHttpController')
const { ObjectId } = require('./mongojs') const { ObjectId } = require('./mongodb')
module.exports = Router = { module.exports = Router = {
route(app) { route(app) {

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@
"express": "4.17.1", "express": "4.17.1",
"logger-sharelatex": "^2.2.0", "logger-sharelatex": "^2.2.0",
"metrics-sharelatex": "^2.5.0", "metrics-sharelatex": "^2.5.0",
"mongojs": "3.1.0", "mongodb": "^3.6.0",
"request": "^2.88.2", "request": "^2.88.2",
"settings-sharelatex": "^1.1.0" "settings-sharelatex": "^1.1.0"
}, },

View file

@ -8,7 +8,7 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* 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 { ObjectId } = require('../../../app/js/mongojs') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')

View file

@ -9,7 +9,7 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* 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 { ObjectId } = require('../../../app/js/mongojs') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const crypto = require('crypto') const crypto = require('crypto')

View file

@ -9,7 +9,7 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* 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 { ObjectId } = require('../../../app/js/mongojs') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')

View file

@ -10,7 +10,7 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* 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 { ObjectId } = require('../../../app/js/mongojs') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const async = require('async') const async = require('async')
const crypto = require('crypto') const crypto = require('crypto')

View file

@ -10,7 +10,7 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* 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 { ObjectId } = require('../../../app/js/mongojs') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const crypto = require('crypto') const crypto = require('crypto')

View file

@ -11,7 +11,7 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* 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 { ObjectId } = require('../../../app/js/mongojs') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')

View file

@ -12,7 +12,6 @@
* 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')
require('logger-sharelatex').logger.level('error')
module.exports = { module.exports = {
running: false, running: false,