mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
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:
commit
d345911730
16 changed files with 263 additions and 868 deletions
|
@ -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
|
||||
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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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(
|
||||
{
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages
|
||||
.find({
|
||||
room_id: { $in: room_ids }
|
||||
},
|
||||
callback
|
||||
})
|
||||
.toArray(callback)
|
||||
)
|
||||
},
|
||||
|
||||
|
@ -64,12 +77,14 @@ module.exports = MessageManager = {
|
|||
if (callback == null) {
|
||||
callback = function (error) {}
|
||||
}
|
||||
return db.messages.remove(
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages.deleteMany(
|
||||
{
|
||||
room_id
|
||||
},
|
||||
callback
|
||||
)
|
||||
)
|
||||
},
|
||||
|
||||
updateMessage(room_id, message_id, content, timestamp, callback) {
|
||||
|
@ -80,7 +95,8 @@ module.exports = MessageManager = {
|
|||
_id: message_id,
|
||||
room_id
|
||||
})
|
||||
return db.messages.update(
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages.updateOne(
|
||||
query,
|
||||
{
|
||||
$set: {
|
||||
|
@ -88,12 +104,8 @@ module.exports = MessageManager = {
|
|||
edited_at: timestamp
|
||||
}
|
||||
},
|
||||
function (error) {
|
||||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
return callback()
|
||||
}
|
||||
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) {
|
||||
|
|
|
@ -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,29 +50,25 @@ module.exports = ThreadManager = {
|
|||
}
|
||||
}
|
||||
|
||||
return db.rooms.update(query, { $set: update }, { upsert: true }, function (
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
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) {
|
||||
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(
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms
|
||||
.find(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: { $exists: true }
|
||||
|
@ -80,8 +76,9 @@ module.exports = ThreadManager = {
|
|||
{
|
||||
thread_id: 1,
|
||||
resolved: 1
|
||||
},
|
||||
callback
|
||||
}
|
||||
)
|
||||
.toArray(callback)
|
||||
)
|
||||
},
|
||||
|
||||
|
@ -89,7 +86,8 @@ module.exports = ThreadManager = {
|
|||
if (callback == null) {
|
||||
callback = function (error) {}
|
||||
}
|
||||
return db.rooms.update(
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.updateOne(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: ObjectId(thread_id.toString())
|
||||
|
@ -104,13 +102,15 @@ module.exports = ThreadManager = {
|
|||
},
|
||||
callback
|
||||
)
|
||||
)
|
||||
},
|
||||
|
||||
reopenThread(project_id, thread_id, callback) {
|
||||
if (callback == null) {
|
||||
callback = function (error) {}
|
||||
}
|
||||
return db.rooms.update(
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.updateOne(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: ObjectId(thread_id.toString())
|
||||
|
@ -122,6 +122,7 @@ module.exports = ThreadManager = {
|
|||
},
|
||||
callback
|
||||
)
|
||||
)
|
||||
},
|
||||
|
||||
deleteThread(project_id, thread_id, callback) {
|
||||
|
@ -135,7 +136,8 @@ module.exports = ThreadManager = {
|
|||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
return db.rooms.remove(
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.deleteOne(
|
||||
{
|
||||
_id: room._id
|
||||
},
|
||||
|
@ -146,6 +148,7 @@ module.exports = ThreadManager = {
|
|||
return callback(null, room._id)
|
||||
}
|
||||
)
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
15
services/chat/app/js/mongodb.js
Normal file
15
services/chat/app/js/mongodb.js
Normal 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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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) {
|
||||
|
|
865
services/chat/package-lock.json
generated
865
services/chat/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -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"
|
||||
},
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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')
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue