From 534ed7a6461de9c081ce49f88c279910ec8d8f82 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 19 Dec 2024 21:17:06 -0600 Subject: [PATCH] Don't error our device saving for nonexistent user --- apps/platform/src/queue/Job.ts | 9 +++++++++ apps/platform/src/queue/RedisQueueProvider.ts | 3 +++ apps/platform/src/users/UserDeviceJob.ts | 11 +++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/platform/src/queue/Job.ts b/apps/platform/src/queue/Job.ts index 60483331..4cc95bea 100644 --- a/apps/platform/src/queue/Job.ts +++ b/apps/platform/src/queue/Job.ts @@ -6,9 +6,14 @@ interface JobOptions { jobId?: string } +interface JobState { + attemptsMade: number +} + export interface EncodedJob { data: any options: JobOptions + state: JobState name: string token?: string } @@ -29,6 +34,10 @@ export default class Job implements EncodedJob { attempts: 3, } + state: JobState = { + attemptsMade: 0, + } + static $name: string = Job.constructor.name get name() { diff --git a/apps/platform/src/queue/RedisQueueProvider.ts b/apps/platform/src/queue/RedisQueueProvider.ts index 1219a437..4b559c49 100644 --- a/apps/platform/src/queue/RedisQueueProvider.ts +++ b/apps/platform/src/queue/RedisQueueProvider.ts @@ -95,6 +95,9 @@ export default class RedisQueueProvider implements QueueProvider { ...job.data.options, jobId: job.id, }, + state: { + attemptsMade: job.attemptsMade, + }, token, }) }, { diff --git a/apps/platform/src/users/UserDeviceJob.ts b/apps/platform/src/users/UserDeviceJob.ts index f5bd9910..077ff140 100644 --- a/apps/platform/src/users/UserDeviceJob.ts +++ b/apps/platform/src/users/UserDeviceJob.ts @@ -13,7 +13,14 @@ export default class UserDeviceJob extends Job { return new this(data) } - static async handler({ project_id, ...device }: UserDeviceTrigger) { - await saveDevice(project_id, device) + static async handler({ project_id, ...device }: UserDeviceTrigger, job: UserDeviceJob) { + const attempts = job.options.attempts ?? 1 + const attemptsMade = job.state.attemptsMade ?? 0 + + try { + await saveDevice(project_id, device) + } catch (error) { + if (attemptsMade < (attempts - 1)) throw error + } } }