diff --git a/Dockerfile b/Dockerfile index 239399c98..778d46764 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ ENV PRISMA_QUERY_ENGINE_BINARY=/app/prisma-engines/query-engine \ COPY --from=coollabsio/prisma-engine:3.15 /prisma-engines/query-engine /prisma-engines/migration-engine /prisma-engines/introspection-engine /prisma-engines/prisma-fmt /app/prisma-engines/ -RUN apk add --no-cache git git-lfs openssh-client curl jq cmake sqlite openssl +RUN apk add --no-cache git git-lfs openssh-client curl jq cmake sqlite openssl psmisc RUN curl -sL https://unpkg.com/@pnpm/self-installer | node RUN mkdir -p ~/.docker/cli-plugins/ diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 4b71f70d8..ed862a59e 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -104,6 +104,7 @@ fastify.listen({ port, host }, async (err: any, address: any) => { await initServer(); await scheduler.start('deployApplication'); await scheduler.start('cleanupStorage'); + await scheduler.start('cleanupPrismaEngines'); await scheduler.start('checkProxies'); // Check if no build is running @@ -116,14 +117,14 @@ fastify.listen({ port, host }, async (err: any, address: any) => { scheduler.workers.get('deployApplication').postMessage("status:autoUpdater"); } } - }, 60000 * 15) + }, isDev ? 5000 : 60000 * 15) // Cleanup storage setInterval(async () => { if (scheduler.workers.has('deployApplication')) { scheduler.workers.get('deployApplication').postMessage("status:cleanupStorage"); } - }, 60000 * 10) + }, isDev ? 5000 : 60000 * 10) scheduler.on('worker deleted', async (name) => { if (name === 'autoUpdater' || name === 'cleanupStorage') { diff --git a/apps/api/src/jobs/checkProxies.ts b/apps/api/src/jobs/checkProxies.ts index 761554061..e307ef858 100644 --- a/apps/api/src/jobs/checkProxies.ts +++ b/apps/api/src/jobs/checkProxies.ts @@ -4,87 +4,92 @@ import { checkContainer } from '../lib/docker'; (async () => { if (parentPort) { - // Coolify Proxy local - const engine = '/var/run/docker.sock'; - const localDocker = await prisma.destinationDocker.findFirst({ - where: { engine, network: 'coolify' } - }); - if (localDocker && localDocker.isCoolifyProxyUsed) { - // Remove HAProxy - const found = await checkContainer({ dockerId: localDocker.id, container: 'coolify-haproxy' }); - if (found) { - await executeDockerCmd({ - dockerId: localDocker.id, - command: `docker stop -t 0 coolify-haproxy && docker rm coolify-haproxy` - }) - } - await startTraefikProxy(localDocker.id); - - } - - // TCP Proxies - const databasesWithPublicPort = await prisma.database.findMany({ - where: { publicPort: { not: null } }, - include: { settings: true, destinationDocker: true } - }); - for (const database of databasesWithPublicPort) { - const { destinationDockerId, destinationDocker, publicPort, id } = database; - if (destinationDockerId && destinationDocker.isCoolifyProxyUsed) { - const { privatePort } = generateDatabaseConfiguration(database); + try { + // Coolify Proxy local + const engine = '/var/run/docker.sock'; + const localDocker = await prisma.destinationDocker.findFirst({ + where: { engine, network: 'coolify' } + }); + if (localDocker && localDocker.isCoolifyProxyUsed) { // Remove HAProxy - const found = await checkContainer({ - dockerId: localDocker.id, container: `haproxy-for-${publicPort}` - }); + const found = await checkContainer({ dockerId: localDocker.id, container: 'coolify-haproxy' }); if (found) { await executeDockerCmd({ dockerId: localDocker.id, - command: `docker stop -t 0 haproxy-for-${publicPort} && docker rm haproxy-for-${publicPort}` + command: `docker stop -t 0 coolify-haproxy && docker rm coolify-haproxy` }) } - await startTraefikTCPProxy(destinationDocker, id, publicPort, privatePort); + await startTraefikProxy(localDocker.id); + } - } - } - const wordpressWithFtp = await prisma.wordpress.findMany({ - where: { ftpPublicPort: { not: null } }, - include: { service: { include: { destinationDocker: true } } } - }); - for (const ftp of wordpressWithFtp) { - const { service, ftpPublicPort } = ftp; - const { destinationDockerId, destinationDocker, id } = service; - if (destinationDockerId && destinationDocker.isCoolifyProxyUsed) { - // Remove HAProxy - const found = await checkContainer({ dockerId: localDocker.id, container: `haproxy-for-${ftpPublicPort}` }); - if (found) { - await executeDockerCmd({ - dockerId: localDocker.id, - command: `docker stop -t 0 haproxy -for-${ftpPublicPort} && docker rm haproxy-for-${ftpPublicPort}` - }) + // TCP Proxies + const databasesWithPublicPort = await prisma.database.findMany({ + where: { publicPort: { not: null } }, + include: { settings: true, destinationDocker: true } + }); + for (const database of databasesWithPublicPort) { + const { destinationDockerId, destinationDocker, publicPort, id } = database; + if (destinationDockerId && destinationDocker.isCoolifyProxyUsed) { + const { privatePort } = generateDatabaseConfiguration(database); + // Remove HAProxy + const found = await checkContainer({ + dockerId: localDocker.id, container: `haproxy-for-${publicPort}` + }); + if (found) { + await executeDockerCmd({ + dockerId: localDocker.id, + command: `docker stop -t 0 haproxy-for-${publicPort} && docker rm haproxy-for-${publicPort}` + }) + } + await startTraefikTCPProxy(destinationDocker, id, publicPort, privatePort); } - await startTraefikTCPProxy(destinationDocker, id, ftpPublicPort, 22, 'wordpressftp'); } + const wordpressWithFtp = await prisma.wordpress.findMany({ + where: { ftpPublicPort: { not: null } }, + include: { service: { include: { destinationDocker: true } } } + }); + for (const ftp of wordpressWithFtp) { + const { service, ftpPublicPort } = ftp; + const { destinationDockerId, destinationDocker, id } = service; + if (destinationDockerId && destinationDocker.isCoolifyProxyUsed) { + // Remove HAProxy + const found = await checkContainer({ dockerId: localDocker.id, container: `haproxy-for-${ftpPublicPort}` }); + if (found) { + await executeDockerCmd({ + dockerId: localDocker.id, + command: `docker stop -t 0 haproxy -for-${ftpPublicPort} && docker rm haproxy-for-${ftpPublicPort}` + }) + } + await startTraefikTCPProxy(destinationDocker, id, ftpPublicPort, 22, 'wordpressftp'); + } + } + + // HTTP Proxies + const minioInstances = await prisma.minio.findMany({ + where: { publicPort: { not: null } }, + include: { service: { include: { destinationDocker: true } } } + }); + for (const minio of minioInstances) { + const { service, publicPort } = minio; + const { destinationDockerId, destinationDocker, id } = service; + if (destinationDockerId && destinationDocker.isCoolifyProxyUsed) { + // Remove HAProxy + const found = await checkContainer({ dockerId: localDocker.id, container: `${id}-${publicPort}` }); + if (found) { + await executeDockerCmd({ + dockerId: localDocker.id, + command: `docker stop -t 0 ${id}-${publicPort} && docker rm ${id}-${publicPort} ` + }) + } + await startTraefikTCPProxy(destinationDocker, id, publicPort, 9000); + } + } + + } catch (error) { + + } finally { + await prisma.$disconnect(); } - // HTTP Proxies - const minioInstances = await prisma.minio.findMany({ - where: { publicPort: { not: null } }, - include: { service: { include: { destinationDocker: true } } } - }); - for (const minio of minioInstances) { - const { service, publicPort } = minio; - const { destinationDockerId, destinationDocker, id } = service; - if (destinationDockerId && destinationDocker.isCoolifyProxyUsed) { - // Remove HAProxy - const found = await checkContainer({ dockerId: localDocker.id, container: `${id}-${publicPort}` }); - if (found) { - await executeDockerCmd({ - dockerId: localDocker.id, - command: `docker stop -t 0 ${id}-${publicPort} && docker rm ${id}-${publicPort} ` - }) - } - await startTraefikTCPProxy(destinationDocker, id, publicPort, 9000); - } - } - await prisma.$disconnect(); } else process.exit(0); })(); diff --git a/apps/api/src/jobs/cleanupPrismaEngines.ts b/apps/api/src/jobs/cleanupPrismaEngines.ts new file mode 100644 index 000000000..b1c912edc --- /dev/null +++ b/apps/api/src/jobs/cleanupPrismaEngines.ts @@ -0,0 +1,16 @@ +import { parentPort } from 'node:worker_threads'; +import { asyncExecShell, isDev, prisma } from '../lib/common'; + +(async () => { + if (parentPort) { + if (!isDev) { + try { + await asyncExecShell(`killall -q -e /app/prisma-engines/query-engine -o 10m`) + } catch (error) { + console.log(error); + } finally { + await prisma.$disconnect(); + } + } + } else process.exit(0); +})(); diff --git a/apps/api/src/lib/scheduler.ts b/apps/api/src/lib/scheduler.ts index c32226bc9..d48ed1f11 100644 --- a/apps/api/src/lib/scheduler.ts +++ b/apps/api/src/lib/scheduler.ts @@ -35,6 +35,9 @@ const options: any = { { name: 'cleanupStorage', }, + { + name: 'cleanupPrismaEngines', + }, { name: 'checkProxies', interval: '10s'