diff --git a/src/controllers/sessionController.ts b/src/controllers/sessionController.ts index 2f9bf91..a871b77 100644 --- a/src/controllers/sessionController.ts +++ b/src/controllers/sessionController.ts @@ -1,4 +1,5 @@ import { Request, Response } from "express"; +import { addMinutes, formatISO } from "date-fns"; import ActiveSession from "../models/ActiveSession"; import SessionServer from "../models/SessionServer"; import Build from "../models/Build"; @@ -21,7 +22,11 @@ export const startSession = async (req: Request, res: Response) => { const location = req.query.location as string; const buildName = req.query.build as string; const ownerIp = req.headers["x-real-ip"]; - const endAt = req.query.endAt as string; + const endAtParam = req.query.endAt as string | undefined; + const DEFAULT_SESSION_DURATION_MINUTES = 30; + const endAt = endAtParam + ? endAtParam + : formatISO(addMinutes(new Date(), DEFAULT_SESSION_DURATION_MINUTES)); let type = req.query.type as string; const userRegion = req.headers["x-user-region"] as string; @@ -67,7 +72,7 @@ export const startSession = async (req: Request, res: Response) => { } const sessionServers = await SessionServer.find({ location, type }).sort({ - gpuMemoryFree: -1, + gpuMemoryUsed: 1, }); if (sessionServers.length === 0) { @@ -76,9 +81,11 @@ export const startSession = async (req: Request, res: Response) => { } for (const sessionServer of sessionServers) { - const gpuMemoryFree = sessionServer.gpuMemoryFree!; + const gpuMemoryTotal = sessionServer.gpuMemoryTotal ?? 0; + const gpuMemoryUsed = sessionServer.gpuMemoryUsed ?? 0; + const gpuMemoryAvailable = gpuMemoryTotal - gpuMemoryUsed; - if (gpuMemoryFree < build.gpuMemoryUsed!) { + if (gpuMemoryAvailable < build.gpuMemoryUsed!) { continue; } @@ -87,11 +94,13 @@ export const startSession = async (req: Request, res: Response) => { console.log("endAt", endAt); try { + const startAt = formatISO(new Date()); const result: any = await got .post(`${sessionServerUrl}/start`, { json: { buildName, ownerIp, + startAt, endAt, }, }) diff --git a/src/models/ActiveSession.ts b/src/models/ActiveSession.ts index 3657f08..dd3ab7f 100644 --- a/src/models/ActiveSession.ts +++ b/src/models/ActiveSession.ts @@ -32,6 +32,9 @@ const activeSessionSchema = new Schema( connectedPlayersCount: { type: Number, }, + startAt: { + type: Date, + }, endAt: { type: Date, }, diff --git a/src/models/SessionServer.ts b/src/models/SessionServer.ts index 428fc18..0381240 100644 --- a/src/models/SessionServer.ts +++ b/src/models/SessionServer.ts @@ -15,7 +15,10 @@ const sessionServerSchema = new Schema( type: String, unique: true, }, - gpuMemoryFree: { + gpuMemoryTotal: { + type: Number, + }, + gpuMemoryUsed: { type: Number, }, localIP: { diff --git a/src/services/cronService.ts b/src/services/cronService.ts index 135c0c1..36c0b43 100644 --- a/src/services/cronService.ts +++ b/src/services/cronService.ts @@ -1,16 +1,20 @@ import ActiveSession from "../models/ActiveSession"; import got from "got-cjs"; -import { differenceInMinutes, isAfter } from "date-fns"; +import { differenceInMinutes, isAfter, parseISO } from "date-fns"; export async function checkActiveSessions() { const activeSessions = await ActiveSession.find(); + const now = new Date(); for (const activeSession of activeSessions) { - if ( + const shouldEndByEndAt = + activeSession.endAt && isAfter(now, activeSession.endAt); + const shouldEndAsOrphan = !activeSession.endAt && !activeSession.connectedPlayersCount && - differenceInMinutes(new Date(), activeSession.updatedAt) >= 3 - ) { + differenceInMinutes(now, activeSession.updatedAt) >= 3; + + if (shouldEndByEndAt || shouldEndAsOrphan) { const activeSessionId = activeSession.id; try { @@ -39,7 +43,7 @@ export async function checkScheduledSessions() { for (const session of scheduledSessions) { if ( - isAfter(new Date(), new Date(session.startAt)) && + isAfter(new Date(), parseISO(session.startAt)) && !session.activeSessionId ) { console.log("session.buildId", session.buildId); @@ -69,7 +73,7 @@ export async function checkScheduledSessions() { .json(); console.log("result2: ", result2); - } else if (isAfter(new Date(), new Date(session.endAt))) { + } else if (isAfter(new Date(), parseISO(session.endAt))) { const result = await got .post(`https://coord.graff.tech/end`, { json: { activeSessionId: session.activeSessionId },