feat: add redis installation on server and deploy applications on servers

This commit is contained in:
Mauricio Siu
2024-09-08 22:10:24 -06:00
parent 1a34ba175e
commit 3d60236b36
39 changed files with 8616 additions and 155 deletions
+46 -46
View File
@@ -29,52 +29,52 @@ type DeployJob =
export type DeploymentJob = DeployJob;
export const deploymentWorker = new Worker(
"deployments",
async (job: Job<DeploymentJob>) => {
try {
if (job.data.applicationType === "application") {
await updateApplicationStatus(job.data.applicationId, "running");
if (job.data.type === "redeploy") {
await rebuildApplication({
applicationId: job.data.applicationId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
} else if (job.data.type === "deploy") {
await deployApplication({
applicationId: job.data.applicationId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
}
} else if (job.data.applicationType === "compose") {
await updateCompose(job.data.composeId, {
composeStatus: "running",
});
if (job.data.type === "deploy") {
await deployCompose({
composeId: job.data.composeId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
} else if (job.data.type === "redeploy") {
await rebuildCompose({
composeId: job.data.composeId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
}
}
} catch (error) {
console.log("Error", error);
}
},
{
autorun: false,
connection: redisConfig,
},
);
// export const deploymentWorker = new Worker(
// "deployments",
// async (job: Job<DeploymentJob>) => {
// try {
// if (job.data.applicationType === "application") {
// await updateApplicationStatus(job.data.applicationId, "running");
// if (job.data.type === "redeploy") {
// await rebuildApplication({
// applicationId: job.data.applicationId,
// titleLog: job.data.titleLog,
// descriptionLog: job.data.descriptionLog,
// });
// } else if (job.data.type === "deploy") {
// await deployApplication({
// applicationId: job.data.applicationId,
// titleLog: job.data.titleLog,
// descriptionLog: job.data.descriptionLog,
// });
// }
// } else if (job.data.applicationType === "compose") {
// await updateCompose(job.data.composeId, {
// composeStatus: "running",
// });
// if (job.data.type === "deploy") {
// await deployCompose({
// composeId: job.data.composeId,
// titleLog: job.data.titleLog,
// descriptionLog: job.data.descriptionLog,
// });
// } else if (job.data.type === "redeploy") {
// await rebuildCompose({
// composeId: job.data.composeId,
// titleLog: job.data.titleLog,
// descriptionLog: job.data.descriptionLog,
// });
// }
// }
// } catch (error) {
// console.log("Error", error);
// }
// },
// {
// autorun: false,
// connection: redisConfig,
// },
// );
export const cleanQueuesByApplication = async (applicationId: string) => {
const jobs = await myQueue.getJobs(["waiting", "delayed"]);
+70 -1
View File
@@ -1,4 +1,10 @@
import { type ConnectionOptions, Queue } from "bullmq";
import { type ConnectionOptions, type Job, Queue, Worker } from "bullmq";
import { findServerById, type Server } from "../api/services/server";
import type { DeploymentJob } from "./deployments-queue";
import {
deployApplication,
updateApplicationStatus,
} from "../api/services/application";
export const redisConfig: ConnectionOptions = {
host: "31.220.108.27",
@@ -26,3 +32,66 @@ myQueue.on("error", (error) => {
});
export { myQueue };
const workersMap = new Map<string, Worker>();
const queuesMap = new Map<string, Queue>();
function createRedisConnection(server: Server) {
return {
host: server.ipAddress,
port: "6379",
} as ConnectionOptions;
}
async function setupServerQueueAndWorker(server: Server) {
const connection = createRedisConnection(server);
if (!workersMap.has(server.serverId)) {
const queue = new Queue(`deployments-${server.serverId}`, {
connection,
});
const worker = new Worker(
`deployments-${server.serverId}`,
async (job: Job<DeploymentJob>) => {
// Ejecuta el trabajo de despliegue
if (job.data.applicationType === "application") {
await updateApplicationStatus(job.data.applicationId, "running");
if (job.data.type === "deploy") {
await deployApplication({
applicationId: job.data.applicationId,
titleLog: job.data.titleLog,
descriptionLog: job.data.descriptionLog,
});
}
}
},
{
limiter: {
max: 1,
duration: 1000,
},
connection,
},
);
// Almacena worker y queue para reutilizar
workersMap.set(server.serverId, worker);
queuesMap.set(server.serverId, queue);
}
return {
queue: queuesMap.get(server.serverId),
worker: workersMap.get(server.serverId),
};
}
export async function enqueueDeploymentJob(
serverId: string,
jobData: DeploymentJob,
) {
const server = await findServerById(serverId);
const { queue } = await setupServerQueueAndWorker(server);
await queue?.add(`deployments-${serverId}`, jobData, {
removeOnComplete: true,
removeOnFail: true,
});
}