2021-05-25 09:27:45 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const rclient = require('@overleaf/redis-wrapper').createClient(
|
|
|
|
Settings.redis.documentupdater
|
|
|
|
)
|
|
|
|
let keys = Settings.redis.documentupdater.key_schema
|
|
|
|
const async = require('async')
|
|
|
|
const RedisManager = require('./app/js/RedisManager')
|
2017-06-14 07:09:34 -04:00
|
|
|
|
2021-05-25 09:27:45 -04:00
|
|
|
const getKeysFromNode = function (node, pattern, callback) {
|
|
|
|
let cursor = 0 // redis iterator
|
|
|
|
const keySet = {} // use hash to avoid duplicate results
|
|
|
|
// scan over all keys looking for pattern
|
2021-05-25 09:32:03 -04:00
|
|
|
const doIteration = () =>
|
|
|
|
node.scan(cursor, 'MATCH', pattern, 'COUNT', 1000, function(error, reply) {
|
|
|
|
if (error) {
|
2021-05-25 09:27:45 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
2021-05-25 09:32:03 -04:00
|
|
|
;[cursor, keys] = reply
|
2021-05-25 09:27:45 -04:00
|
|
|
console.log('SCAN', keys.length)
|
2021-05-25 09:32:03 -04:00
|
|
|
for (const key of keys) {
|
2021-05-25 09:27:45 -04:00
|
|
|
keySet[key] = true
|
|
|
|
}
|
2021-05-25 09:32:03 -04:00
|
|
|
if (cursor==='0') {
|
2021-05-25 09:27:45 -04:00
|
|
|
// note redis returns string result not numeric
|
|
|
|
return callback(null, Object.keys(keySet))
|
|
|
|
} else {
|
|
|
|
return doIteration()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return doIteration()
|
|
|
|
}
|
2017-06-14 07:09:34 -04:00
|
|
|
|
2021-05-25 09:27:45 -04:00
|
|
|
const getKeys = function (pattern, callback) {
|
|
|
|
const nodes = (typeof rclient.nodes === 'function'
|
|
|
|
? rclient.nodes('master')
|
|
|
|
: undefined) || [rclient]
|
|
|
|
console.log('GOT NODES', nodes.length)
|
|
|
|
const doKeyLookupForNode = (node, cb) => getKeysFromNode(node, pattern, cb)
|
|
|
|
return async.concatSeries(nodes, doKeyLookupForNode, callback)
|
|
|
|
}
|
2017-06-14 07:09:34 -04:00
|
|
|
|
2021-05-25 09:27:45 -04:00
|
|
|
const expireDocOps = (callback) =>
|
2021-05-25 09:32:03 -04:00
|
|
|
// eslint-disable-next-line handle-callback-err
|
2021-05-25 09:27:45 -04:00
|
|
|
getKeys(keys.docOps({ doc_id: '*' }), (error, keys) =>
|
|
|
|
async.mapSeries(
|
|
|
|
keys,
|
|
|
|
function (key, cb) {
|
|
|
|
console.log(`EXPIRE ${key} ${RedisManager.DOC_OPS_TTL}`)
|
|
|
|
return rclient.expire(key, RedisManager.DOC_OPS_TTL, cb)
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
)
|
2017-06-14 07:09:34 -04:00
|
|
|
|
2021-05-25 09:27:45 -04:00
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
// Give redis a chance to connect
|
|
|
|
expireDocOps(function (error) {
|
2021-05-25 09:32:03 -04:00
|
|
|
if (error) {
|
2021-05-25 09:27:45 -04:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
return process.exit()
|
|
|
|
}),
|
|
|
|
1000
|
|
|
|
)
|