2020-05-06 10:09:15 +00:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 10:08:21 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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
|
|
|
|
*/
|
2020-05-06 10:09:33 +00:00
|
|
|
let RealTimeRedisManager
|
2021-07-12 16:47:15 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-11-10 11:32:04 +00:00
|
|
|
const rclient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 10:09:33 +00:00
|
|
|
Settings.redis.documentupdater
|
|
|
|
)
|
2020-11-10 11:32:04 +00:00
|
|
|
const pubsubClient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 10:09:33 +00:00
|
|
|
Settings.redis.pubsub
|
|
|
|
)
|
|
|
|
const Keys = Settings.redis.documentupdater.key_schema
|
2021-10-06 09:10:28 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 10:09:33 +00:00
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const metrics = require('./Metrics')
|
2019-03-21 12:10:15 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
const HOST = os.hostname()
|
|
|
|
const RND = crypto.randomBytes(4).toString('hex') // generate a random key for this process
|
|
|
|
let COUNT = 0
|
2016-06-17 11:17:22 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
const MAX_OPS_PER_ITERATION = 8 // process a limited number of ops for safety
|
2017-05-12 12:11:04 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
module.exports = RealTimeRedisManager = {
|
|
|
|
getPendingUpdatesForDoc(doc_id, callback) {
|
|
|
|
const multi = rclient.multi()
|
|
|
|
multi.lrange(Keys.pendingUpdates({ doc_id }), 0, MAX_OPS_PER_ITERATION - 1)
|
|
|
|
multi.ltrim(Keys.pendingUpdates({ doc_id }), MAX_OPS_PER_ITERATION, -1)
|
|
|
|
return multi.exec(function (error, replys) {
|
|
|
|
let jsonUpdate
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const jsonUpdates = replys[0]
|
|
|
|
for (jsonUpdate of Array.from(jsonUpdates)) {
|
|
|
|
// record metric for each update removed from queue
|
|
|
|
metrics.summary('redis.pendingUpdates', jsonUpdate.length, {
|
2021-07-13 11:04:42 +00:00
|
|
|
status: 'pop',
|
2020-05-06 10:09:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
const updates = []
|
|
|
|
for (jsonUpdate of Array.from(jsonUpdates)) {
|
2021-10-26 08:08:56 +00:00
|
|
|
let update
|
2020-05-06 10:09:33 +00:00
|
|
|
try {
|
|
|
|
update = JSON.parse(jsonUpdate)
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e)
|
|
|
|
}
|
|
|
|
updates.push(update)
|
|
|
|
}
|
|
|
|
return callback(error, updates)
|
|
|
|
})
|
|
|
|
},
|
2016-06-17 11:17:22 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
getUpdatesLength(doc_id, callback) {
|
|
|
|
return rclient.llen(Keys.pendingUpdates({ doc_id }), callback)
|
|
|
|
},
|
2016-06-17 11:17:22 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
sendData(data) {
|
|
|
|
// create a unique message id using a counter
|
|
|
|
const message_id = `doc:${HOST}:${RND}-${COUNT++}`
|
|
|
|
if (data != null) {
|
|
|
|
data._id = message_id
|
|
|
|
}
|
2020-03-30 09:31:43 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
const blob = JSON.stringify(data)
|
|
|
|
metrics.summary('redis.publish.applied-ops', blob.length)
|
2020-03-30 09:31:43 +00:00
|
|
|
|
2020-05-06 10:09:33 +00:00
|
|
|
// publish on separate channels for individual projects and docs when
|
|
|
|
// configured (needs realtime to be configured for this too).
|
|
|
|
if (Settings.publishOnIndividualChannels) {
|
|
|
|
return pubsubClient.publish(`applied-ops:${data.doc_id}`, blob)
|
|
|
|
} else {
|
|
|
|
return pubsubClient.publish('applied-ops', blob)
|
|
|
|
}
|
2021-07-13 11:04:42 +00:00
|
|
|
},
|
2020-05-06 10:09:33 +00:00
|
|
|
}
|