// backend/jobs/reminderCron.js import cron from 'node-cron'; import pool from '../config/mysqlPool.js'; import { sendSMS } from '../utils/smsService.js'; import { query } from '../shared/db/withEncryption.js'; const BATCH_SIZE = 25; // tune as you like /* Every minute */ cron.schedule('*/1 * * * *', async () => { try { /* 1️⃣ Fetch at most BATCH_SIZE reminders that are due */ const [rows] = await pool.query( `SELECT id, phone_e164 AS toNumber, message_body AS body FROM reminders WHERE status = 'pending' AND send_at_utc <= UTC_TIMESTAMP() ORDER BY send_at_utc ASC LIMIT ?`, [BATCH_SIZE] ); if (!rows.length) return; // nothing to do let sent = 0, failed = 0; /* 2️⃣ Fire off each SMS (sendSMS handles its own DB status update) */ for (const r of rows) { try { await sendSMS({ // ← updated signature reminderId: r.id, to : r.toNumber, body : r.body }); sent++; } catch (err) { console.error('[reminderCron] Twilio error:', err?.message || err); failed++; /* sendSMS already logged the failure + updated status */ } } console.log(`[reminderCron] processed ${rows.length}: ${sent} sent, ${failed} failed`); } catch (err) { console.error('[reminderCron] DB error:', err); } });