re-organize files + setup more orchestration logic

This commit is contained in:
Ishaan Dey
2024-05-12 02:10:31 -07:00
parent e699028096
commit 18aca540cc
9 changed files with 848 additions and 780 deletions

View File

@ -1,10 +0,0 @@
import { Server } from "socket.io";
import { DefaultEventsMap } from "socket.io/dist/typed-events";
export function checkForInactivity(io: Server<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>) {
io.fetchSockets().then(sockets => {
if (sockets.length === 0) {
console.log("No users have been connected for 15 seconds.");
}
});
}

View File

@ -18,6 +18,7 @@ import {
getSandboxFiles,
renameFile,
saveFile,
stopServer,
} from "./utils"
import { IDisposable, IPty, spawn } from "node-pty"
import {
@ -437,10 +438,12 @@ io.on("connection", async (socket) => {
inactivityTimeout = setTimeout(() => {
io.fetchSockets().then(sockets => {
if (sockets.length === 0) {
console.log("No users have been connected for 15 seconds.");
// close server
console.log("Closing server due to inactivity.");
stopServer(data.sandboxId, data.userId)
}
});
}, 15000);
}, 60000);
}
})

View File

@ -196,3 +196,16 @@ ${code}`,
}
)
}
export const stopServer = async (sandboxId: string, userId: string) => {
await fetch("http://localhost:4001/stop", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sandboxId,
userId
}),
})
}