From 6f0f43d75148d72c0aa515d6af56cb8191f73a1b Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 2 Dec 2024 13:01:26 -0300 Subject: [PATCH] define tryAcquire before cleanSubscription --- apps/api/src/lock.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/api/src/lock.ts b/apps/api/src/lock.ts index 74bc94d3..fa75cf88 100644 --- a/apps/api/src/lock.ts +++ b/apps/api/src/lock.ts @@ -34,20 +34,6 @@ export async function acquireLock( const channel = `lock_releases_${getPartition(name)}` return new Promise(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 @@ -179,6 +165,20 @@ export async function acquireLock( } } + 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() }) }