mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
feat: add session expiry deletion script and scheduler
This commit is contained in:
1 parent
8e63284211
commit
22060070b9
8 files changed
+94
-3
No files matched your search
@@ -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)
|
||||
})
|
||||
@@ -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),
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user