Add env variables to cron jobs

This commit is contained in:
austinkelsay 2024-11-08 14:33:16 -06:00
parent 83c614f276
commit cac0d8c3b1
No known key found for this signature in database
GPG Key ID: 44CB4EC6D9F2FA02
4 changed files with 21 additions and 61 deletions

View File

@ -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;

View File

@ -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' });
}
}

View File

@ -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,

View File

@ -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
}
}
]
}