mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-07 19:10:12 +08:00
feat: delete previous container on session creation
This commit is contained in:
1 parent
96678a45c3
commit
2285192eac
2 files changed
+24
-7
No files matched your search
+2
-1
@@ -1,6 +1,5 @@
|
|||||||
FROM node:20-alpine AS build
|
FROM node:20-alpine AS build
|
||||||
|
|
||||||
COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY ./package.json yarn.lock ./
|
COPY ./package.json yarn.lock ./
|
||||||
COPY server/package.json ./server/package.json
|
COPY server/package.json ./server/package.json
|
||||||
@@ -8,6 +7,8 @@ COPY packages/file-graph/package.json ./packages/file-graph/package.json
|
|||||||
COPY packages/utils/package.json ./packages/utils/package.json
|
COPY packages/utils/package.json ./packages/utils/package.json
|
||||||
COPY packages/models/package.json ./packages/models/package.json
|
COPY packages/models/package.json ./packages/models/package.json
|
||||||
RUN yarn install --frozen-lockfile --production
|
RUN yarn install --frozen-lockfile --production
|
||||||
|
# Install docker cli
|
||||||
|
COPY --from=docker:dind /usr/local/bin/docker /usr/local/bin/
|
||||||
|
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Session, Quest } from '@linux-odyssey/models'
|
import { Session, Quest } from '@linux-odyssey/models'
|
||||||
import { createContainer } from '../../containers/docker.js'
|
import { createContainer, deleteContainer } from '../../containers/docker.js'
|
||||||
import SessionHandler from '../../game/sessionHandler.js'
|
import SessionHandler from '../../game/sessionHandler.js'
|
||||||
|
|
||||||
function sessionSummary(session) {
|
function sessionSummary(session) {
|
||||||
@@ -37,6 +37,23 @@ export async function getSessionList(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function deactivateSessions(user, quest) {
|
||||||
|
const sessions = await Session.find({
|
||||||
|
user,
|
||||||
|
quest,
|
||||||
|
status: 'active',
|
||||||
|
})
|
||||||
|
return Promise.all(
|
||||||
|
sessions.map((session) => {
|
||||||
|
return deleteContainer(session.containerId).then(() => {
|
||||||
|
session.status = 'inactive'
|
||||||
|
session.containerId = null
|
||||||
|
return session.save()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export async function createSession(req, res) {
|
export async function createSession(req, res) {
|
||||||
const quest = await Quest.findById(req.body.quest_id)
|
const quest = await Quest.findById(req.body.quest_id)
|
||||||
if (!quest) {
|
if (!quest) {
|
||||||
@@ -44,11 +61,10 @@ export async function createSession(req, res) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// deactive all active sessions
|
// deactivate all active sessions
|
||||||
await Session.updateMany(
|
deactivateSessions(req.user._id, quest._id).catch((err) => {
|
||||||
{ user: req.user._id, quest, status: 'active' },
|
console.error(err)
|
||||||
{ status: 'inactive' }
|
})
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const container = await createContainer(
|
const container = await createContainer(
|
||||||
|
|||||||
Reference in new issue
Block a user