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
+42 -28
View File
@@ -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();
+3
View File
@@ -35,6 +35,9 @@ const activeSessionSchema = new Schema(
endAt: {
type: Date,
},
localIP: {
type: String,
},
},
{
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: {
type: Number,
},
localIP: {
type: String,
},
},
{
timestamps: true,