overleaf/services/document-updater/expire_docops.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

const Settings = require('@overleaf/settings')
2021-05-25 13:27:45 +00:00
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')
2021-05-25 13:27:45 +00: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 13:32:03 +00:00
const doIteration = () =>
2021-05-25 14:45:42 +00:00
node.scan(cursor, 'MATCH', pattern, 'COUNT', 1000, function (error, reply) {
2021-05-25 13:32:03 +00:00
if (error) {
2021-05-25 13:27:45 +00:00
return callback(error)
}
2021-05-25 13:32:03 +00:00
;[cursor, keys] = reply
2021-05-25 13:27:45 +00:00
console.log('SCAN', keys.length)
2021-05-25 13:32:03 +00:00
for (const key of keys) {
2021-05-25 13:27:45 +00:00
keySet[key] = true
}
2021-05-25 14:45:42 +00:00
if (cursor === '0') {
2021-05-25 13:27:45 +00:00
// note redis returns string result not numeric
return callback(null, Object.keys(keySet))
} else {
return doIteration()
}
})
return doIteration()
}
2021-05-25 13:27:45 +00: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)
}
2021-07-13 11:04:42 +00:00
const expireDocOps = callback =>
2021-05-25 13:32:03 +00:00
// eslint-disable-next-line handle-callback-err
2021-05-25 13:27:45 +00: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
)
)
2021-05-25 13:27:45 +00:00
setTimeout(
() =>
// Give redis a chance to connect
expireDocOps(function (error) {
2021-05-25 13:32:03 +00:00
if (error) {
2021-05-25 13:27:45 +00:00
throw error
}
return process.exit()
}),
1000
)