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 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);
}