fix: simply event buffer

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-11-03 12:02:35 +01:00
parent 1285ad85a2
commit 742ee8dc1c
7 changed files with 753 additions and 1335 deletions

View File

@@ -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,
},
},
);
}
}
}

View File

@@ -1,13 +1,13 @@
import type { Job } from 'bullmq';
import { logger as baseLogger } from '@/utils/logger';
import { getTime } from '@openpanel/common';
import {
type IClickhouseSession,
type IServiceCreateEventPayload,
type IServiceEvent,
TABLE_NAMES,
checkNotificationRulesForSessionEnd,
convertClickhouseDateToJs,
createEvent,
eventBuffer,
formatClickhouseDate,
@@ -77,7 +77,7 @@ export async function createSessionEnd(
}
try {
handleSessionEndNotifications({
await handleSessionEndNotifications({
session,
payload,
});
@@ -103,7 +103,9 @@ export async function createSessionEnd(
name: 'session_end',
duration: session.duration ?? 0,
path: lastScreenView?.path ?? '',
createdAt: new Date(getTime(session.ended_at) + 1000),
createdAt: new Date(
convertClickhouseDateToJs(session.ended_at).getTime() + 100,
),
profileId: lastScreenView?.profileId || payload.profileId,
});
}