Enhance session management: add startAt and endAt fields to ActiveSession model, update sessionController to handle default session duration, and improve memory management logic in SessionServer. Modify cronService to correctly parse and check session times.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
import { addMinutes, formatISO } from "date-fns";
|
||||||
import ActiveSession from "../models/ActiveSession";
|
import ActiveSession from "../models/ActiveSession";
|
||||||
import SessionServer from "../models/SessionServer";
|
import SessionServer from "../models/SessionServer";
|
||||||
import Build from "../models/Build";
|
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 location = req.query.location as string;
|
||||||
const buildName = req.query.build as string;
|
const buildName = req.query.build as string;
|
||||||
const ownerIp = req.headers["x-real-ip"];
|
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;
|
let type = req.query.type as string;
|
||||||
const userRegion = req.headers["x-user-region"] 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({
|
const sessionServers = await SessionServer.find({ location, type }).sort({
|
||||||
gpuMemoryFree: -1,
|
gpuMemoryUsed: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (sessionServers.length === 0) {
|
if (sessionServers.length === 0) {
|
||||||
@@ -76,9 +81,11 @@ export const startSession = async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const sessionServer of sessionServers) {
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,11 +94,13 @@ export const startSession = async (req: Request, res: Response) => {
|
|||||||
console.log("endAt", endAt);
|
console.log("endAt", endAt);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const startAt = formatISO(new Date());
|
||||||
const result: any = await got
|
const result: any = await got
|
||||||
.post(`${sessionServerUrl}/start`, {
|
.post(`${sessionServerUrl}/start`, {
|
||||||
json: {
|
json: {
|
||||||
buildName,
|
buildName,
|
||||||
ownerIp,
|
ownerIp,
|
||||||
|
startAt,
|
||||||
endAt,
|
endAt,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ const activeSessionSchema = new Schema(
|
|||||||
connectedPlayersCount: {
|
connectedPlayersCount: {
|
||||||
type: Number,
|
type: Number,
|
||||||
},
|
},
|
||||||
|
startAt: {
|
||||||
|
type: Date,
|
||||||
|
},
|
||||||
endAt: {
|
endAt: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ const sessionServerSchema = new Schema(
|
|||||||
type: String,
|
type: String,
|
||||||
unique: true,
|
unique: true,
|
||||||
},
|
},
|
||||||
gpuMemoryFree: {
|
gpuMemoryTotal: {
|
||||||
|
type: Number,
|
||||||
|
},
|
||||||
|
gpuMemoryUsed: {
|
||||||
type: Number,
|
type: Number,
|
||||||
},
|
},
|
||||||
localIP: {
|
localIP: {
|
||||||
|
|||||||
@@ -1,16 +1,20 @@
|
|||||||
import ActiveSession from "../models/ActiveSession";
|
import ActiveSession from "../models/ActiveSession";
|
||||||
import got from "got-cjs";
|
import got from "got-cjs";
|
||||||
import { differenceInMinutes, isAfter } from "date-fns";
|
import { differenceInMinutes, isAfter, parseISO } from "date-fns";
|
||||||
|
|
||||||
export async function checkActiveSessions() {
|
export async function checkActiveSessions() {
|
||||||
const activeSessions = await ActiveSession.find();
|
const activeSessions = await ActiveSession.find();
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
for (const activeSession of activeSessions) {
|
for (const activeSession of activeSessions) {
|
||||||
if (
|
const shouldEndByEndAt =
|
||||||
|
activeSession.endAt && isAfter(now, activeSession.endAt);
|
||||||
|
const shouldEndAsOrphan =
|
||||||
!activeSession.endAt &&
|
!activeSession.endAt &&
|
||||||
!activeSession.connectedPlayersCount &&
|
!activeSession.connectedPlayersCount &&
|
||||||
differenceInMinutes(new Date(), activeSession.updatedAt) >= 3
|
differenceInMinutes(now, activeSession.updatedAt) >= 3;
|
||||||
) {
|
|
||||||
|
if (shouldEndByEndAt || shouldEndAsOrphan) {
|
||||||
const activeSessionId = activeSession.id;
|
const activeSessionId = activeSession.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -39,7 +43,7 @@ export async function checkScheduledSessions() {
|
|||||||
|
|
||||||
for (const session of scheduledSessions) {
|
for (const session of scheduledSessions) {
|
||||||
if (
|
if (
|
||||||
isAfter(new Date(), new Date(session.startAt)) &&
|
isAfter(new Date(), parseISO(session.startAt)) &&
|
||||||
!session.activeSessionId
|
!session.activeSessionId
|
||||||
) {
|
) {
|
||||||
console.log("session.buildId", session.buildId);
|
console.log("session.buildId", session.buildId);
|
||||||
@@ -69,7 +73,7 @@ export async function checkScheduledSessions() {
|
|||||||
.json();
|
.json();
|
||||||
|
|
||||||
console.log("result2: ", result2);
|
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
|
const result = await got
|
||||||
.post(`https://coord.graff.tech/end`, {
|
.post(`https://coord.graff.tech/end`, {
|
||||||
json: { activeSessionId: session.activeSessionId },
|
json: { activeSessionId: session.activeSessionId },
|
||||||
|
|||||||
Reference in New Issue
Block a user