diff --git a/app/api/cron/push-scheduler/route.ts b/app/api/cron/push-scheduler/route.ts index 181134e..233846f 100644 --- a/app/api/cron/push-scheduler/route.ts +++ b/app/api/cron/push-scheduler/route.ts @@ -32,7 +32,7 @@ export async function GET(req: Request) { const isPushReady = initWebPush(); if (isPushReady) { - for (const push of scheduledPushes) { + await Promise.all(scheduledPushes.map(async (push: { id: string; topic: string; title: string; message: string; image_url: string }) => { // Pobierz odbiorców dla danego tematu let query = supabase.from('push_subscriptions').select('subscription_data'); if (push.topic !== 'wszystkie') { @@ -55,12 +55,15 @@ export async function GET(req: Request) { webpush.sendNotification(s.subscription_data, payload).catch(() => null) )); } + })); - // Zmień status na wysłane + // Zmień status na wysłane + const pushIds = scheduledPushes.map((p: { id: string }) => p.id); + if (pushIds.length > 0) { await supabase .from('push_history') .update({ status: 'sent', created_at: now }) - .eq('id', push.id); + .in('id', pushIds); } } else { console.warn('[CRON] Pominięto wysyłkę zaplanowanych powiadomień - brak konfiguracji VAPID.'); diff --git a/test_perf.js b/test_perf.js new file mode 100644 index 0000000..d3c9878 --- /dev/null +++ b/test_perf.js @@ -0,0 +1,34 @@ +const performance = require('perf_hooks').performance; + +async function mockQuery(delayMs) { + return new Promise(resolve => setTimeout(resolve, delayMs)); +} + +async function sequential(pushes) { + const start = performance.now(); + for (const push of pushes) { + await mockQuery(50); // mock query subs + await mockQuery(50); // mock update status + } + return performance.now() - start; +} + +async function optimized(pushes) { + const start = performance.now(); + await Promise.all(pushes.map(async (push) => { + await mockQuery(50); // mock query subs + })); + // mock bulk update + await mockQuery(50); + return performance.now() - start; +} + +async function run() { + const pushes = new Array(10).fill({}); + const seqTime = await sequential(pushes); + const optTime = await optimized(pushes); + console.log(`Sequential time: ${seqTime.toFixed(2)} ms`); + console.log(`Optimized time: ${optTime.toFixed(2)} ms`); +} + +run();