Skip to content

Commit

Permalink
Merge pull request #344 from RocketChat/fix-db
Browse files Browse the repository at this point in the history
Fix stuck send worker on db error
  • Loading branch information
Morten N.O. Nørgaard Henriksen committed Oct 24, 2018
2 parents 1c42bc7 + 2d27b44 commit ef3323c
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 @@ -686,47 +686,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 ef3323c

Please sign in to comment.