Merge pull request #14 from lancatlin/server-session-manage

* add `status` and `lastActivityAt` field to session
* automatically delete expiry container, mark session as inactive
* default retrieving only active session on `GET /sessions`
* create container on session creation, start container on socket connection
This commit is contained in:
Lin, Hung-Shin authored and GitHub committed 2023-08-24 11:38:02 +08:00
commit 5da311d9a4
17 files changed
+173 -29

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"
@@ -26,10 +26,18 @@ export async function newCommand(req, res) {
const session = await Session.findById(sessionId)
if (!session) {
res.status(401).json({ message: 'session not found' })
res.status(404).json({ message: 'session not found' })
return
}
if (session.status !== 'active') {
res.status(400).json({ message: 'session is not active' })
return
}
session.lastActivityAt = new Date()
await session.save()
const c = new Command({
session,
command,
@@ -1,5 +1,6 @@
import Session from '../../models/session.js'
import Quest from '../../models/quest.js'
import { createContainer } from '../../containers/docker.js'
export async function getSessionList(req, res) {
try {
@@ -7,8 +8,29 @@ export async function getSessionList(req, res) {
if (req.query.quest_id) {
query.quest = req.query.quest_id
}
query.status = req.query.status || 'active'
const sessions = await Session.find(query)
res.json(sessions)
res.json(
sessions.map(
({
_id,
user,
quest,
status,
progress,
createdAt,
lastActivityAt,
}) => ({
_id,
user,
quest,
status,
progress,
createdAt,
lastActivityAt,
})
)
)
} catch (err) {
console.error(err)
res.status(500).json({ message: err.message })
@@ -24,11 +46,14 @@ export async function createSession(req, res) {
try {
const progress = quest.stages[0].id
const container = await createContainer(
`quest-${quest.id}-${req.user.username}-${Date.now()}`
)
const newSession = new Session({
user: req.user,
quest,
progress,
containerId: `quest-${quest.id}-${req.user.username}-${Date.now()}`,
containerId: container.id,
})
const session = await newSession.save()
+3 -2
View File
@@ -1,5 +1,5 @@
import { Server } from 'socket.io'
import { getOrCreateContainer, attachContainer } from '../containers/docker.js'
import { getAndStartContainer, attachContainer } from '../containers/docker.js'
import Session from '../models/session.js'
import { defaultUser, genSessionJWT } from '../utils/auth.js'
@@ -47,6 +47,7 @@ export default (server) => {
session = await Session.findOne({
_id: sessionId,
user: socket.user,
status: 'active',
})
if (!session) {
socket.send('Session not found.')
@@ -61,7 +62,7 @@ export default (server) => {
const token = await genSessionJWT(session)
const container = await getOrCreateContainer(session.containerId)
const container = await getAndStartContainer(session.containerId)
const stream = await attachContainer(container, { token })
stream.on('data', (chunk) => {
+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),
}
+24 -15
View File
@@ -24,7 +24,16 @@ const containerOptions = {
const network = engine.getNetwork(config.dockerNetwork)
export async function getOrCreateContainer(id) {
export async function createContainer(name) {
const container = await engine.createContainer({
...containerOptions,
name,
})
await network.connect({ Container: container.id })
return container
}
export async function getAndStartContainer(id) {
console.log(`Getting container: ${id}`)
let container
try {
@@ -34,20 +43,7 @@ export async function getOrCreateContainer(id) {
}
return container
} catch (error) {
try {
container = await engine.createContainer({
...containerOptions,
name: id,
})
} catch (err) {
console.log(err)
throw err
}
await network.connect({ Container: container.id })
await container.start()
console.log(container.id)
return container
throw new Error(`Container not found: ${id}`)
}
}
@@ -69,3 +65,16 @@ 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()
} catch (error) {
console.error(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 = 'inactive'
session.containerId = null
await session.save()
})
)
console.log('Done checking for expired sessions')
}
export default function expiryRemovalScheduler() {
schedule.scheduleJob('*/5 * * * *', removeExpired)
}
+6
View File
@@ -42,6 +42,12 @@ async function commandHandler(session, commandInput, additionalData) {
session.progress = stage.next
await session.save()
if (stage.next === 'END') {
session.status = 'finished'
session.finishedAt = new Date()
await session.save()
}
return {
responses: stage.responses,
hints: stage.hints,
+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)
+3 -3
View File
@@ -17,11 +17,11 @@ const Command = model(
type: String,
required: true,
},
exit_code: {
type: Number,
output: {
type: String,
required: false,
},
output: {
error: {
type: String,
required: false,
},
+11
View File
@@ -13,7 +13,18 @@ const Session = model(
ref: 'Quest',
},
containerId: String,
status: {
type: String,
enum: ['active', 'finished', 'inactive'],
required: true,
default: 'active',
},
finishedAt: Date,
lastActivityAt: {
type: Date,
default: Date.now,
required: true,
},
progress: String,
completion: [String],
hints: [String],
+6
View File
@@ -31,6 +31,12 @@ paths:
description: find sessions by quest id
schema:
type: string
- in: query
name: status
description: find sessions by status
schema:
type: string
enum: [active, finished, inactive]
responses:
'200':
description: list of existing session