From 2d27b442a379b29085031bfad56b2f2501a69f75 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Tue, 31 Jul 2018 14:50:56 -0300 Subject: [PATCH] fix: prevent send worker to stuck on db error --- lib/server/push.api.js | 81 ++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/lib/server/push.api.js b/lib/server/push.api.js index 5f901d4..12064fe 100644 --- a/lib/server/push.api.js +++ b/lib/server/push.api.js @@ -673,47 +673,52 @@ Push.Configure = function(options) { if (isSendingNotification) { return; } - // Set send fence - isSendingNotification = true; - - // var countSent = 0; - var batchSize = options.sendBatchSize || 1; - - var now = +new Date(); - - // Find notifications that are not being or already sent - var pendingNotifications = Push.notifications.find({ $and: [ - // Message is not sent - { sent : false }, - // And not being sent by other instances - { sending: { $lt: now } }, - // And not queued for future - { $or: [ - { delayUntil: { $exists: false } }, - { delayUntil: { $lte: new Date() } } - ] - } - ]}, { - // Sort by created date - sort: { createdAt: 1 }, - limit: batchSize - }); - pendingNotifications.forEach(function(notification) { - try { - sendNotification(notification); - } catch(error) { - if (typeof Push.Log === 'function') { - Push.Log('Push: Could not send notification id: "' + notification._id + '", Error:', error.message); - } - if (Push.debug) { - console.log('Push: Could not send notification id: "' + notification._id + '", Error: ' + error.message); + try { + + // Set send fence + isSendingNotification = true; + + // var countSent = 0; + var batchSize = options.sendBatchSize || 1; + + var now = +new Date(); + + // Find notifications that are not being or already sent + var pendingNotifications = Push.notifications.find({ $and: [ + // Message is not sent + { sent : false }, + // And not being sent by other instances + { sending: { $lt: now } }, + // And not queued for future + { $or: [ + { delayUntil: { $exists: false } }, + { delayUntil: { $lte: new Date() } } + ] + } + ]}, { + // Sort by created date + sort: { createdAt: 1 }, + limit: batchSize + }); + + pendingNotifications.forEach(function(notification) { + try { + sendNotification(notification); + } catch(error) { + if (typeof Push.Log === 'function') { + Push.Log('Push: Could not send notification id: "' + notification._id + '", Error:', error.message); + } + if (Push.debug) { + console.log('Push: Could not send notification id: "' + notification._id + '", Error: ' + error.message); + } } - } - }); // EO forEach + }); // EO forEach + } finally { - // Remove the send fence - isSendingNotification = false; + // Remove the send fence + isSendingNotification = false; + } }, options.sendInterval || 15000); // Default every 15th sec } else {