feat: add session expiry deletion script and scheduler

This commit is contained in:
wancat committed 2023-08-23 12:22:15 +08:00
1 parent 8e63284211
commit 22060070b9
8 files changed
+94 -3

No files matched your search

+3 -2
View File
@@ -6,8 +6,8 @@
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"cli": "node src/client.js",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)"
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"clean": "node src/cli/clean.js"
},
"dependencies": {
"commander": "^11.0.0",
@@ -16,6 +16,7 @@
"jsonwebtoken": "^9.0.1",
"minimist": "^1.2.8",
"mongoose": "^7.4.0",
"node-schedule": "^2.1.1",
"socket.io": "^4.7.1",
"swagger-ui-express": "^5.0.0",
"yaml": "^2.3.1"
+12
View File
@@ -0,0 +1,12 @@
import { removeExpired } from '../containers/expiryChecker.js'
import connectDB from '../db.js'
connectDB()
.then(removeExpired)
.then(() => {
process.exit(0)
})
.catch((err) => {
console.error(err)
process.exit(1)
})
+1
View File
@@ -8,4 +8,5 @@ export default {
dockerNetwork: get('DOCKER_NETWORK', 'linux-odyssey-players'),
isProduction: process.env.NODE_ENV === 'production',
hostPwd: get('HOST_PWD', ''),
expiry: get('EXPIRY', 1000 * 60 * 60),
}
+15
View File
@@ -65,3 +65,18 @@ export async function attachContainer(container, { token }) {
return execOutput.socket
}
export async function deleteContainer(id) {
const container = engine.getContainer(id)
if (!container) {
return
}
try {
await container.stop()
await container.remove({
force: true,
})
} catch (error) {
console.log(error)
}
}
+28
View File
@@ -0,0 +1,28 @@
import schedule from 'node-schedule'
import config from '../config.js'
import Session from '../models/session.js'
import { deleteContainer } from './docker.js'
export async function removeExpired() {
// Find all sessions that are containerId is not null and lastActivityAt is
// less than the current time minus the expiry time
console.log('Checking for expired sessions')
const sessions = await Session.find({
containerId: { $ne: null },
lastActivityAt: { $lt: new Date(Date.now() - config.expiry) },
})
await Promise.all(
sessions.map(async (session) => {
console.log('Expiring session: ', session._id, session.containerId)
await deleteContainer(session.containerId)
session.status = 'deactive'
session.containerId = null
await session.save()
})
)
console.log('Done checking for expired sessions')
}
export default function expiryRemovalScheduler() {
schedule.scheduleJob('*/5 * * * *', removeExpired)
}
+2
View File
@@ -10,6 +10,7 @@ import apiRouter from './api/routes/index.js'
import loadAndUpdateQuests from './utils/quest.js'
import config from './config.js'
import errorHandler from './middleware/error.js'
import expiryRemovalScheduler from './containers/expiryChecker.js'
async function main() {
const app = express()
@@ -17,6 +18,7 @@ async function main() {
socketServer(server)
await connectDB()
await loadAndUpdateQuests()
expiryRemovalScheduler()
const file = await fs.readFile('./swagger.yaml', 'utf8')
const swaggerDocument = YAML.parse(file)