overleaf/services/web/scripts/purge_non_logged_in_sessions.js
June Kelly a450a74351 Upgrade async package to 3.2.2 (#8447)
* Revert "Revert "Bump async to 3.2.2 (#7618)""

This reverts commit 75153a555211d654744c2e61e27fe21085826c22.

* [web] fix usage of async.queue.drain in script

* [clsi] fix usage of async.queue.drain

* [spelling] fix usage of async.queue.drain

* [redis-wrapper] fix usage of async.queue.drain

* [web] Test that LockManager queue is cleared

This protects against a regression found when upgrading the
async package. Here we test that the `queue.drain` callback
is really getting called, and the lock is being removed from
the LOCK_QUEUES map.

* [redis-wrapper] Upgrade async to 3.2.2

GitOrigin-RevId: df921e6d7f1d505bd467f22e58600ba1aff48869
2022-06-22 08:03:35 +00:00

62 lines
1.4 KiB
JavaScript

const RedisWrapper = require('@overleaf/redis-wrapper')
const Settings = require('@overleaf/settings')
const SessionManager = require('../app/src/Features/Authentication/SessionManager')
const async = require('async')
const _ = require('lodash')
const redis = RedisWrapper.createClient(Settings.redis.web)
let totalDeletedSessions = 0
const queue = async.queue(function (sessKey, callback) {
const cb = _.once(callback)
redis.get(sessKey, (_err, session) => {
try {
if (SessionManager.isUserLoggedIn(JSON.parse(session))) {
cb()
} else {
redis.del(sessKey, () => {
totalDeletedSessions++
if (totalDeletedSessions % 1000 === 0) {
console.log(`Keys deleted so far: ${totalDeletedSessions}`)
}
cb()
})
}
} catch (err) {
console.log(`${sessKey} couldn't parse`)
cb()
}
})
}, 20)
function scanAndPurge(cb) {
const stream = redis.scanStream({
match: 'sess:*',
count: 1000,
})
console.log('starting scan')
stream.on('data', resultKeys => {
console.log(`Keys found, count:${resultKeys.length}`)
queue.push(resultKeys)
})
stream.on('end', () => {
queue.drain(() => {
console.log(
`All sessions have been checked, ${totalDeletedSessions} deleted`
)
cb()
})
})
stream.on('error', err => {
console.log(err)
})
}
scanAndPurge(err => {
console.error(err)
process.exit()
})