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 {