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 settings = require('settings-sharelatex')
const mongodb = require('./app/js/mongodb')
const Server = require('./app/js/server')
if (!module.parent) {
@ -22,12 +23,20 @@ if (!module.parent) {
settings.internal != null ? settings.internal.chat : undefined,
(x1) => x1.host
) || 'localhost'
Server.server.listen(port, host, function (error) {
if (error != null) {
throw error
}
return logger.info(`Chat starting up, listening on ${host}:${port}`)
})
mongodb.clientConnecting
.then(() => {
Server.server.listen(port, host, function (err) {
if (err) {
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

View file

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

View file

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

View file

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

File diff suppressed because it is too large Load diff

View file

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

View file

@ -8,7 +8,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* 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 ChatClient = require('./helpers/ChatClient')

View file

@ -9,7 +9,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* 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 crypto = require('crypto')

View file

@ -9,7 +9,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* 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 ChatClient = require('./helpers/ChatClient')

View file

@ -10,7 +10,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* 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 async = require('async')
const crypto = require('crypto')

View file

@ -10,7 +10,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* 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 crypto = require('crypto')

View file

@ -11,7 +11,7 @@
* DS102: Remove unnecessary code created because of implicit returns
* 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 ChatClient = require('./helpers/ChatClient')

View file

@ -12,7 +12,6 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const app = require('../../../../app')
require('logger-sharelatex').logger.level('error')
module.exports = {
running: false,