overleaf/services/document-updater/scripts/expire_docops.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

const Settings = require('@overleaf/settings')
2021-05-25 09:27:45 -04: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 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 = () =>
2021-05-25 10:45:42 -04:00
node.scan(cursor, 'MATCH', pattern, 'COUNT', 1000, function (error, reply) {
2021-05-25 09:32:03 -04:00
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 10:45:42 -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()
}
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)
}
2021-07-13 07:04:42 -04:00
const expireDocOps = callback =>
getKeys(keys.docOps({ doc_id: '*' }), (error, keys) => {
if (error) return callback(error)
2021-05-25 09:27:45 -04:00
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 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
)