improve(worker): add env variable to disable workers

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-11-27 12:54:36 +01:00
parent 6f35562976
commit 9ffa213fc2
3 changed files with 213 additions and 185 deletions

View File

@@ -0,0 +1,76 @@
import type { CronQueueType } from '@openpanel/queue';
import { cronQueue } from '@openpanel/queue';
import { logger } from './utils/logger';
export async function bootCron() {
const jobs: {
name: string;
type: CronQueueType;
pattern: string | number;
}[] = [
{
name: 'salt',
type: 'salt',
pattern: '0 0 * * *',
},
{
name: 'flush',
type: 'flushEvents',
pattern: 1000 * 10,
},
{
name: 'flush',
type: 'flushProfiles',
pattern: 1000 * 60,
},
];
if (process.env.SELF_HOSTED && process.env.NODE_ENV === 'production') {
jobs.push({
name: 'ping',
type: 'ping',
pattern: '0 0 * * *',
});
}
// Add repeatable jobs
for (const job of jobs) {
await cronQueue.add(
job.name,
{
type: job.type,
payload: undefined,
},
{
jobId: job.type,
repeat:
typeof job.pattern === 'number'
? {
every: job.pattern,
}
: {
pattern: job.pattern,
},
},
);
}
// Remove outdated repeatable jobs
const repeatableJobs = await cronQueue.getRepeatableJobs();
for (const repeatableJob of repeatableJobs) {
const match = jobs.find(
(job) => `${job.name}:${job.type}:::${job.pattern}` === repeatableJob.key,
);
if (match) {
logger.info('Repeatable job exists', {
key: repeatableJob.key,
});
} else {
logger.info('Removing repeatable job', {
key: repeatableJob.key,
});
cronQueue.removeRepeatableByKey(repeatableJob.key);
}
}
}