Skip to content

Commit

Permalink
make sure we dont try lock again after it has already rejected
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Dec 2, 2024
1 parent 634664b commit 517ae08
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions apps/api/src/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function acquireLock<T>(

const ownerId = uuidv4()
let acquired = false
let failed = false
let attempt = 0
let timeout: NodeJS.Timeout | null = null

Expand All @@ -40,7 +41,7 @@ export async function acquireLock<T>(
let cleanSubscription: () => Promise<void> = async () => {}

const tryAcquire = async () => {
if (acquired) {
if (acquired || failed) {
return
}

Expand Down Expand Up @@ -132,6 +133,15 @@ export async function acquireLock<T>(
{ name, ownerId, channel, err },
'Failed to acquire lock'
)
failed = true
try {
await cleanSubscription()
} catch (err) {
logger().error(
{ name, ownerId, channel, err },
'Failed to clean subscription'
)
}
reject(err)
return
}
Expand Down Expand Up @@ -203,12 +213,13 @@ export async function acquireLock<T>(
if (r.success) {
resolve(r.data)
} else {
failed = true
reject(r.error)
}
}

cleanSubscription = await subscribe(channel, async (event) => {
if (acquired) {
if (acquired || failed) {
await cleanSubscription()
return
}
Expand Down

0 comments on commit 517ae08

Please sign in to comment.