Files
gameno-api/jobs/notificationRetryJob.js
T
kavehhn f04c797be6 Initial commit: teaching institution management API.
Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
2026-08-09 04:18:08 +02:00

46 lines
1.4 KiB
JavaScript

// /jobs/notificationRetryJob.js
const cron = require('node-cron');
const Notification = require('../components/notifications/notificationModel');
const { retryNotification } = require('../components/notifications/notificationService');
const logger = require('../utils/logger');
const runNotificationRetryJob = async () => {
try {
const failedNotifications = await Notification.find({
status: 'failed',
$expr: { $lt: ['$retryCount', '$maxRetries'] }
}).limit(50);
if (failedNotifications.length === 0) {
return;
}
logger.info(`[NotificationRetryJob] Found ${failedNotifications.length} failed notifications to retry...`);
for (const notification of failedNotifications) {
try {
await retryNotification(notification._id);
logger.info(`[NotificationRetryJob] Successfully retried notification ID: ${notification._id}`);
} catch (retryError) {
logger.warn(`[NotificationRetryJob] Retry failed for notification ID ${notification._id}: ${retryError.message}`);
}
}
} catch (error) {
logger.error(`[NotificationRetryJob ERROR]: ${error.message}`);
}
};
const startNotificationRetryJob = () => {
// Run every 5 minutes
cron.schedule('*/5 * * * *', async () => {
await runNotificationRetryJob();
});
logger.info('[NotificationRetryJob] Scheduled to run every 5 minutes.');
};
module.exports = {
startNotificationRetryJob,
runNotificationRetryJob
};