import { execAsync, execAsyncRemote, } from "@dokploy/server/utils/process/execAsync"; import { Queue } from "bullmq"; import { redisConfig } from "./redis-connection"; const myQueue = new Queue("deployments", { connection: redisConfig, }); export const getJobsByApplicationId = async (applicationId: string) => { const jobs = await myQueue.getJobs(); return jobs.filter((job) => job?.data?.applicationId === applicationId); }; export const getJobsByComposeId = async (composeId: string) => { const jobs = await myQueue.getJobs(); return jobs.filter((job) => job?.data?.composeId === composeId); }; process.on("SIGTERM", () => { myQueue.close(); process.exit(0); }); myQueue.on("error", (error) => { if ((error as any).code === "ECONNREFUSED") { console.error( "Make sure you have installed Redis and it is running.", error, ); } }); export const cleanQueuesByApplication = async (applicationId: string) => { const jobs = await myQueue.getJobs(["waiting", "delayed"]); for (const job of jobs) { if (job?.data?.applicationId === applicationId) { await job.remove(); console.log(`Removed job ${job.id} for application ${applicationId}`); } } }; export const cleanQueuesByCompose = async (composeId: string) => { const jobs = await myQueue.getJobs(["waiting", "delayed"]); for (const job of jobs) { if (job?.data?.composeId === composeId) { await job.remove(); console.log(`Removed job ${job.id} for compose ${composeId}`); } } }; export const killDockerBuild = async ( type: "application" | "compose", serverId: string | null, ) => { try { if (type === "application") { const command = `pkill -2 -f "docker build"`; if (serverId) { await execAsyncRemote(serverId, command); } else { await execAsync(command); } } else if (type === "compose") { const command = `pkill -2 -f "docker compose"`; if (serverId) { await execAsyncRemote(serverId, command); } else { await execAsync(command); } } } catch (error) { console.error(error); } }; export { myQueue };