Skip to content

Commit

Permalink
fix: prevent send worker to stuck on db error
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Aug 1, 2018
1 parent 464d824 commit 2d27b44
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions lib/server/push.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2d27b44

Please sign in to comment.