From cac0d8c3b104fd3d28a499e9c518f54b659309e7 Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Fri, 8 Nov 2024 14:33:16 -0600 Subject: [PATCH] Add env variables to cron jobs --- src/pages/api/invoices/polling.js | 16 ++++++++++ src/pages/api/invoices/short-poll.js | 44 -------------------------- src/pages/api/lightning-address/lnd.js | 9 ------ vercel.json | 13 +++----- 4 files changed, 21 insertions(+), 61 deletions(-) delete mode 100644 src/pages/api/invoices/short-poll.js diff --git a/src/pages/api/invoices/polling.js b/src/pages/api/invoices/polling.js index 52beb44..00c382c 100644 --- a/src/pages/api/invoices/polling.js +++ b/src/pages/api/invoices/polling.js @@ -16,8 +16,20 @@ export default async function handler(req, res) { } try { + // Add execution time limit protection + const startTime = Date.now(); + const TIMEOUT_MS = 8000; // Vercel timeout is 10s, give ourselves margin + // Get all invoice keys from Redis const keys = await kv.keys('invoice:*'); + + // Add batch size limit + const BATCH_LIMIT = 500; + if (keys.length > BATCH_LIMIT) { + console.warn(`Large number of invoices: ${keys.length}. Processing first ${BATCH_LIMIT} only.`); + keys.length = BATCH_LIMIT; + } + const results = { processed: 0, settled: 0, @@ -28,6 +40,10 @@ export default async function handler(req, res) { // Process each invoice for (const key of keys) { + if (Date.now() - startTime > TIMEOUT_MS) { + console.warn('Approaching timeout, stopping processing'); + break; + } try { const invoiceData = await kv.get(key); if (!invoiceData) continue; diff --git a/src/pages/api/invoices/short-poll.js b/src/pages/api/invoices/short-poll.js deleted file mode 100644 index 1aef79c..0000000 --- a/src/pages/api/invoices/short-poll.js +++ /dev/null @@ -1,44 +0,0 @@ -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 = 30000; // 30 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 efe8b48..537f0b0 100644 --- a/src/pages/api/lightning-address/lnd.js +++ b/src/pages/api/lightning-address/lnd.js @@ -78,15 +78,6 @@ export default async function handler(req, res) { settled: false }, { ex: expiry || 86400 }); - // Trigger the polling endpoint without waiting for it - fetch(`${BACKEND_URL}/api/lightning-address/short-poll`, { - headers: { - 'Authorization': PLEBDEVS_API_KEY - } - }).catch(error => { - console.error('Error triggering polling:', error); - }); - // Return response immediately res.status(200).json({ invoice, diff --git a/vercel.json b/vercel.json index 79ce55e..84caa32 100644 --- a/vercel.json +++ b/vercel.json @@ -3,16 +3,13 @@ "crons": [ { "path": "/api/users/subscription/cron", - "schedule": "0 0 * * *" + "schedule": "0 0 * * *", + "authorization": "@CRON_SECRET" }, { "path": "/api/invoices/polling", - "schedule": "*/10 * * * *" + "schedule": "*/3 * * * *", + "authorization": "@PLEBDEVS_API_KEY" } - ], - "functions": { - "src/pages/api/invoices/short-poll.js": { - "maxDuration": 60 - } - } + ] }