services logic
This commit is contained in:
@ -1,8 +1,15 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import ecsClient from "./ecs";
|
||||
import { CreateServiceCommand, StartTaskCommand } from "@aws-sdk/client-ecs";
|
||||
import ecsClient, { ec2Client } from "./ecs";
|
||||
import {
|
||||
CreateServiceCommand,
|
||||
DescribeClustersCommand,
|
||||
DescribeServicesCommand,
|
||||
DescribeTasksCommand,
|
||||
ListTasksCommand,
|
||||
} from "@aws-sdk/client-ecs";
|
||||
import { DescribeNetworkInterfacesCommand } from "@aws-sdk/client-ec2";
|
||||
|
||||
export async function createSandbox(body: {
|
||||
type: string;
|
||||
@ -81,12 +88,105 @@ export async function unshareSandbox(sandboxId: string, userId: string) {
|
||||
revalidatePath(`/code/${sandboxId}`);
|
||||
}
|
||||
|
||||
export async function startServer(serviceName: string) {
|
||||
export async function describeService(serviceName: string) {
|
||||
const command = new DescribeServicesCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
services: [serviceName],
|
||||
});
|
||||
|
||||
return await ecsClient.send(command);
|
||||
}
|
||||
|
||||
export async function getTaskIp(serviceName: string) {
|
||||
const listCommand = new ListTasksCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
serviceName,
|
||||
});
|
||||
|
||||
const listResponse = await ecsClient.send(listCommand);
|
||||
const taskArns = listResponse.taskArns;
|
||||
|
||||
const describeCommand = new DescribeTasksCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
tasks: taskArns,
|
||||
});
|
||||
|
||||
const describeResponse = await ecsClient.send(describeCommand);
|
||||
const tasks = describeResponse.tasks;
|
||||
const taskAttachment = tasks?.[0].attachments?.[0].details;
|
||||
if (!taskAttachment) {
|
||||
throw new Error("Task attachment not found");
|
||||
}
|
||||
|
||||
const eni = taskAttachment.find(
|
||||
(detail) => detail.name === "networkInterfaceId"
|
||||
)?.value;
|
||||
if (!eni) {
|
||||
throw new Error("Network interface not found");
|
||||
}
|
||||
|
||||
const describeNetworkInterfacesCommand = new DescribeNetworkInterfacesCommand(
|
||||
{
|
||||
NetworkInterfaceIds: [eni],
|
||||
}
|
||||
);
|
||||
const describeNetworkInterfacesResponse = await ec2Client.send(
|
||||
describeNetworkInterfacesCommand
|
||||
);
|
||||
|
||||
const ip =
|
||||
describeNetworkInterfacesResponse.NetworkInterfaces?.[0].Association
|
||||
?.PublicIp;
|
||||
if (!ip) {
|
||||
throw new Error("Public IP not found");
|
||||
}
|
||||
|
||||
return ip;
|
||||
}
|
||||
|
||||
async function doesServiceExist(serviceName: string) {
|
||||
const response = await describeService(serviceName);
|
||||
const activeServices = response.services?.filter(
|
||||
(service) => service.status === "ACTIVE"
|
||||
);
|
||||
|
||||
console.log("activeServices: ", activeServices);
|
||||
|
||||
return activeServices?.length === 1;
|
||||
}
|
||||
|
||||
async function countServices() {
|
||||
const command = new DescribeClustersCommand({
|
||||
clusters: [process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!],
|
||||
});
|
||||
|
||||
const response = await ecsClient.send(command);
|
||||
return response.clusters?.[0].activeServicesCount!;
|
||||
}
|
||||
|
||||
export async function startServer(
|
||||
serviceName: string
|
||||
): Promise<{ success: boolean; message: string }> {
|
||||
const serviceExists = await doesServiceExist(serviceName);
|
||||
if (serviceExists) {
|
||||
return { success: true, message: "" };
|
||||
}
|
||||
|
||||
const activeServices = await countServices();
|
||||
if (activeServices >= 100) {
|
||||
return {
|
||||
success: false,
|
||||
message:
|
||||
"Too many servers are running! Please try again later or contact @ishaandey_ on Twitter/X.",
|
||||
};
|
||||
}
|
||||
|
||||
const command = new CreateServiceCommand({
|
||||
cluster: process.env.AWS_ECS_CLUSTER!,
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER!,
|
||||
serviceName,
|
||||
taskDefinition: "Sandbox1",
|
||||
desiredCount: 1,
|
||||
launchType: "FARGATE",
|
||||
networkConfiguration: {
|
||||
awsvpcConfiguration: {
|
||||
securityGroups: [process.env.AWS_ECS_SECURITY_GROUP!],
|
||||
@ -107,6 +207,8 @@ export async function startServer(serviceName: string) {
|
||||
const response = await ecsClient.send(command);
|
||||
console.log("started server:", response.service?.serviceName);
|
||||
|
||||
return { success: true, message: "" };
|
||||
|
||||
// store in workers kv:
|
||||
// {
|
||||
// userId: {
|
||||
@ -116,7 +218,11 @@ export async function startServer(serviceName: string) {
|
||||
|
||||
// }
|
||||
// }
|
||||
} catch (error) {
|
||||
console.error("Error starting server:", error);
|
||||
} catch (error: any) {
|
||||
// console.error("Error starting server:", error.message);
|
||||
return {
|
||||
success: false,
|
||||
message: `Error starting server: ${error.message}. Try again in a minute, or contact @ishaandey_ on Twitter/X if it still doesn't work.`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { ECSClient } from "@aws-sdk/client-ecs";
|
||||
import { EC2Client } from "@aws-sdk/client-ec2";
|
||||
|
||||
const ecsClient = new ECSClient({
|
||||
region: "us-east-1",
|
||||
@ -8,4 +9,12 @@ const ecsClient = new ECSClient({
|
||||
},
|
||||
});
|
||||
|
||||
export const ec2Client = new EC2Client({
|
||||
region: "us-east-1",
|
||||
credentials: {
|
||||
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
|
||||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
|
||||
},
|
||||
});
|
||||
|
||||
export default ecsClient;
|
||||
|
@ -2,8 +2,8 @@ import { type ClassValue, clsx } from "clsx";
|
||||
// import { toast } from "sonner"
|
||||
import { twMerge } from "tailwind-merge";
|
||||
import { Sandbox, TFile, TFolder } from "./types";
|
||||
import ecsClient from "./ecs";
|
||||
import { DescribeServicesCommand } from "@aws-sdk/client-ecs";
|
||||
import { Service } from "@aws-sdk/client-ecs";
|
||||
import { describeService } from "./actions";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
@ -94,34 +94,44 @@ export function addNew(
|
||||
// }
|
||||
// }
|
||||
|
||||
const checkServiceStatus = (serviceName: string) => {
|
||||
export function checkServiceStatus(serviceName: string): Promise<Service> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const command = new DescribeServicesCommand({
|
||||
cluster: process.env.NEXT_PUBLIC_AWS_ECS_CLUSTER,
|
||||
services: [serviceName],
|
||||
});
|
||||
let tries = 0;
|
||||
|
||||
const interval = setInterval(async () => {
|
||||
try {
|
||||
const response = await ecsClient.send(command);
|
||||
console.log("Checking service status", response);
|
||||
tries++;
|
||||
|
||||
if (response.services && response.services.length > 0) {
|
||||
const service = response.services?.[0];
|
||||
if (tries > 40) {
|
||||
clearInterval(interval);
|
||||
reject(new Error("Timed out."));
|
||||
}
|
||||
|
||||
const response = await describeService(serviceName);
|
||||
const activeServices = response.services?.filter(
|
||||
(service) => service.status === "ACTIVE"
|
||||
);
|
||||
console.log("Checking activeServices status", activeServices);
|
||||
|
||||
if (activeServices?.length === 1) {
|
||||
const service = activeServices?.[0];
|
||||
if (
|
||||
service.runningCount === service.desiredCount &&
|
||||
service.deployments &&
|
||||
service.deployments.length === 1 &&
|
||||
service.deployments[0].rolloutState === "COMPLETED"
|
||||
service.deployments?.length === 1
|
||||
) {
|
||||
clearInterval(interval);
|
||||
resolve(service);
|
||||
if (service.deployments[0].rolloutState === "COMPLETED") {
|
||||
clearInterval(interval);
|
||||
resolve(service);
|
||||
} else if (service.deployments[0].rolloutState === "FAILED") {
|
||||
clearInterval(interval);
|
||||
reject(new Error("Deployment failed."));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
clearInterval(interval);
|
||||
reject(error);
|
||||
}
|
||||
}, 5000);
|
||||
}, 3000);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user