Express/MongoDB backend with JWT auth, RBAC, S3 uploads, notifications, and scheduled jobs.
46 lines
1.4 KiB
JavaScript
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
|
|
};
|