diff --git a/dist/index.js b/dist/index.js index d8e33f4..f3de339 100644 --- a/dist/index.js +++ b/dist/index.js @@ -34,9 +34,9 @@ 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 livekit_server_sdk_1 = require("livekit-server-sdk"); 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; @@ -84,7 +84,9 @@ app.get("/start", async (req, res) => { 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 }); + 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 }); @@ -94,8 +96,7 @@ app.get("/start", async (req, res) => { if (gpuMemoryFree < build.gpuMemoryUsed) { continue; } - const sessionServerUrl = `https://${location}.sess.stream.graff.tech/${sessionServer.name}`; - // const sessionServerUrl = "http://localhost:3000"; + const sessionServerUrl = `https://${location}.sess.stream.graff.tech/server/${sessionServer.localIP}:3000`; console.log("endAt", endAt); try { const result = await got_cjs_1.default @@ -108,6 +109,16 @@ app.get("/start", async (req, res) => { }) .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) { @@ -129,31 +140,33 @@ app.post("/end", async (req, res) => { 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/${activeSession.name}/end`, { json: { activeSessionId } }) + .post(`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`, { json: { activeSessionId } }) .json(); res.json(result); }); -const createToken = (roomName, participantName) => { - const at = new livekit_server_sdk_1.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, req.query.participantName); - res.json({ token }); - } - catch (error) { - if (error instanceof Error) { - res.json({ error: error.message }); - } - } -}); +// 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(); @@ -176,7 +189,7 @@ async function checkActiveSessions() { const activeSessionId = activeSession.id; try { const result = await got_cjs_1.default - .post(`https://${activeSession.location}.sess.stream.graff.tech/${activeSession.name}/end`, { json: { activeSessionId } }) + .post(`https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`, { json: { activeSessionId } }) .json(); console.log("Result:", result); } diff --git a/dist/models/ActiveSession.js b/dist/models/ActiveSession.js index 3d6c804..f54000e 100644 --- a/dist/models/ActiveSession.js +++ b/dist/models/ActiveSession.js @@ -35,6 +35,9 @@ const activeSessionSchema = new mongoose_1.Schema({ endAt: { type: Date, }, + localIP: { + type: String, + }, }, { timestamps: true, toJSON: { virtuals: true }, diff --git a/dist/models/SessionServer.js b/dist/models/SessionServer.js index 57978a5..5770648 100644 --- a/dist/models/SessionServer.js +++ b/dist/models/SessionServer.js @@ -18,6 +18,9 @@ const sessionServerSchema = new mongoose_1.Schema({ gpuMemoryFree: { type: Number, }, + localIP: { + type: String, + }, }, { timestamps: true, toJSON: { virtuals: true }, diff --git a/src/index.ts b/src/index.ts index 20d0573..aca3fbf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ 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"; connectDB(); @@ -78,7 +79,9 @@ app.get("/start", async (req, res) => { return res.json({ error: "The prod build launch limit has been reached." }); } - const sessionServers = await SessionServer.find({ location, type }); + const sessionServers = await SessionServer.find({ location, type }).sort({ + gpuMemoryFree: -1, + }); if (sessionServers.length === 0) { console.log("sessionServers.length", sessionServers.length); @@ -92,8 +95,7 @@ app.get("/start", async (req, res) => { continue; } - const sessionServerUrl = `https://${location}.sess.stream.graff.tech/${sessionServer.name}`; - // const sessionServerUrl = "http://localhost:3000"; + const sessionServerUrl = `https://${location}.sess.stream.graff.tech/server/${sessionServer.localIP}:3000`; console.log("endAt", endAt); @@ -110,6 +112,18 @@ app.get("/start", async (req, res) => { console.log("Result: ", result); + console.log("typeof endAt", typeof endAt, endAt); + + if (result.id) { + await LaunchLog.create({ + location, + server: sessionServer.name, + type, + buildName, + ownerIp, + }); + } + return res.json({ stream: result.id }); } catch (error) { if (error instanceof Error) { @@ -136,7 +150,7 @@ app.post("/end", async (req, res) => { const result = await got .post( - `https://${activeSession.location}.sess.stream.graff.tech/${activeSession.name}/end`, + `https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`, { json: { activeSessionId } } ) .json(); @@ -144,32 +158,32 @@ app.post("/end", async (req, res) => { res.json(result); }); -const createToken = (roomName: string, participantName: string) => { - const at = new AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", { - identity: participantName, - }); - at.addGrant({ roomJoin: true, room: roomName }); +// const createToken = (roomName: string, participantName: string) => { +// const at = new AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", { +// identity: participantName, +// }); +// at.addGrant({ roomJoin: true, room: roomName }); - return at.toJwt(); -}; +// 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" }); - } +// 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 }); - } - } -}); +// 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 { @@ -199,7 +213,7 @@ async function checkActiveSessions() { try { const result = await got .post( - `https://${activeSession.location}.sess.stream.graff.tech/${activeSession.name}/end`, + `https://${activeSession.location}.sess.stream.graff.tech/server/${activeSession.localIP}:3000/end`, { json: { activeSessionId } } ) .json(); diff --git a/src/models/ActiveSession.ts b/src/models/ActiveSession.ts index a490712..3657f08 100644 --- a/src/models/ActiveSession.ts +++ b/src/models/ActiveSession.ts @@ -35,6 +35,9 @@ const activeSessionSchema = new Schema( endAt: { type: Date, }, + localIP: { + type: String, + }, }, { timestamps: true, diff --git a/src/models/LaunchLog.ts b/src/models/LaunchLog.ts new file mode 100644 index 0000000..73899a2 --- /dev/null +++ b/src/models/LaunchLog.ts @@ -0,0 +1,35 @@ +import { model, Schema } from "mongoose"; + +const launchLogSchema = new Schema( + { + location: { + type: String, + required: true, + }, + server: { + type: String, + required: true, + }, + type: { + type: String, + required: true, + }, + buildName: { + type: String, + required: true, + }, + ownerIp: { + type: String, + required: true, + }, + }, + { + timestamps: true, + toJSON: { virtuals: true }, + toObject: { virtuals: true }, + } +); + +const LaunchLog = model("Launch_Log", launchLogSchema); + +export default LaunchLog; diff --git a/src/models/SessionServer.ts b/src/models/SessionServer.ts index 4aca4a6..428fc18 100644 --- a/src/models/SessionServer.ts +++ b/src/models/SessionServer.ts @@ -18,6 +18,9 @@ const sessionServerSchema = new Schema( gpuMemoryFree: { type: Number, }, + localIP: { + type: String, + }, }, { timestamps: true,