fix: simply event buffer
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { CronQueueType } from '@openpanel/queue';
|
||||
import { cronQueue } from '@openpanel/queue';
|
||||
|
||||
import { getLock } from '@openpanel/redis';
|
||||
import { logger } from './utils/logger';
|
||||
|
||||
export async function bootCron() {
|
||||
@@ -44,43 +45,40 @@ export async function bootCron() {
|
||||
});
|
||||
}
|
||||
|
||||
// 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,
|
||||
},
|
||||
},
|
||||
);
|
||||
const lock = await getLock('cron:lock', '1', 1000 * 60 * 60 * 5);
|
||||
|
||||
if (lock) {
|
||||
logger.info('Cron lock acquired');
|
||||
} else {
|
||||
logger.info('Cron lock not acquired');
|
||||
}
|
||||
|
||||
// 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,
|
||||
});
|
||||
if (lock) {
|
||||
logger.info('Updating cron jobs');
|
||||
// TODO: Switch to getJobSchedulers
|
||||
const repeatableJobs = await cronQueue.getRepeatableJobs();
|
||||
for (const repeatableJob of repeatableJobs) {
|
||||
cronQueue.removeRepeatableByKey(repeatableJob.key);
|
||||
}
|
||||
|
||||
// Add repeatable jobs
|
||||
for (const job of jobs) {
|
||||
await cronQueue.upsertJobScheduler(
|
||||
job.type,
|
||||
typeof job.pattern === 'number'
|
||||
? {
|
||||
every: job.pattern,
|
||||
}
|
||||
: {
|
||||
pattern: job.pattern,
|
||||
},
|
||||
{
|
||||
data: {
|
||||
type: job.type,
|
||||
payload: undefined,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user