add worker service binding + inactivity detection
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
ARG cf_api_token
|
||||
ARG cf_user_id
|
||||
ARG CF_API_TOKEN
|
||||
ARG CF_USER_ID
|
||||
|
||||
FROM node:20
|
||||
|
||||
@ -15,7 +15,7 @@ RUN npm run build
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
ENV cf_api_token=$cf_api_token
|
||||
ENV cf_user_id=$cf_user_id
|
||||
ENV CF_API_TOKEN=$CF_API_TOKEN
|
||||
ENV CF_USER_ID=$CF_USER_ID
|
||||
|
||||
CMD [ "node", "dist/index.js" ]
|
10
backend/server/src/inactivity.ts
Normal file
10
backend/server/src/inactivity.ts
Normal file
@ -0,0 +1,10 @@
|
||||
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.");
|
||||
}
|
||||
});
|
||||
}
|
@ -38,6 +38,8 @@ const io = new Server(httpServer, {
|
||||
},
|
||||
})
|
||||
|
||||
let inactivityTimeout: NodeJS.Timeout | null = null;
|
||||
|
||||
const terminals: {
|
||||
[id: string]: { terminal: IPty; onData: IDisposable; onExit: IDisposable }
|
||||
} = {}
|
||||
@ -85,15 +87,20 @@ io.use(async (socket, next) => {
|
||||
socket.data = {
|
||||
userId,
|
||||
sandboxId: sandboxId,
|
||||
isOwner: sandbox !== undefined,
|
||||
}
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
|
||||
if (inactivityTimeout) clearTimeout(inactivityTimeout);
|
||||
|
||||
const data = socket.data as {
|
||||
userId: string
|
||||
sandboxId: string
|
||||
isOwner: boolean
|
||||
}
|
||||
|
||||
const sandboxFiles = await getSandboxFiles(data.sandboxId)
|
||||
@ -298,14 +305,37 @@ io.on("connection", async (socket) => {
|
||||
}
|
||||
)
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
Object.entries(terminals).forEach((t) => {
|
||||
socket.on("disconnect", async () => {
|
||||
if (data.isOwner) {
|
||||
Object.entries(terminals).forEach((t) => {
|
||||
const { terminal, onData, onExit } = t[1]
|
||||
if (os.platform() !== "win32") terminal.kill()
|
||||
onData.dispose()
|
||||
onExit.dispose()
|
||||
delete terminals[t[0]]
|
||||
})
|
||||
onData.dispose()
|
||||
onExit.dispose()
|
||||
delete terminals[t[0]]
|
||||
})
|
||||
|
||||
console.log("The owner disconnected.")
|
||||
socket.broadcast.emit("ownerDisconnected")
|
||||
}
|
||||
else {
|
||||
console.log("A shared user disconnected.")
|
||||
}
|
||||
|
||||
const sockets = await io.fetchSockets()
|
||||
if (inactivityTimeout) {
|
||||
clearTimeout(inactivityTimeout)
|
||||
};
|
||||
if (sockets.length === 0) {
|
||||
inactivityTimeout = setTimeout(() => {
|
||||
io.fetchSockets().then(sockets => {
|
||||
if (sockets.length === 0) {
|
||||
console.log("No users have been connected for 15 seconds.");
|
||||
}
|
||||
});
|
||||
}, 15000);
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user