diff --git a/packages/backend-core/src/redis/redlockImpl.ts b/packages/backend-core/src/redis/redlockImpl.ts index d3a74bb4db..f641bbf36e 100644 --- a/packages/backend-core/src/redis/redlockImpl.ts +++ b/packages/backend-core/src/redis/redlockImpl.ts @@ -63,10 +63,22 @@ export const newRedlock = async (opts: Options = {}) => { return new Redlock([client], options) } +type SuccessfulRedlockExecution = { + executed: true + result: T +} +type UnsuccessfulRedlockExecution = { + executed: false +} + +type RedlockExecution = + | SuccessfulRedlockExecution + | UnsuccessfulRedlockExecution + export const doWithLock = async ( opts: LockOptions, task: () => Promise -) => { +): Promise> => { const redlock = await getClient(opts.type) let lock try { @@ -86,7 +98,7 @@ export const doWithLock = async ( // perform locked task // need to await to ensure completion before unlocking const result = await task() - return result + return { executed: true, result } } catch (e: any) { console.warn("lock error") // lock limit exceeded @@ -95,7 +107,7 @@ export const doWithLock = async ( // don't throw for try-once locks, they will always error // due to retry count (0) exceeded console.warn(e) - return + return { executed: false } } else { console.error(e) throw e