From c733675cbcb43d625a2c125e718ffd40c67908e3 Mon Sep 17 00:00:00 2001 From: inmake Date: Thu, 3 Oct 2024 17:14:49 +0500 Subject: [PATCH] upd --- .gitignore | 24 +++- dist/config/db.js | 16 --- dist/index.js | 254 ----------------------------------- dist/models/ActiveSession.js | 47 ------- dist/models/Build.js | 23 ---- dist/models/SessionServer.js | 30 ----- dist/routes/test.js | 9 -- src/index.ts | 1 + 8 files changed, 24 insertions(+), 380 deletions(-) delete mode 100644 dist/config/db.js delete mode 100644 dist/index.js delete mode 100644 dist/models/ActiveSession.js delete mode 100644 dist/models/Build.js delete mode 100644 dist/models/SessionServer.js delete mode 100644 dist/routes/test.js diff --git a/.gitignore b/.gitignore index 76add87..a547bf3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + node_modules -dist \ No newline at end of file +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/dist/config/db.js b/dist/config/db.js deleted file mode 100644 index 48f7f9c..0000000 --- a/dist/config/db.js +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const mongoose_1 = require("mongoose"); -async function connectDB() { - try { - await (0, mongoose_1.connect)(process.env.MONGO_URI, { dbName: "pixel_streaming2" }); - console.log("MongoDB connected..."); - } - catch (error) { - if (error instanceof Error) { - console.error(error.message); - } - process.exit(1); - } -} -exports.default = connectDB; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index f3de339..0000000 --- a/dist/index.js +++ /dev/null @@ -1,254 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -require("dotenv/config"); -const db_1 = __importDefault(require("./config/db")); -const express_1 = __importStar(require("express")); -const cors_1 = __importDefault(require("cors")); -const SessionServer_1 = __importDefault(require("./models/SessionServer")); -const ActiveSession_1 = __importDefault(require("./models/ActiveSession")); -const got_cjs_1 = __importDefault(require("got-cjs")); -const Build_1 = __importDefault(require("./models/Build")); -const node_cron_1 = __importDefault(require("node-cron")); -const date_fns_1 = require("date-fns"); -const LaunchLog_1 = __importDefault(require("./models/LaunchLog")); -(0, db_1.default)(); -const app = (0, express_1.default)(); -const port = process.env.PORT; -app.use((0, express_1.json)()); -app.use((0, cors_1.default)()); -async function getBuild(name) { - const build = await Build_1.default.findOne({ name }); - return build; -} -app.get("/active_sessions", async (req, res) => { - const activeSessions = await ActiveSession_1.default.find(); - res.json(activeSessions); -}); -app.get("/start", async (req, res) => { - const location = req.query.location; - const buildName = req.query.build; - const ownerIp = req.headers["x-real-ip"]; - const endAt = req.query.endAt; - let type = req.query.type; - 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_1.default.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_1.default.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 = await got_cjs_1.default - .post(`${sessionServerUrl}/start`, { - json: { - buildName, - ownerIp, - endAt, - }, - }) - .json(); - console.log("Result: ", result); - console.log("typeof endAt", typeof endAt, endAt); - if (result.id) { - await LaunchLog_1.default.create({ - location, - server: sessionServer.name, - type, - buildName, - ownerIp, - }); - } - return res.json({ stream: result.id }); - } - catch (error) { - if (error instanceof Error) { - return res.json({ error: 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_1.default.findById(activeSessionId); - console.log("activeSession", activeSession); - if (!activeSession) { - return res.json({ error: "A session with this ID was not found" }); - } - const result = await got_cjs_1.default - .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_1.default.find(); - res.json(sessionServers); - } - catch (error) { - if (error instanceof Error) { - res.json({ error: error.message }); - } - } -}); -app.listen(port, () => { - console.log(`Server is listening on port ${port}`); -}); -async function checkActiveSessions() { - const activeSessions = await ActiveSession_1.default.find(); - for (const activeSession of activeSessions) { - if (!activeSession.connectedPlayersCount && - (0, date_fns_1.differenceInMinutes)(new Date(), activeSession.updatedAt) > 2) { - const activeSessionId = activeSession.id; - try { - const result = await got_cjs_1.default - .post(`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`, { json: { activeSessionId } }) - .json(); - console.log("Result:", result); - } - catch (error) { - if (error instanceof Error) { - console.log({ error }); - } - } - } - } -} -async function checkScheduledSessions() { - try { - const scheduledSessions = await got_cjs_1.default - .get(`${process.env.CRM_API_URL}/scheduled_sessions`) - .json(); - if (!scheduledSessions.length) - return; - for (const session of scheduledSessions) { - if ((0, date_fns_1.isAfter)(new Date(), new Date(session.startAt)) && - !session.activeSessionId) { - console.log("session.buildId", session.buildId); - const { build } = await got_cjs_1.default - .get(`${process.env.CRM_API_URL}/builds/${session.buildId}`) - .json(); - console.log("build", build); - const result = await got_cjs_1.default - .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_cjs_1.default - .put(`${process.env.CRM_API_URL}/scheduled_sessions/${session.id}`, { - json: { - activeSessionId: result.stream, - }, - }) - .json(); - console.log("result2: ", result2); - } - else if ((0, date_fns_1.isAfter)(new Date(), new Date(session.endAt))) { - const result = await got_cjs_1.default - .post(`https://coord.graff.tech/end`, { - json: { activeSessionId: session.activeSessionId }, - }) - .json(); - console.log("CRON End active session: ", result); - } - } - } - catch (error) { - console.log("Error: ", error.message); - } -} -node_cron_1.default.schedule("* * * * *", () => { - checkActiveSessions(); -}); -node_cron_1.default.schedule("* * * * *", () => { - checkScheduledSessions(); -}); diff --git a/dist/models/ActiveSession.js b/dist/models/ActiveSession.js deleted file mode 100644 index f54000e..0000000 --- a/dist/models/ActiveSession.js +++ /dev/null @@ -1,47 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const mongoose_1 = require("mongoose"); -const activeSessionSchema = new mongoose_1.Schema({ - location: { - type: String, - }, - name: { - type: String, - }, - buildName: { - type: String, - }, - type: { - type: String, - }, - uePort: { - type: Number, - }, - cirrusPort: { - type: Number, - }, - ueProcessId: { - type: Number, - }, - cirrusProcessId: { - type: Number, - }, - ownerIp: { - type: String, - }, - connectedPlayersCount: { - type: Number, - }, - endAt: { - type: Date, - }, - localIP: { - type: String, - }, -}, { - timestamps: true, - toJSON: { virtuals: true }, - toObject: { virtuals: true }, -}); -const ActiveSession = (0, mongoose_1.model)("Active_Session", activeSessionSchema); -exports.default = ActiveSession; diff --git a/dist/models/Build.js b/dist/models/Build.js deleted file mode 100644 index 5297a8f..0000000 --- a/dist/models/Build.js +++ /dev/null @@ -1,23 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const mongoose_1 = require("mongoose"); -const buildSchema = new mongoose_1.Schema({ - name: { - type: String, - }, - demoLaunchLimit: { - type: Number, - }, - prodLaunchLimit: { - type: Number, - }, - gpuMemoryUsed: { - type: Number, - }, -}, { - timestamps: true, - toJSON: { virtuals: true }, - toObject: { virtuals: true }, -}); -const Build = (0, mongoose_1.model)("Build", buildSchema); -exports.default = Build; diff --git a/dist/models/SessionServer.js b/dist/models/SessionServer.js deleted file mode 100644 index 5770648..0000000 --- a/dist/models/SessionServer.js +++ /dev/null @@ -1,30 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const mongoose_1 = require("mongoose"); -const sessionServerSchema = new mongoose_1.Schema({ - location: { - type: String, - }, - name: { - type: String, - }, - type: { - type: String, - }, - hostname: { - type: String, - unique: true, - }, - gpuMemoryFree: { - type: Number, - }, - localIP: { - type: String, - }, -}, { - timestamps: true, - toJSON: { virtuals: true }, - toObject: { virtuals: true }, -}); -const SessionServer = (0, mongoose_1.model)("Session_Server", sessionServerSchema); -exports.default = SessionServer; diff --git a/dist/routes/test.js b/dist/routes/test.js deleted file mode 100644 index 8f410d5..0000000 --- a/dist/routes/test.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const express_1 = require("express"); -const router = (0, express_1.Router)(); -router.get("/", async (req, res) => { - res.json({ ok: 1 }); -}); -const testRouter = router; -exports.default = testRouter; diff --git a/src/index.ts b/src/index.ts index aca3fbf..7af6dbe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -205,6 +205,7 @@ async function checkActiveSessions() { for (const activeSession of activeSessions) { if ( + !activeSession.endAt && !activeSession.connectedPlayersCount && differenceInMinutes(new Date(), activeSession.updatedAt) > 2 ) {