fix: don't throw lock errors

This commit is contained in:
Chris Anderson 2025-07-24 13:38:00 -05:00
parent 49306af8f9
commit 8f32370014
3 changed files with 25 additions and 6 deletions

View file

@ -45,4 +45,9 @@ export const releaseLock = async (key: string) => {
await App.main.redis.del(`lock:${key}`)
}
export class LockError extends Error {}
export class LockError extends Error {
constructor(message: string) {
super(message)
this.name = 'LockError'
}
}

View file

@ -20,7 +20,12 @@ export interface EncodedJob {
}
export class JobError extends Error {}
export class RetryError extends JobError {}
export class RetryError extends JobError {
constructor(message: string) {
super(message)
this.name = 'RetryError'
}
}
export const JobPriority = {
none: 0,

View file

@ -1,6 +1,8 @@
import { Job } from '../queue'
import { EncodedJob, Job } from '../queue'
import { saveDevice } from './UserRepository'
import { DeviceParams } from './Device'
import { LockError } from '../core/Lock'
import App from '../app'
type UserDeviceTrigger = DeviceParams & {
project_id: number
@ -13,13 +15,20 @@ export default class UserDeviceJob extends Job {
return new this(data)
}
static async handler({ project_id, ...device }: UserDeviceTrigger, job: UserDeviceJob) {
const attempts = job.options.attempts ?? 1
const attemptsMade = job.state.attemptsMade ?? 0
static async handler({ project_id, ...device }: UserDeviceTrigger, raw: EncodedJob) {
const attempts = raw.options.attempts ?? 1
const attemptsMade = raw.state.attemptsMade ?? 0
try {
await saveDevice(project_id, device)
} catch (error) {
// If record is locked, re-queue the job
if (error instanceof LockError) {
await App.main.queue.retry(raw)
throw error
}
if (attemptsMade < (attempts - 1)) throw error
}
}