158 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-02-10 15:47:44 +01:00
import * as Bullmq from 'bullmq';
import { default as ProdBullmq, Job, QueueEvents, QueueScheduler } from 'bullmq';
import cuid from 'cuid';
import { dev } from '$app/env';
import { prisma } from '$lib/database';
import builder from './builder';
import logger from './logger';
import cleanup from './cleanup';
import proxy from './proxy';
import ssl from './ssl';
import sslrenewal from './sslrenewal';
import { asyncExecShell, saveBuildLog } from '$lib/common';
let { Queue, Worker } = Bullmq;
let redisHost = 'localhost';
if (!dev) {
Queue = ProdBullmq.Queue;
Worker = ProdBullmq.Worker;
redisHost = 'coolify-redis';
}
const connectionOptions = {
connection: {
host: redisHost
}
};
const cron = async () => {
new QueueScheduler('proxy', connectionOptions);
new QueueScheduler('cleanup', connectionOptions);
new QueueScheduler('ssl', connectionOptions);
new QueueScheduler('sslRenew', connectionOptions);
const queue = {
proxy: new Queue('proxy', { ...connectionOptions }),
cleanup: new Queue('cleanup', { ...connectionOptions }),
ssl: new Queue('ssl', { ...connectionOptions }),
sslRenew: new Queue('sslRenew', { ...connectionOptions })
};
await queue.proxy.drain();
await queue.cleanup.drain();
await queue.ssl.drain();
await queue.sslRenew.drain();
new Worker(
'proxy',
async () => {
await proxy();
},
{
...connectionOptions
}
);
new Worker(
'ssl',
async () => {
await ssl();
},
{
...connectionOptions
}
);
new Worker(
'cleanup',
async () => {
await cleanup();
},
{
...connectionOptions
}
);
new Worker(
'sslRenew',
async () => {
await sslrenewal();
},
{
...connectionOptions
}
);
await queue.proxy.add('proxy', {}, { repeat: { every: 10000 } });
2022-03-01 13:07:34 +01:00
await queue.ssl.add('ssl', {}, { repeat: { every: 60000 } });
await queue.cleanup.add('cleanup', {}, { repeat: { every: 600000 } });
await queue.sslRenew.add('sslRenew', {}, { repeat: { every: 1800000 } });
2022-02-10 15:47:44 +01:00
const events = {
proxy: new QueueEvents('proxy', { ...connectionOptions }),
ssl: new QueueEvents('ssl', { ...connectionOptions })
};
events.proxy.on('completed', (data) => {
// console.log(data)
});
events.ssl.on('completed', (data) => {
// console.log(data)
});
};
cron().catch((error) => {
console.log('cron failed to start');
console.log(error);
});
2022-02-11 20:28:56 +01:00
const buildQueueName = 'build_queue';
2022-02-10 15:47:44 +01:00
const buildQueue = new Queue(buildQueueName, connectionOptions);
const buildWorker = new Worker(buildQueueName, async (job) => await builder(job), {
concurrency: 2,
...connectionOptions
});
buildWorker.on('completed', async (job: Bullmq.Job) => {
try {
await prisma.build.update({ where: { id: job.data.build_id }, data: { status: 'success' } });
} catch (err) {
console.log(err);
} finally {
2022-02-11 20:28:56 +01:00
const workdir = `/tmp/build-sources/${job.data.repository}/`;
2022-02-10 21:43:54 +01:00
await asyncExecShell(`rm -fr ${workdir}`);
2022-02-10 15:47:44 +01:00
}
return;
});
buildWorker.on('failed', async (job: Bullmq.Job, failedReason) => {
try {
await prisma.build.update({ where: { id: job.data.build_id }, data: { status: 'failed' } });
} catch (error) {
console.log(error);
} finally {
2022-02-11 20:28:56 +01:00
const workdir = `/tmp/build-sources/${job.data.repository}`;
2022-02-10 21:43:54 +01:00
await asyncExecShell(`rm -fr ${workdir}`);
2022-02-10 15:47:44 +01:00
}
2022-02-18 08:48:05 +01:00
saveBuildLog({
line: 'Failed to deploy!',
buildId: job.data.build_id,
applicationId: job.data.id
});
2022-02-10 15:47:44 +01:00
saveBuildLog({
line: `Reason: ${failedReason.toString()}`,
buildId: job.data.build_id,
applicationId: job.data.id
});
});
const buildLogQueueName = 'log_queue';
2022-02-10 15:47:44 +01:00
const buildLogQueue = new Queue(buildLogQueueName, connectionOptions);
const buildLogWorker = new Worker(buildLogQueueName, async (job) => await logger(job), {
concurrency: 1,
...connectionOptions
});
export { buildQueue, buildLogQueue };