Feat: adds async local storage to admin server
This commit is contained in:
parent
8128f05ffb
commit
cb2e1b3907
5 changed files with 47 additions and 8 deletions
23
lib/new-admin/middlewares/cleanUserSessions.js
Normal file
23
lib/new-admin/middlewares/cleanUserSessions.js
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
const { asyncLocalStorage } = require('../../async-storage')
|
||||
const db = require('../../db')
|
||||
const { USER_SESSIONS_TABLE_NAME } = require('../../constants')
|
||||
|
||||
const schemaCache = {}
|
||||
|
||||
const cleanUserSessions = (cleanInterval) => (req, res, next) => {
|
||||
const schema = asyncLocalStorage.getStore() ? asyncLocalStorage.getStore().get('schema') : null
|
||||
const now = Date.now()
|
||||
|
||||
if (!schema) return next()
|
||||
if (schema && schemaCache.schema + cleanInterval > now) return next()
|
||||
|
||||
console.log('Clearing old sessions for schema', schema)
|
||||
return db.$none('DELETE FROM $1^ WHERE expire < to_timestamp($2)', [USER_SESSIONS_TABLE_NAME, now])
|
||||
.then(() => {
|
||||
schemaCache.schema = now
|
||||
return next()
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
module.exports = cleanUserSessions
|
||||
Loading…
Add table
Add a link
Reference in a new issue