dev1/backend/jobs/reminderCron.js

49 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// backend/jobs/reminderCron.js
import cron from 'node-cron';
import pool from '../config/mysqlPool.js';
import { sendSMS } from '../utils/smsService.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);
}
});