Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async lockTipsForClaiming(
- tips: Tip[],
- walletAddress: string,
- ): Promise<string | null> {
- const claimNonce = crypto.randomUUID();
- const now = new Date();
- const lockKey = this.getLockKey(tips);
- console.log(
- `[ClaimLockService] 🔒 Attempting Redis lock for key: ${lockKey}`,
- );
- const lock = new Lock({
- id: lockKey,
- lease: this.LOCK_LEASE,
- redis: Redis.fromEnv(),
- });
- const acquired = await lock.acquire();
- if (!acquired) {
- console.error(
- `[ClaimLockService] ❌ Failed to acquire Redis lock for tips at`,
- );
- return null;
- }
- // Store the lock instance for this flow
- this.activeLocks.set(lockKey, lock);
- console.log(`[ClaimLockService] ✅ Redis lock acquired`);
- try {
- const { error } = await supabaseClient
- .from('gigbot_tips')
- .update({
- claim_nonce: claimNonce,
- claim_started_at: now.toISOString(),
- status: 'processing',
- user_address: walletAddress,
- })
- .in(
- 'id',
- tips.map((tip) => tip.id),
- )
- .eq('status', 'pending')
- .is('tx_hash', null)
- .is('claim_nonce', null);
- if (error) {
- console.error(
- '[ClaimLockService] ❌ Failed to lock tips:',
- error.message,
- );
- console.log(
- '[ClaimLockService] 🔓 Releasing Redis lock due to Supabase error',
- );
- await this.releaseLock(lockKey);
- return null;
- }
- const gigId = tips[0]?.gig?.id;
- console.log(
- `[ClaimLockService] ✅ Supabase Locked ${
- tips.length
- } tips with nonce: ${claimNonce}${gigId ? ` (gig id: ${gigId})` : ''}`,
- );
- return claimNonce;
- } catch (error) {
- console.log('[ClaimLockService] 🔓 Releasing Redis lock due to error');
- await this.releaseLock(lockKey);
- throw error;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment