transfer to ecs

This commit is contained in:
Ishaan Dey
2024-05-17 22:23:44 -07:00
parent 509669ea30
commit 80547e9ae1
12 changed files with 1206 additions and 3018 deletions

View File

@ -18,6 +18,7 @@ import {
renameFile,
saveFile,
stopServer,
testDescribe,
} from "./utils";
import { IDisposable, IPty, spawn } from "node-pty";
import {
@ -115,6 +116,10 @@ io.on("connection", async (socket) => {
}
}
console.log("describing service:");
const describeService = await testDescribe();
console.log(describeService);
const sandboxFiles = await getSandboxFiles(data.sandboxId);
sandboxFiles.fileData.forEach((file) => {
const filePath = path.join(dirName, file.id);
@ -290,9 +295,9 @@ io.on("connection", async (socket) => {
}
});
socket.on("renameFolder", async (folderId: string, newName: string) => {
// todo
});
// todo
// socket.on("renameFolder", async (folderId: string, newName: string) => {
// });
socket.on("deleteFolder", async (folderId: string, callback) => {
const files = await getFolder(folderId);
@ -329,7 +334,6 @@ io.on("connection", async (socket) => {
});
const onData = pty.onData((data) => {
// console.log("terminalResponse", id, data)
io.emit("terminalResponse", {
id,
data,
@ -430,7 +434,6 @@ io.on("connection", async (socket) => {
socket.on("disconnect", async () => {
console.log("disconnected", data.userId, data.sandboxId);
if (data.isOwner) {
// console.log("deleting all terminals")
Object.entries(terminals).forEach((t) => {
const { terminal, onData, onExit } = t[1];
onData.dispose();
@ -453,9 +456,8 @@ io.on("connection", async (socket) => {
inactivityTimeout = setTimeout(() => {
io.fetchSockets().then(async (sockets) => {
if (sockets.length === 0) {
// close server
console.log("Closing server due to inactivity.");
const res = await stopServer(data.sandboxId, data.userId);
// const res = await stopServer(data.sandboxId, data.userId);
}
});
}, 20000);

View File

@ -1,4 +1,4 @@
import e from "cors";
import * as dotenv from "dotenv";
import {
R2FileBody,
R2Files,
@ -9,6 +9,33 @@ import {
User,
} from "./types";
import {
DescribeServicesCommand,
ECSClient,
StartTaskCommand,
StopTaskCommand,
} from "@aws-sdk/client-ecs";
dotenv.config();
const client = new ECSClient({
region: "us-east-1",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
});
export const testDescribe = async () => {
const command = new DescribeServicesCommand({
cluster: "Sandbox",
services: ["Sandbox"],
});
const response = await client.send(command);
console.log("describing: ", response);
return response;
};
export const getSandboxFiles = async (id: string) => {
const res = await fetch(
`https://storage.ishaan1013.workers.dev/api?sandboxId=${id}`
@ -151,18 +178,16 @@ export const getProjectSize = async (id: string) => {
return (await res.json()).size;
};
export const stopServer = async (sandboxId: string, userId: string) => {
const res = await fetch("http://localhost:4001/stop", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
sandboxId,
userId,
}),
export const stopServer = async (task: string) => {
const command = new StopTaskCommand({
cluster: "arn:aws:ecs:us-east-1:767398085538:service/Sandbox/Sandbox",
task,
});
const data = await res.json();
return data;
try {
const response = await client.send(command);
console.log("Stopped server:", response);
} catch (error) {
console.error("Error stopping server:", error);
}
};