mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-06 18:31:00 +00:00
Add env variables to cron jobs
This commit is contained in:
parent
83c614f276
commit
cac0d8c3b1
@ -16,8 +16,20 @@ export default async function handler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
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
|
// Get all invoice keys from Redis
|
||||||
const keys = await kv.keys('invoice:*');
|
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 = {
|
const results = {
|
||||||
processed: 0,
|
processed: 0,
|
||||||
settled: 0,
|
settled: 0,
|
||||||
@ -28,6 +40,10 @@ export default async function handler(req, res) {
|
|||||||
|
|
||||||
// Process each invoice
|
// Process each invoice
|
||||||
for (const key of keys) {
|
for (const key of keys) {
|
||||||
|
if (Date.now() - startTime > TIMEOUT_MS) {
|
||||||
|
console.warn('Approaching timeout, stopping processing');
|
||||||
|
break;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const invoiceData = await kv.get(key);
|
const invoiceData = await kv.get(key);
|
||||||
if (!invoiceData) continue;
|
if (!invoiceData) continue;
|
||||||
|
@ -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' });
|
|
||||||
}
|
|
||||||
}
|
|
@ -78,15 +78,6 @@ export default async function handler(req, res) {
|
|||||||
settled: false
|
settled: false
|
||||||
}, { ex: expiry || 86400 });
|
}, { 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
|
// Return response immediately
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
invoice,
|
invoice,
|
||||||
|
13
vercel.json
13
vercel.json
@ -3,16 +3,13 @@
|
|||||||
"crons": [
|
"crons": [
|
||||||
{
|
{
|
||||||
"path": "/api/users/subscription/cron",
|
"path": "/api/users/subscription/cron",
|
||||||
"schedule": "0 0 * * *"
|
"schedule": "0 0 * * *",
|
||||||
|
"authorization": "@CRON_SECRET"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "/api/invoices/polling",
|
"path": "/api/invoices/polling",
|
||||||
"schedule": "*/10 * * * *"
|
"schedule": "*/3 * * * *",
|
||||||
|
"authorization": "@PLEBDEVS_API_KEY"
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"functions": {
|
|
||||||
"src/pages/api/invoices/short-poll.js": {
|
|
||||||
"maxDuration": 60
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user