This commit is contained in:
2024-09-26 18:32:14 +05:00
parent 7f9afc592c
commit 6ea74b5ffc
7 changed files with 129 additions and 55 deletions
+40 -27
View File
@@ -34,9 +34,9 @@ const SessionServer_1 = __importDefault(require("./models/SessionServer"));
const ActiveSession_1 = __importDefault(require("./models/ActiveSession")); const ActiveSession_1 = __importDefault(require("./models/ActiveSession"));
const got_cjs_1 = __importDefault(require("got-cjs")); const got_cjs_1 = __importDefault(require("got-cjs"));
const Build_1 = __importDefault(require("./models/Build")); 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 node_cron_1 = __importDefault(require("node-cron"));
const date_fns_1 = require("date-fns"); const date_fns_1 = require("date-fns");
const LaunchLog_1 = __importDefault(require("./models/LaunchLog"));
(0, db_1.default)(); (0, db_1.default)();
const app = (0, express_1.default)(); const app = (0, express_1.default)();
const port = process.env.PORT; const port = process.env.PORT;
@@ -84,7 +84,9 @@ app.get("/start", async (req, res) => {
activeSessionsWithBuild.length >= build.prodLaunchLimit) { activeSessionsWithBuild.length >= build.prodLaunchLimit) {
return res.json({ error: "The prod build launch limit has been reached." }); 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) { if (sessionServers.length === 0) {
console.log("sessionServers.length", sessionServers.length); console.log("sessionServers.length", sessionServers.length);
return res.json({ error: 2 }); return res.json({ error: 2 });
@@ -94,8 +96,7 @@ app.get("/start", async (req, res) => {
if (gpuMemoryFree < build.gpuMemoryUsed) { if (gpuMemoryFree < build.gpuMemoryUsed) {
continue; continue;
} }
const sessionServerUrl = `https://${location}.sess.stream.graff.tech/${sessionServer.name}`; const sessionServerUrl = `https://${location}.sess.stream.graff.tech/server/${sessionServer.localIP}:3000`;
// const sessionServerUrl = "http://localhost:3000";
console.log("endAt", endAt); console.log("endAt", endAt);
try { try {
const result = await got_cjs_1.default const result = await got_cjs_1.default
@@ -108,6 +109,16 @@ app.get("/start", async (req, res) => {
}) })
.json(); .json();
console.log("Result: ", result); 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 }); return res.json({ stream: result.id });
} }
catch (error) { catch (error) {
@@ -129,31 +140,33 @@ app.post("/end", async (req, res) => {
return res.json({ error: "A session with this ID was not found" }); return res.json({ error: "A session with this ID was not found" });
} }
const result = await got_cjs_1.default 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(); .json();
res.json(result); res.json(result);
}); });
const createToken = (roomName, participantName) => { // const createToken = (roomName: string, participantName: string) => {
const at = new livekit_server_sdk_1.AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", { // const at = new AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", {
identity: participantName, // identity: participantName,
}); // });
at.addGrant({ roomJoin: true, room: roomName }); // at.addGrant({ roomJoin: true, room: roomName });
return at.toJwt(); // return at.toJwt();
}; // };
app.get("/getToken", (req, res) => { // app.get("/getToken", (req, res) => {
if (!req.query.roomName && !req.query.participantName) { // if (!req.query.roomName && !req.query.participantName) {
return res.json({ error: "roomName or participantName is not defined" }); // return res.json({ error: "roomName or participantName is not defined" });
} // }
try { // try {
const token = createToken(req.query.roomName, req.query.participantName); // const token = createToken(
res.json({ token }); // req.query.roomName as string,
} // req.query.participantName as string
catch (error) { // );
if (error instanceof Error) { // res.json({ token });
res.json({ error: error.message }); // } catch (error) {
} // if (error instanceof Error) {
} // res.json({ error: error.message });
}); // }
// }
// });
app.get("/session_servers", async (req, res) => { app.get("/session_servers", async (req, res) => {
try { try {
const sessionServers = await SessionServer_1.default.find(); const sessionServers = await SessionServer_1.default.find();
@@ -176,7 +189,7 @@ async function checkActiveSessions() {
const activeSessionId = activeSession.id; const activeSessionId = activeSession.id;
try { try {
const result = await got_cjs_1.default 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(); .json();
console.log("Result:", result); console.log("Result:", result);
} }
+3
View File
@@ -35,6 +35,9 @@ const activeSessionSchema = new mongoose_1.Schema({
endAt: { endAt: {
type: Date, type: Date,
}, },
localIP: {
type: String,
},
}, { }, {
timestamps: true, timestamps: true,
toJSON: { virtuals: true }, toJSON: { virtuals: true },
+3
View File
@@ -18,6 +18,9 @@ const sessionServerSchema = new mongoose_1.Schema({
gpuMemoryFree: { gpuMemoryFree: {
type: Number, type: Number,
}, },
localIP: {
type: String,
},
}, { }, {
timestamps: true, timestamps: true,
toJSON: { virtuals: true }, toJSON: { virtuals: true },
+42 -28
View File
@@ -9,6 +9,7 @@ import Build from "./models/Build";
import { AccessToken } from "livekit-server-sdk"; import { AccessToken } from "livekit-server-sdk";
import cron from "node-cron"; import cron from "node-cron";
import { differenceInMinutes, isAfter, parseISO } from "date-fns"; import { differenceInMinutes, isAfter, parseISO } from "date-fns";
import LaunchLog from "./models/LaunchLog";
connectDB(); connectDB();
@@ -78,7 +79,9 @@ app.get("/start", async (req, res) => {
return res.json({ error: "The prod build launch limit has been reached." }); 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) { if (sessionServers.length === 0) {
console.log("sessionServers.length", sessionServers.length); console.log("sessionServers.length", sessionServers.length);
@@ -92,8 +95,7 @@ app.get("/start", async (req, res) => {
continue; continue;
} }
const sessionServerUrl = `https://${location}.sess.stream.graff.tech/${sessionServer.name}`; const sessionServerUrl = `https://${location}.sess.stream.graff.tech/server/${sessionServer.localIP}:3000`;
// const sessionServerUrl = "http://localhost:3000";
console.log("endAt", endAt); console.log("endAt", endAt);
@@ -110,6 +112,18 @@ app.get("/start", async (req, res) => {
console.log("Result: ", result); 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 }); return res.json({ stream: result.id });
} catch (error) { } catch (error) {
if (error instanceof Error) { if (error instanceof Error) {
@@ -136,7 +150,7 @@ app.post("/end", async (req, res) => {
const result = await got const result = await got
.post( .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: { activeSessionId } }
) )
.json(); .json();
@@ -144,32 +158,32 @@ app.post("/end", async (req, res) => {
res.json(result); res.json(result);
}); });
const createToken = (roomName: string, participantName: string) => { // const createToken = (roomName: string, participantName: string) => {
const at = new AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", { // const at = new AccessToken("prodkey", "ZUwD12h0hsBfhgnYadHyHENaBGlFSVZJ", {
identity: participantName, // identity: participantName,
}); // });
at.addGrant({ roomJoin: true, room: roomName }); // at.addGrant({ roomJoin: true, room: roomName });
return at.toJwt(); // return at.toJwt();
}; // };
app.get("/getToken", (req, res) => { // app.get("/getToken", (req, res) => {
if (!req.query.roomName && !req.query.participantName) { // if (!req.query.roomName && !req.query.participantName) {
return res.json({ error: "roomName or participantName is not defined" }); // return res.json({ error: "roomName or participantName is not defined" });
} // }
try { // try {
const token = createToken( // const token = createToken(
req.query.roomName as string, // req.query.roomName as string,
req.query.participantName as string // req.query.participantName as string
); // );
res.json({ token }); // res.json({ token });
} catch (error) { // } catch (error) {
if (error instanceof Error) { // if (error instanceof Error) {
res.json({ error: error.message }); // res.json({ error: error.message });
} // }
} // }
}); // });
app.get("/session_servers", async (req, res) => { app.get("/session_servers", async (req, res) => {
try { try {
@@ -199,7 +213,7 @@ async function checkActiveSessions() {
try { try {
const result = await got const result = await got
.post( .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: { activeSessionId } }
) )
.json(); .json();
+3
View File
@@ -35,6 +35,9 @@ const activeSessionSchema = new Schema(
endAt: { endAt: {
type: Date, type: Date,
}, },
localIP: {
type: String,
},
}, },
{ {
timestamps: true, timestamps: true,
+35
View File
@@ -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;
+3
View File
@@ -18,6 +18,9 @@ const sessionServerSchema = new Schema(
gpuMemoryFree: { gpuMemoryFree: {
type: Number, type: Number,
}, },
localIP: {
type: String,
},
}, },
{ {
timestamps: true, timestamps: true,