fix: duplicate session start (race condition) + remove old device id handling

This commit is contained in:
Carl-Gerhard Lindesvärd
2026-02-27 09:56:51 +01:00
parent a42adcdbfb
commit 928c44ef6a
6 changed files with 87 additions and 105 deletions

View File

@@ -1,5 +1,3 @@
import { Queue, QueueEvents } from 'bullmq';
import { createHash } from 'node:crypto';
import type {
IServiceCreateEventPayload,
@@ -8,12 +6,13 @@ import type {
} from '@openpanel/db';
import { createLogger } from '@openpanel/logger';
import { getRedisGroupQueue, getRedisQueue } from '@openpanel/redis';
import { Queue, QueueEvents } from 'bullmq';
import { Queue as GroupQueue } from 'groupmq';
import type { ITrackPayload } from '../../validation';
export const EVENTS_GROUP_QUEUES_SHARDS = Number.parseInt(
process.env.EVENTS_GROUP_QUEUES_SHARDS || '1',
10,
10
);
export const getQueueName = (name: string) =>
@@ -65,8 +64,6 @@ export interface EventsQueuePayloadIncomingEvent {
latitude: number | undefined;
};
headers: Record<string, string | undefined>;
currentDeviceId: string; // TODO: Remove
previousDeviceId: string; // TODO: Remove
deviceId: string;
sessionId: string;
};
@@ -154,12 +151,12 @@ export type CronQueueType = CronQueuePayload['type'];
const orderingDelayMs = Number.parseInt(
process.env.ORDERING_DELAY_MS || '100',
10,
10
);
const autoBatchMaxWaitMs = Number.parseInt(
process.env.AUTO_BATCH_MAX_WAIT_MS || '0',
10,
10
);
const autoBatchSize = Number.parseInt(process.env.AUTO_BATCH_SIZE || '0', 10);
@@ -170,12 +167,12 @@ export const eventsGroupQueues = Array.from({
new GroupQueue<EventsQueuePayloadIncomingEvent['payload']>({
logger: process.env.NODE_ENV === 'production' ? queueLogger : undefined,
namespace: getQueueName(
list.length === 1 ? 'group_events' : `group_events_${index}`,
list.length === 1 ? 'group_events' : `group_events_${index}`
),
redis: getRedisGroupQueue(),
keepCompleted: 1_000,
keepCompleted: 1000,
keepFailed: 10_000,
orderingDelayMs: orderingDelayMs,
orderingDelayMs,
autoBatch:
autoBatchMaxWaitMs && autoBatchSize
? {
@@ -183,7 +180,7 @@ export const eventsGroupQueues = Array.from({
size: autoBatchSize,
}
: undefined,
}),
})
);
export const getEventsGroupQueueShard = (groupId: string) => {
@@ -202,7 +199,7 @@ export const sessionsQueue = new Queue<SessionsQueuePayload>(
defaultJobOptions: {
removeOnComplete: 10,
},
},
}
);
export const sessionsQueueEvents = new QueueEvents(getQueueName('sessions'), {
connection: getRedisQueue(),
@@ -236,7 +233,7 @@ export const notificationQueue = new Queue<NotificationQueuePayload>(
defaultJobOptions: {
removeOnComplete: 10,
},
},
}
);
export type ImportQueuePayload = {
@@ -254,7 +251,7 @@ export const importQueue = new Queue<ImportQueuePayload>(
removeOnComplete: 10,
removeOnFail: 50,
},
},
}
);
export type InsightsQueuePayloadProject = {
@@ -269,5 +266,5 @@ export const insightsQueue = new Queue<InsightsQueuePayloadProject>(
defaultJobOptions: {
removeOnComplete: 100,
},
},
}
);