0x0x230x

Untitled

Mar 31st, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async lockTipsForClaiming(
  2.     tips: Tip[],
  3.     walletAddress: string,
  4.   ): Promise<string | null> {
  5.     const claimNonce = crypto.randomUUID();
  6.     const now = new Date();
  7.     const lockKey = this.getLockKey(tips);
  8.  
  9.     console.log(
  10.       `[ClaimLockService] 🔒 Attempting Redis lock for key: ${lockKey}`,
  11.     );
  12.  
  13.     const lock = new Lock({
  14.       id: lockKey,
  15.       lease: this.LOCK_LEASE,
  16.       redis: Redis.fromEnv(),
  17.     });
  18.  
  19.     const acquired = await lock.acquire();
  20.  
  21.     if (!acquired) {
  22.       console.error(
  23.         `[ClaimLockService] ❌ Failed to acquire Redis lock for tips at`,
  24.       );
  25.       return null;
  26.     }
  27.  
  28.     // Store the lock instance for this flow
  29.     this.activeLocks.set(lockKey, lock);
  30.     console.log(`[ClaimLockService] ✅ Redis lock acquired`);
  31.  
  32.     try {
  33.       const { error } = await supabaseClient
  34.         .from('gigbot_tips')
  35.         .update({
  36.           claim_nonce: claimNonce,
  37.           claim_started_at: now.toISOString(),
  38.           status: 'processing',
  39.           user_address: walletAddress,
  40.         })
  41.         .in(
  42.           'id',
  43.           tips.map((tip) => tip.id),
  44.         )
  45.         .eq('status', 'pending')
  46.         .is('tx_hash', null)
  47.         .is('claim_nonce', null);
  48.  
  49.       if (error) {
  50.         console.error(
  51.           '[ClaimLockService] ❌ Failed to lock tips:',
  52.           error.message,
  53.         );
  54.         console.log(
  55.           '[ClaimLockService] 🔓 Releasing Redis lock due to Supabase error',
  56.         );
  57.         await this.releaseLock(lockKey);
  58.         return null;
  59.       }
  60.  
  61.       const gigId = tips[0]?.gig?.id;
  62.       console.log(
  63.         `[ClaimLockService] ✅ Supabase Locked ${
  64.           tips.length
  65.         } tips with nonce: ${claimNonce}${gigId ? ` (gig id: ${gigId})` : ''}`,
  66.       );
  67.       return claimNonce;
  68.     } catch (error) {
  69.       console.log('[ClaimLockService] 🔓 Releasing Redis lock due to error');
  70.       await this.releaseLock(lockKey);
  71.       throw error;
  72.     }
  73.   }
Advertisement
Add Comment
Please, Sign In to add comment