2020-05-06 06:09:15 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:08:21 -04: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
|
|
|
|
*/
|
2021-07-12 12:47:15 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2024-01-30 10:35:54 -05:00
|
|
|
const { promisifyAll } = require('@overleaf/promise-utils')
|
2020-11-10 06:32:04 -05:00
|
|
|
const rclient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 06:09:33 -04:00
|
|
|
Settings.redis.documentupdater
|
|
|
|
)
|
2020-11-10 06:32:04 -05:00
|
|
|
const pubsubClient = require('@overleaf/redis-wrapper').createClient(
|
2020-05-06 06:09:33 -04:00
|
|
|
Settings.redis.pubsub
|
|
|
|
)
|
|
|
|
const Keys = Settings.redis.documentupdater.key_schema
|
2021-10-06 05:10:28 -04:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-05-06 06:09:33 -04:00
|
|
|
const os = require('os')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const metrics = require('./Metrics')
|
2019-03-21 08:10:15 -04:00
|
|
|
|
2020-05-06 06:09:33 -04: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 07:17:22 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const MAX_OPS_PER_ITERATION = 8 // process a limited number of ops for safety
|
2017-05-12 08:11:04 -04:00
|
|
|
|
2024-01-30 10:35:54 -05:00
|
|
|
const RealTimeRedisManager = {
|
2023-03-21 08:06:13 -04:00
|
|
|
getPendingUpdatesForDoc(docId, callback) {
|
2024-05-29 11:50:34 -04:00
|
|
|
// Make sure that this MULTI operation only operates on doc
|
|
|
|
// specific keys, i.e. keys that have the doc id in curly braces.
|
|
|
|
// The curly braces identify a hash key for Redis and ensures that
|
|
|
|
// the MULTI's operations are all done on the same node in a
|
|
|
|
// cluster environment.
|
2020-05-06 06:09:33 -04:00
|
|
|
const multi = rclient.multi()
|
2023-03-21 08:06:13 -04:00
|
|
|
multi.lrange(
|
|
|
|
Keys.pendingUpdates({ doc_id: docId }),
|
|
|
|
0,
|
|
|
|
MAX_OPS_PER_ITERATION - 1
|
|
|
|
)
|
|
|
|
multi.ltrim(
|
|
|
|
Keys.pendingUpdates({ doc_id: docId }),
|
|
|
|
MAX_OPS_PER_ITERATION,
|
|
|
|
-1
|
|
|
|
)
|
2020-05-06 06:09:33 -04:00
|
|
|
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 07:04:42 -04:00
|
|
|
status: 'pop',
|
2020-05-06 06:09:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
const updates = []
|
|
|
|
for (jsonUpdate of Array.from(jsonUpdates)) {
|
2021-10-26 04:08:56 -04:00
|
|
|
let update
|
2020-05-06 06:09:33 -04:00
|
|
|
try {
|
|
|
|
update = JSON.parse(jsonUpdate)
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e)
|
|
|
|
}
|
|
|
|
updates.push(update)
|
|
|
|
}
|
|
|
|
return callback(error, updates)
|
|
|
|
})
|
|
|
|
},
|
2016-06-17 07:17:22 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
getUpdatesLength(docId, callback) {
|
|
|
|
return rclient.llen(Keys.pendingUpdates({ doc_id: docId }), callback)
|
2020-05-06 06:09:33 -04:00
|
|
|
},
|
2016-06-17 07:17:22 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
sendData(data) {
|
|
|
|
// create a unique message id using a counter
|
2023-03-21 08:06:13 -04:00
|
|
|
const messageId = `doc:${HOST}:${RND}-${COUNT++}`
|
2020-05-06 06:09:33 -04:00
|
|
|
if (data != null) {
|
2023-03-21 08:06:13 -04:00
|
|
|
data._id = messageId
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2020-03-30 05:31:43 -04:00
|
|
|
|
2020-05-06 06:09:33 -04:00
|
|
|
const blob = JSON.stringify(data)
|
|
|
|
metrics.summary('redis.publish.applied-ops', blob.length)
|
2020-03-30 05:31:43 -04:00
|
|
|
|
2020-05-06 06:09:33 -04: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 07:04:42 -04:00
|
|
|
},
|
2020-05-06 06:09:33 -04:00
|
|
|
}
|
2024-01-30 10:35:54 -05:00
|
|
|
|
|
|
|
module.exports = RealTimeRedisManager
|
|
|
|
module.exports.promises = promisifyAll(RealTimeRedisManager, {
|
|
|
|
without: ['sendData'],
|
|
|
|
})
|