195 lines
7.2 KiB
JavaScript
195 lines
7.2 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
require("dotenv/config");
|
|
const db_1 = __importDefault(require("./config/db"));
|
|
const express_1 = __importStar(require("express"));
|
|
const cors_1 = __importDefault(require("cors"));
|
|
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");
|
|
(0, db_1.default)();
|
|
const app = (0, express_1.default)();
|
|
const port = process.env.PORT;
|
|
app.use((0, express_1.json)());
|
|
app.use((0, cors_1.default)());
|
|
async function getBuild(name) {
|
|
const build = await Build_1.default.findOne({ name });
|
|
return build;
|
|
}
|
|
app.get("/active_sessions", async (req, res) => {
|
|
const activeSessions = await ActiveSession_1.default.find();
|
|
res.json(activeSessions);
|
|
});
|
|
app.get("/start", async (req, res) => {
|
|
const location = req.query.location;
|
|
const buildName = req.query.build;
|
|
const ownerIp = req.headers["x-real-ip"];
|
|
let type = req.query.type;
|
|
console.log("location", location);
|
|
console.log("buildName", buildName);
|
|
if (!location || !buildName) {
|
|
return res.json({ error: 1 });
|
|
}
|
|
if (type !== "prod") {
|
|
type = "demo";
|
|
}
|
|
console.log("type", type);
|
|
const build = await getBuild(buildName);
|
|
if (!build) {
|
|
return res.json({ error: "Build not found" });
|
|
}
|
|
console.log(build);
|
|
const activeSessionsWithBuild = await ActiveSession_1.default.find({
|
|
location,
|
|
buildName,
|
|
type,
|
|
});
|
|
console.log("type", type);
|
|
console.log("activeSessionsWithBuild", activeSessionsWithBuild);
|
|
if (type === "demo" &&
|
|
build.demoLaunchLimit &&
|
|
activeSessionsWithBuild.length >= build.demoLaunchLimit) {
|
|
return res.json({ error: "The demo build launch limit has been reached." });
|
|
}
|
|
if (type === "prod" &&
|
|
build.prodLaunchLimit &&
|
|
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 });
|
|
if (sessionServers.length === 0) {
|
|
console.log("sessionServers.length", sessionServers.length);
|
|
return res.json({ error: 2 });
|
|
}
|
|
for (const sessionServer of sessionServers) {
|
|
const gpuMemoryFree = sessionServer.gpuMemoryFree;
|
|
if (gpuMemoryFree < build.gpuMemoryUsed) {
|
|
continue;
|
|
}
|
|
const sessionServerUrl = `https://${location}.sess.stream.graff.tech/${sessionServer.name}`;
|
|
// const sessionServerUrl = "http://localhost:3000";
|
|
console.log(`${sessionServerUrl}/start`);
|
|
try {
|
|
const result = await got_cjs_1.default
|
|
.post(`${sessionServerUrl}/start`, {
|
|
json: {
|
|
buildName,
|
|
ownerIp,
|
|
},
|
|
})
|
|
.json();
|
|
console.log("Result: ", result);
|
|
return res.json({ stream: result.id });
|
|
}
|
|
catch (error) {
|
|
if (error instanceof Error) {
|
|
return res.json({ error: error.message });
|
|
}
|
|
}
|
|
}
|
|
res.json({
|
|
error: "There are no servers available to run the build. Please try again later.",
|
|
});
|
|
});
|
|
app.post("/end", async (req, res) => {
|
|
const activeSessionId = req.body.activeSessionId;
|
|
console.log("activeSessionId", activeSessionId);
|
|
const activeSession = await ActiveSession_1.default.findById(activeSessionId);
|
|
console.log("activeSession", activeSession);
|
|
if (!activeSession) {
|
|
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 } })
|
|
.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 });
|
|
}
|
|
}
|
|
});
|
|
app.get("/session_servers", async (req, res) => {
|
|
try {
|
|
const sessionServers = await SessionServer_1.default.find();
|
|
res.json(sessionServers);
|
|
}
|
|
catch (error) {
|
|
if (error instanceof Error) {
|
|
res.json({ error: error.message });
|
|
}
|
|
}
|
|
});
|
|
app.listen(port, () => {
|
|
console.log(`Server is listening on port ${port}`);
|
|
});
|
|
async function checkActiveSessions() {
|
|
const activeSessions = await ActiveSession_1.default.find();
|
|
for (const activeSession of activeSessions) {
|
|
if (!activeSession.connectedPlayersCount &&
|
|
(0, date_fns_1.differenceInMinutes)(new Date(), activeSession.updatedAt) > 2) {
|
|
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 } })
|
|
.json();
|
|
console.log("Result:", result);
|
|
}
|
|
catch (error) {
|
|
if (error instanceof Error) {
|
|
console.log({ error });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
node_cron_1.default.schedule("* * * * *", () => {
|
|
checkActiveSessions();
|
|
});
|