Skip to content

Commit

Permalink
define tryAcquire before cleanSubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiralucas committed Dec 2, 2024
1 parent 0a94f08 commit 6f0f43d
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions apps/api/src/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ export async function acquireLock<T>(
const channel = `lock_releases_${getPartition(name)}`

return new Promise<T>(async (resolve, reject) => {
const cleanSubscription = await subscribe(channel, async (event) => {
if (acquired) {
return
}

if (event === name) {
logger().trace(
{ name, ownerId, channel },
'Got lock released message. Anticipating lock acquisition attempt'
)
tryAcquire()
}
})

const tryAcquire = async () => {
if (acquired) {
return
Expand Down Expand Up @@ -179,6 +165,20 @@ export async function acquireLock<T>(
}
}

const cleanSubscription = await subscribe(channel, async (event) => {
if (acquired) {
return
}

if (event === name) {
logger().trace(
{ name, ownerId, channel },
'Got lock released message. Anticipating lock acquisition attempt'
)
tryAcquire()
}
})

tryAcquire()
})
}

0 comments on commit 6f0f43d

Please sign in to comment.