Refactor index.ts: streamline server setup, integrate routing, and add error handling middleware. Removed unused code and initialized cron jobs.

This commit is contained in:
2026-01-13 16:15:18 +05:00
parent dbab2c6305
commit d0563cb9b2
10 changed files with 1007 additions and 294 deletions
+16
View File
@@ -0,0 +1,16 @@
import cron from "node-cron";
import { checkActiveSessions, checkScheduledSessions } from "../services/cronService";
export function initializeCronJobs() {
// Check active sessions every second
cron.schedule("* * * * * *", () => {
checkActiveSessions();
});
// Check scheduled sessions every second
cron.schedule("* * * * * *", () => {
checkScheduledSessions();
});
console.log("Cron jobs initialized");
}
+13
View File
@@ -0,0 +1,13 @@
import { Request, Response } from "express";
import Build from "../models/Build";
export const getBuilds = async (req: Request, res: Response) => {
try {
const builds = await Build.find();
res.json(builds);
} catch (error) {
res.status(500).json({
error: error instanceof Error ? error.message : "Unknown error"
});
}
};
+25
View File
@@ -0,0 +1,25 @@
import { Request, Response } from "express";
import SessionServer from "../models/SessionServer";
import LaunchLog from "../models/LaunchLog";
export const getSessionServers = async (req: Request, res: Response) => {
try {
const sessionServers = await SessionServer.find();
res.json(sessionServers);
} catch (error) {
res.status(500).json({
error: error instanceof Error ? error.message : "Unknown error"
});
}
};
export const getLaunchLogs = async (req: Request, res: Response) => {
try {
const result = await LaunchLog.find().limit(200);
res.json(result);
} catch (error) {
res.status(500).json({
error: error instanceof Error ? error.message : "Unknown error"
});
}
};
+154
View File
@@ -0,0 +1,154 @@
import { Request, Response } from "express";
import ActiveSession from "../models/ActiveSession";
import SessionServer from "../models/SessionServer";
import Build from "../models/Build";
import LaunchLog from "../models/LaunchLog";
import got from "got-cjs";
import { getMessage } from "../utils/messages";
export const getActiveSessions = async (req: Request, res: Response) => {
try {
const activeSessions = await ActiveSession.find();
res.json(activeSessions);
} catch (error) {
res.status(500).json({
error: error instanceof Error ? error.message : "Unknown error"
});
}
};
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;
let type = req.query.type as string;
const userRegion = req.headers["x-user-region"] as string;
console.log("location", location);
console.log("buildName", buildName);
if (!location || !buildName) {
return res.json({ error: getMessage("MISSING_PARAMETERS", userRegion) });
}
if (type !== "prod") {
type = "demo";
}
const build = await Build.findOne({ name: buildName });
if (!build) {
return res.json({ error: getMessage("BUILD_NOT_FOUND", userRegion) });
}
console.log(build);
const activeSessionsWithBuild = await ActiveSession.find({
location,
buildName,
type,
});
if (
type === "demo" &&
build.demoLaunchLimit &&
activeSessionsWithBuild.length >= build.demoLaunchLimit
) {
return res.json({ error: getMessage("DEMO_LAUNCH_LIMIT_REACHED", userRegion) });
}
if (
type === "prod" &&
build.prodLaunchLimit &&
activeSessionsWithBuild.length >= build.prodLaunchLimit
) {
return res.json({ error: getMessage("PROD_LAUNCH_LIMIT_REACHED", userRegion) });
}
const sessionServers = await SessionServer.find({ location, type }).sort({
gpuMemoryFree: -1,
});
if (sessionServers.length === 0) {
console.log("sessionServers.length", sessionServers.length);
return res.json({ error: 2 });
}
for (const sessionServer of sessionServers) {
const gpuMemoryFree = sessionServer.gpuMemoryFree!;
if (gpuMemoryFree < build.gpuMemoryUsed!) {
continue;
}
const sessionServerUrl = `https://${location}.sess.stream.graff.tech/server/${sessionServer.localIP}:3000`;
console.log("endAt", endAt);
try {
const result: any = await got
.post(`${sessionServerUrl}/start`, {
json: {
buildName,
ownerIp,
endAt,
},
})
.json();
console.log("Result: ", result);
console.log("typeof endAt", typeof endAt, endAt);
if (result.id) {
await LaunchLog.create({
location,
server: sessionServer.name,
type,
buildName,
ownerIp,
hostname: sessionServer.hostname,
localIP: sessionServer.localIP,
});
}
return res.json({ stream: result.id });
} catch (error) {
console.log("error", error);
continue;
}
}
res.json({
error: getMessage("NO_SERVERS_AVAILABLE", userRegion),
});
};
export const endSession = async (req: Request, res: Response) => {
const activeSessionId = req.body.activeSessionId;
const userRegion = req.headers["x-user-region"] as string;
console.log("activeSessionId", activeSessionId);
try {
const activeSession = await ActiveSession.findById(activeSessionId);
console.log("activeSession", activeSession);
if (!activeSession) {
return res.json({ error: getMessage("SESSION_NOT_FOUND", userRegion) });
}
const result = await got
.post(
`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`,
{ json: { activeSessionId } }
)
.json();
res.json(result);
} catch (error) {
res.status(500).json({
error: error instanceof Error ? error.message : "Unknown error"
});
}
};
+20 -294
View File
@@ -1,310 +1,36 @@
import "dotenv/config";
import connectDB from "./config/db";
import express, { json } from "express";
import cors from "cors";
import SessionServer from "./models/SessionServer";
import ActiveSession from "./models/ActiveSession";
import got from "got-cjs";
import Build from "./models/Build";
import { AccessToken } from "livekit-server-sdk";
import cron from "node-cron";
import { differenceInMinutes, isAfter, parseISO } from "date-fns";
import LaunchLog from "./models/LaunchLog";
import connectDB from "./config/db";
import { initializeCronJobs } from "./config/cron";
import { errorHandler } from "./middleware/errorHandler";
import router from "./routes";
// Connect to database
connectDB();
// Initialize Express app
const app = express();
const port = process.env.PORT;
const port = process.env.PORT || 3000;
// Middleware
app.use(json());
app.use(cors());
app.use(
cors({
origin: "*",
})
);
async function getBuild(name: string) {
const build = await Build.findOne({ name });
// Routes
app.use(router);
return build;
}
app.get("/active_sessions", async (req, res) => {
const activeSessions = await ActiveSession.find();
res.json(activeSessions);
});
app.get("/builds", async (req, res) => {
const builds = await Build.find();
res.json(builds);
});
app.get("/start", async (req, res) => {
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;
let type = req.query.type as string;
console.log("location", location);
console.log("buildName", buildName);
if (!location || !buildName) {
return res.json({ error: 1 });
}
if (type !== "prod") {
type = "demo";
}
const build = await getBuild(buildName);
if (!build) {
return res.json({ error: "Build not found" });
}
console.log(build);
const activeSessionsWithBuild = await ActiveSession.find({
location,
buildName,
type,
});
if (
type === "demo" &&
build.demoLaunchLimit &&
activeSessionsWithBuild.length >= build.demoLaunchLimit
) {
return res.json({ error: "The demo build launch limit has been reached." });
}
if (
type === "prod" &&
build.prodLaunchLimit &&
activeSessionsWithBuild.length >= build.prodLaunchLimit
) {
return res.json({ error: "The prod build launch limit has been reached." });
}
const sessionServers = await SessionServer.find({ location, type }).sort({
gpuMemoryFree: -1,
});
if (sessionServers.length === 0) {
console.log("sessionServers.length", sessionServers.length);
return res.json({ error: 2 });
}
for (const sessionServer of sessionServers) {
const gpuMemoryFree = sessionServer.gpuMemoryFree!;
if (gpuMemoryFree < build.gpuMemoryUsed!) {
continue;
}
const sessionServerUrl = `https://${location}.sess.stream.graff.tech/server/${sessionServer.localIP}:3000`;
console.log("endAt", endAt);
try {
const result: any = await got
.post(`${sessionServerUrl}/start`, {
json: {
buildName,
ownerIp,
endAt,
},
})
.json();
console.log("Result: ", result);
console.log("typeof endAt", typeof endAt, endAt);
if (result.id) {
await LaunchLog.create({
location,
server: sessionServer.name,
type,
buildName,
ownerIp,
hostname: sessionServer.hostname,
localIP: sessionServer.localIP,
});
}
return res.json({ stream: result.id });
} catch (error) {
console.log("error", error);
continue;
// return res.json({ error: (error as Error).message });
}
}
res.json({
error:
"There are no servers available to run the build. Please try again later.",
});
});
app.post("/end", async (req, res) => {
const activeSessionId = req.body.activeSessionId;
console.log("activeSessionId", activeSessionId);
const activeSession = await ActiveSession.findById(activeSessionId);
console.log("activeSession", activeSession);
if (!activeSession) {
return res.json({ error: "A session with this ID was not found" });
}
const result = await got
.post(
`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`,
{ json: { activeSessionId } }
)
.json();
res.json(result);
});
// const createToken = (roomName: string, participantName: string) => {
// const at = new AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", {
// identity: participantName,
// });
// at.addGrant({ roomJoin: true, room: roomName });
// return at.toJwt();
// };
// app.get("/getToken", (req, res) => {
// if (!req.query.roomName && !req.query.participantName) {
// return res.json({ error: "roomName or participantName is not defined" });
// }
// try {
// const token = createToken(
// req.query.roomName as string,
// req.query.participantName as string
// );
// res.json({ token });
// } catch (error) {
// if (error instanceof Error) {
// res.json({ error: error.message });
// }
// }
// });
app.get("/session_servers", async (req, res) => {
try {
const sessionServers = await SessionServer.find();
res.json(sessionServers);
} catch (error) {
if (error instanceof Error) {
res.json({ error: error.message });
}
}
});
app.get("/launch_logs", async (req, res) => {
try {
const result = await LaunchLog.find().limit(200);
res.json(result);
} catch (error) {
if (error instanceof Error) {
res.json({ error: error.message });
}
}
});
// Error handling middleware
app.use(errorHandler);
// Start server
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
async function checkActiveSessions() {
const activeSessions = await ActiveSession.find();
for (const activeSession of activeSessions) {
if (
!activeSession.endAt &&
!activeSession.connectedPlayersCount &&
differenceInMinutes(new Date(), activeSession.updatedAt) >= 3
) {
const activeSessionId = activeSession.id;
try {
const result = await got
.post(
`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`,
{ json: { activeSessionId } }
)
.json();
console.log("Result:", result);
} catch (error) {
console.log({ error });
}
}
}
}
async function checkScheduledSessions() {
try {
const scheduledSessions: any = await got
.get(`${process.env.CRM_API_URL}/scheduledSessions`)
.json();
if (!scheduledSessions.length) return;
for (const session of scheduledSessions) {
if (
isAfter(new Date(), new Date(session.startAt)) &&
!session.activeSessionId
) {
console.log("session.buildId", session.buildId);
const { build }: any = await got
.get(`${process.env.CRM_API_URL}/builds/${session.buildId}`)
.json();
console.log("build", build);
const result: any = await got
.get(
`https://coord.graff.tech/start?build=${build}&location=a1&type=prod&endAt=${session.endAt}`
)
.json();
if (!result.stream) {
console.log("Not result.stream");
return;
}
const result2 = await got
.put(`${process.env.CRM_API_URL}/scheduledSessions/${session.id}`, {
json: {
activeSessionId: result.stream,
},
})
.json();
console.log("result2: ", result2);
} else if (isAfter(new Date(), new Date(session.endAt))) {
const result = await got
.post(`https://coord.graff.tech/end`, {
json: { activeSessionId: session.activeSessionId },
})
.json();
console.log("CRON End active session: ", result);
}
}
} catch (error) {
console.log("Error: ", (error as Error).message);
}
}
cron.schedule("* * * * * *", () => {
checkActiveSessions();
});
cron.schedule("* * * * * *", () => {
checkScheduledSessions();
});
// Initialize cron jobs
initializeCronJobs();
+14
View File
@@ -0,0 +1,14 @@
import { Request, Response, NextFunction } from "express";
export const errorHandler = (
err: Error,
req: Request,
res: Response,
next: NextFunction
) => {
console.error("Error:", err);
res.status(500).json({
error: err.message || "Internal server error",
});
};
+20
View File
@@ -0,0 +1,20 @@
import { Router } from "express";
import { getActiveSessions, startSession, endSession } from "../controllers/sessionController";
import { getBuilds } from "../controllers/buildController";
import { getSessionServers, getLaunchLogs } from "../controllers/serverController";
const router = Router();
// Session routes
router.get("/active_sessions", getActiveSessions);
router.get("/start", startSession);
router.post("/end", endSession);
// Build routes
router.get("/builds", getBuilds);
// Server routes
router.get("/session_servers", getSessionServers);
router.get("/launch_logs", getLaunchLogs);
export default router;
+85
View File
@@ -0,0 +1,85 @@
import ActiveSession from "../models/ActiveSession";
import got from "got-cjs";
import { differenceInMinutes, isAfter } from "date-fns";
export async function checkActiveSessions() {
const activeSessions = await ActiveSession.find();
for (const activeSession of activeSessions) {
if (
!activeSession.endAt &&
!activeSession.connectedPlayersCount &&
differenceInMinutes(new Date(), activeSession.updatedAt) >= 3
) {
const activeSessionId = activeSession.id;
try {
const result = await got
.post(
`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`,
{ json: { activeSessionId } }
)
.json();
console.log("Result:", result);
} catch (error) {
console.log({ error });
}
}
}
}
export async function checkScheduledSessions() {
try {
const scheduledSessions: any = await got
.get(`${process.env.CRM_API_URL}/scheduledSessions`)
.json();
if (!scheduledSessions.length) return;
for (const session of scheduledSessions) {
if (
isAfter(new Date(), new Date(session.startAt)) &&
!session.activeSessionId
) {
console.log("session.buildId", session.buildId);
const { build }: any = await got
.get(`${process.env.CRM_API_URL}/builds/${session.buildId}`)
.json();
console.log("build", build);
const result: any = await got
.get(
`https://coord.graff.tech/start?build=${build}&location=a1&type=prod&endAt=${session.endAt}`
)
.json();
if (!result.stream) {
console.log("Not result.stream");
return;
}
const result2 = await got
.put(`${process.env.CRM_API_URL}/scheduledSessions/${session.id}`, {
json: {
activeSessionId: result.stream,
},
})
.json();
console.log("result2: ", result2);
} else if (isAfter(new Date(), new Date(session.endAt))) {
const result = await got
.post(`https://coord.graff.tech/end`, {
json: { activeSessionId: session.activeSessionId },
})
.json();
console.log("CRON End active session: ", result);
}
}
} catch (error) {
console.log("Error: ", (error as Error).message);
}
}
+35
View File
@@ -0,0 +1,35 @@
type Language = 'RU' | 'EN';
const messages = {
BUILD_NOT_FOUND: {
EN: 'Build not found',
RU: 'Сборка не найдена',
},
DEMO_LAUNCH_LIMIT_REACHED: {
EN: 'The demo build launch limit has been reached.',
RU: 'Достигнут лимит запусков демо-сборки.',
},
PROD_LAUNCH_LIMIT_REACHED: {
EN: 'The prod build launch limit has been reached.',
RU: 'Достигнут лимит запусков продакшн-сборки.',
},
NO_SERVERS_AVAILABLE: {
EN: 'There are no servers available to run the build. Please try again later.',
RU: 'Нет доступных серверов для запуска сборки. Пожалуйста, попробуйте позже.',
},
SESSION_NOT_FOUND: {
EN: 'A session with this ID was not found',
RU: 'Сессия с данным ID не найдена',
},
MISSING_PARAMETERS: {
EN: 'Missing required parameters',
RU: 'Отсутствуют обязательные параметры',
},
} as const;
type MessageKey = keyof typeof messages;
export function getMessage(key: MessageKey, region?: string): string {
const lang: Language = region === 'RU' ? 'RU' : 'EN';
return messages[key][lang];
}