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