mirror of
https://fast.feibisi.com/https://github.com/parcelvoy/platform.git
synced 2025-09-01 12:26:08 +08:00
fix: don't throw lock errors
This commit is contained in:
parent
49306af8f9
commit
8f32370014
3 changed files with 25 additions and 6 deletions
|
@ -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'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue