From 4a5e427b2695ccb756626685f4c872592af70df1 Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Fri, 8 Nov 2024 14:03:53 -0600 Subject: [PATCH] added short poll endpoint to be called after zap req broadcasted and will run for 120 seconds initially, long running cron will kick in after that --- src/pages/api/invoices/short-poll.js | 44 ++++++++++++++++++++++++++ src/pages/api/lightning-address/lnd.js | 33 ++++++------------- vercel.json | 2 +- 3 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 src/pages/api/invoices/short-poll.js diff --git a/src/pages/api/invoices/short-poll.js b/src/pages/api/invoices/short-poll.js new file mode 100644 index 0000000..aed148c --- /dev/null +++ b/src/pages/api/invoices/short-poll.js @@ -0,0 +1,44 @@ +import axios from "axios"; + +const PLEBDEVS_API_KEY = process.env.PLEBDEVS_API_KEY; +const BACKEND_URL = process.env.BACKEND_URL; + +export default async function handler(req, res) { + // Verify API key + const apiKey = req.headers['authorization']; + if (!apiKey || apiKey !== PLEBDEVS_API_KEY) { + res.status(401).json({ error: 'Unauthorized' }); + return; + } + + try { + // Poll for 120 seconds maximum + const startTime = Date.now(); + const timeoutDuration = 120000; // 120 seconds in milliseconds + + while (Date.now() - startTime < timeoutDuration) { + const pollResponse = await axios.get(`${BACKEND_URL}/api/invoices/polling`, { + headers: { + 'Authorization': PLEBDEVS_API_KEY + } + }); + + console.log('Polling response', pollResponse.data); + + // If no pending invoices, we can stop polling + if (pollResponse.data.pending === 0) { + res.status(200).json({ success: true, settled: true }); + return; + } + + // Wait 1 second before next poll + await new Promise(resolve => setTimeout(resolve, 1000)); + } + + // If we reach here, we timed out + res.status(200).json({ success: true, settled: false }); + } catch (error) { + console.error('Polling error:', error); + res.status(500).json({ error: 'Polling failed' }); + } +} \ No newline at end of file diff --git a/src/pages/api/lightning-address/lnd.js b/src/pages/api/lightning-address/lnd.js index 8e921d9..0e9e6c4 100644 --- a/src/pages/api/lightning-address/lnd.js +++ b/src/pages/api/lightning-address/lnd.js @@ -78,30 +78,17 @@ export default async function handler(req, res) { settled: false }, { ex: expiry || 86400 }); - // Start polling for this zap request - let attempts = 0; - console.log('Starting polling interval', attempts); - const pollInterval = setInterval(async () => { - console.log('Polling for invoice', attempts); - try { - const pollResponse = await axios.get(`${BACKEND_URL}/api/invoices/polling`, { - headers: { - 'Authorization': PLEBDEVS_API_KEY - } - }); - console.log('Polling response', pollResponse.data); - - // If no pending invoices or we've reached max attempts, stop polling - if (pollResponse.data.pending === 0 || attempts >= 120) { - clearInterval(pollInterval); + // Trigger the polling endpoint + try { + await axios.get(`${BACKEND_URL}/api/invoices/short-poll`, { + headers: { + 'Authorization': PLEBDEVS_API_KEY } - - attempts++; - } catch (error) { - console.error('Polling error:', error); - clearInterval(pollInterval); - } - }, 1000); // Poll every second + }); + } catch (error) { + console.error('Error triggering polling:', error); + // Continue even if polling fails + } res.status(200).json({ invoice, diff --git a/vercel.json b/vercel.json index 584b419..bcf9dff 100644 --- a/vercel.json +++ b/vercel.json @@ -11,7 +11,7 @@ } ], "functions": { - "src/pages/api/invoices/polling.js": { + "src/pages/api/invoices/short-poll.js": { "maxDuration": 120 } }