upd
This commit is contained in:
Vendored
+16
@@ -0,0 +1,16 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const mongoose_1 = require("mongoose");
|
||||
async function connectDB() {
|
||||
try {
|
||||
await (0, mongoose_1.connect)(process.env.MONGO_URI, { dbName: "pixel_streaming2" });
|
||||
console.log("MongoDB connected...");
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
console.error(error.message);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
exports.default = connectDB;
|
||||
Vendored
+194
@@ -0,0 +1,194 @@
|
||||
"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();
|
||||
});
|
||||
Vendored
+41
@@ -0,0 +1,41 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const mongoose_1 = require("mongoose");
|
||||
const activeSessionSchema = new mongoose_1.Schema({
|
||||
location: {
|
||||
type: String,
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
buildName: {
|
||||
type: String,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
},
|
||||
uePort: {
|
||||
type: Number,
|
||||
},
|
||||
cirrusPort: {
|
||||
type: Number,
|
||||
},
|
||||
ueProcessId: {
|
||||
type: Number,
|
||||
},
|
||||
cirrusProcessId: {
|
||||
type: Number,
|
||||
},
|
||||
ownerIp: {
|
||||
type: String,
|
||||
},
|
||||
connectedPlayersCount: {
|
||||
type: Number,
|
||||
},
|
||||
}, {
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true },
|
||||
toObject: { virtuals: true },
|
||||
});
|
||||
const ActiveSession = (0, mongoose_1.model)("Active_Session", activeSessionSchema);
|
||||
exports.default = ActiveSession;
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const mongoose_1 = require("mongoose");
|
||||
const buildSchema = new mongoose_1.Schema({
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
demoLaunchLimit: {
|
||||
type: Number,
|
||||
},
|
||||
prodLaunchLimit: {
|
||||
type: Number,
|
||||
},
|
||||
gpuMemoryUsed: {
|
||||
type: Number,
|
||||
},
|
||||
}, {
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true },
|
||||
toObject: { virtuals: true },
|
||||
});
|
||||
const Build = (0, mongoose_1.model)("Build", buildSchema);
|
||||
exports.default = Build;
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const mongoose_1 = require("mongoose");
|
||||
const sessionServerSchema = new mongoose_1.Schema({
|
||||
location: {
|
||||
type: String,
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
},
|
||||
hostname: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
gpuMemoryFree: {
|
||||
type: Number,
|
||||
},
|
||||
}, {
|
||||
timestamps: true,
|
||||
toJSON: { virtuals: true },
|
||||
toObject: { virtuals: true },
|
||||
});
|
||||
const SessionServer = (0, mongoose_1.model)("Session_Server", sessionServerSchema);
|
||||
exports.default = SessionServer;
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const express_1 = require("express");
|
||||
const router = (0, express_1.Router)();
|
||||
router.get("/", async (req, res) => {
|
||||
res.json({ ok: 1 });
|
||||
});
|
||||
const testRouter = router;
|
||||
exports.default = testRouter;
|
||||
Reference in New Issue
Block a user