first commit

This commit is contained in:
2024-03-05 19:20:35 +05:00
commit 5546ec6137
11 changed files with 2102 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
import { connect } from "mongoose";
async function connectDB() {
try {
await 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);
}
}
export default connectDB;
+95
View File
@@ -0,0 +1,95 @@
import "dotenv/config";
import connectDB from "./config/db";
import express, { json } from "express";
import cors from "cors";
import os from "os";
import util from "util";
import { exec, execFile } from "child_process";
import treeKill from "tree-kill";
import SessionServer from "./models/SessionServer";
import ActiveSession from "./models/ActiveSession";
import got from "got-cjs";
import Build from "./models/Build";
connectDB();
const app = express();
const port = process.env.PORT;
app.use(json());
app.use(cors());
async function getBuild(name: string) {
const build = await Build.findOne({ name });
return build;
}
app.get("/start", async (req, res) => {
const location = req.query.location as string;
const buildName = req.query.build as string;
if (!location || !buildName) {
return res.json({ error: 1 });
}
const build = await getBuild(buildName);
if (!build) {
return res.json({ error: "Build not found" });
}
console.log(build);
const activeSessionsWithBuild = await ActiveSession.find({ buildName });
if (
build.launchLimit &&
activeSessionsWithBuild.length >= build.launchLimit
) {
return res.json({ error: "Build launch limit reached" });
}
const sessionServers = await SessionServer.find({ location, type: "demo" });
if (sessionServers.length === 0) {
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}`;
const sessionServerUrl = "http://localhost:3000";
try {
const result = await got
.post(`${sessionServerUrl}/start`, {
json: {
buildName,
},
})
.json();
console.log("Result: ", result);
return res.json({ result });
} 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.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
+36
View File
@@ -0,0 +1,36 @@
import { model, Schema } from "mongoose";
const activeSessionSchema = new Schema(
{
location: {
type: String,
},
name: {
type: String,
},
buildName: {
type: String,
},
uePort: {
type: Number,
},
cirrusPort: {
type: Number,
},
ueProcessId: {
type: Number,
},
cirrusProcessId: {
type: Number,
},
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
const ActiveSession = model("Active_Session", activeSessionSchema);
export default ActiveSession;
+24
View File
@@ -0,0 +1,24 @@
import { model, Schema } from "mongoose";
const buildSchema = new Schema(
{
name: {
type: String,
},
launchLimit: {
type: Number,
},
gpuMemoryUsed: {
type: Number,
},
},
{
timestamps: true,
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
const Build = model("Build", buildSchema);
export default Build;
+31
View File
@@ -0,0 +1,31 @@
import { model, Schema } from "mongoose";
const sessionServerSchema = new 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 = model("Session_Server", sessionServerSchema);
export default SessionServer;
+11
View File
@@ -0,0 +1,11 @@
import { Router } from "express";
const router = Router();
router.get("/", async (req, res) => {
res.json({ ok: 1 });
});
const testRouter = router;
export default testRouter;