The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Handle acking only processed events
Ian MacCallum2b3e004f9fad
diff --git a/apps/web/app/(ee)/api/cron/projects/usage-updates-queue/route.ts b/apps/web/app/(ee)/api/cron/projects/usage-updates-queue/route.tsindex f6b72af2ca..a66336b2f4 100644--- a/apps/web/app/(ee)/api/cron/projects/usage-updates-queue/route.ts+++ b/apps/web/app/(ee)/api/cron/projects/usage-updates-queue/route.ts@@ -18,6 +18,7 @@ type ProjectAggregateUsage = {clicks: number;firstTimestamp: number;lastTimestamp: number;+ entryIds: string[];};type ProcessResponse = {@@ -56,6 +57,7 @@ const aggregateProjectUsage = (existing.clicks += 1;existing.lastTimestamp = Math.max(existing.lastTimestamp, timestamp);existing.firstTimestamp = Math.min(existing.firstTimestamp, timestamp);+ existing.entryIds.push(entry.id);} else {aggregatedUsage.set(projectId, {projectId,@@ -63,6 +65,7 @@ const aggregateProjectUsage = (clicks: 1,firstTimestamp: timestamp,lastTimestamp: timestamp,+ entryIds: [entry.id],});}}@@ -74,20 +77,21 @@ const aggregateProjectUsage = (};const processProjectUpdateStreamBatch = () =>- projectUsageStream.processBatch<ClickEvent, ProcessResponse>(+ projectUsageStream.processBatch<ClickEvent>(async (entries) => {if (!entries || Object.keys(entries).length === 0) {return {success: true,updates: [],- } as ProcessResponse;+ processedEntryIds: [],+ };}const { updates, lastProcessedId } = aggregateProjectUsage(entries);if (updates.length === 0) {console.log("No project usage updates to process");- return { success: true, updates: [] } as ProcessResponse;+ return { success: true, updates: [], processedEntryIds: [] };}console.log(@@ -104,6 +108,7 @@ const processProjectUpdateStreamBatch = () =>let totalProcessed = 0;const errors: { projectId: string; error: any }[] = [];+ const processedEntryIds: string[] = [];for (const batch of batches) {console.log(`BATCH: ${JSON.stringify(batch)}`);@@ -118,6 +123,8 @@ const processProjectUpdateStreamBatch = () =>[update.usage, update.clicks, update.projectId],);+ processedEntryIds.push(...update.entryIds);+return {...update,success: true,@@ -172,7 +179,8 @@ const processProjectUpdateStreamBatch = () =>errors,totalProcessed,lastProcessedId,- } as ProcessResponse;+ processedEntryIds,+ };},{count: BATCH_SIZE,diff --git a/apps/web/lib/upstash/redis-streams.ts b/apps/web/lib/upstash/redis-streams.tsindex 3005010827..cadff72379 100644--- a/apps/web/lib/upstash/redis-streams.ts+++ b/apps/web/lib/upstash/redis-streams.ts@@ -26,15 +26,17 @@ export class RedisStream {* Read and process a batch of items from the stream* This provides a consumable batch of messages with the ability to delete them upon successful completion of the handler*/- async processBatch<T, Response>(- handler: (records: Entry<T>[]) => Promise<Response>,+ async processBatch<T>(+ handler: (+ records: Entry<T>[],+ ) => Promise<any & { processedEntryIds: string[] }>,options: {startId?: string;endId?: string;count?: number;deleteAfterRead?: boolean;} = {},- ): Promise<Response> {+ ): Promise<any> {const {startId = "0",endId = "+",@@ -58,16 +60,11 @@ export class RedisStream {}),);- const response = await handler(entries);+ const { processedEntryIds, ...response } = await handler(entries);// Optionally delete processed entries to prevent memory buildupif (deleteAfterRead) {try {- // Delete all entries up to and including the last processed ID- // We do this by collecting the IDs we just processed and deleting them- // (Assumes entries is the batch we just processed)- const processedEntryIds = Object.keys(entriesMap);-if (processedEntryIds.length > 0) {// xdel supports deleting multiple IDs at onceawait redis.xdel(this.streamKey, processedEntryIds);@@ -78,7 +75,7 @@ export class RedisStream {}}- return response;+ return response as Response;} catch (error) {console.error("Failed to read project usage updates from stream:", error);throw error;