Skip to content

Commit ef3323c

Browse files
author
Morten N.O. Nørgaard Henriksen
authored
Merge pull request #344 from RocketChat/fix-db
Fix stuck send worker on db error
2 parents 1c42bc7 + 2d27b44 commit ef3323c

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

lib/server/push.api.js

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -686,47 +686,52 @@ Push.Configure = function(options) {
686686
if (isSendingNotification) {
687687
return;
688688
}
689-
// Set send fence
690-
isSendingNotification = true;
691-
692-
// var countSent = 0;
693-
var batchSize = options.sendBatchSize || 1;
694-
695-
var now = +new Date();
696-
697-
// Find notifications that are not being or already sent
698-
var pendingNotifications = Push.notifications.find({ $and: [
699-
// Message is not sent
700-
{ sent : false },
701-
// And not being sent by other instances
702-
{ sending: { $lt: now } },
703-
// And not queued for future
704-
{ $or: [
705-
{ delayUntil: { $exists: false } },
706-
{ delayUntil: { $lte: new Date() } }
707-
]
708-
}
709-
]}, {
710-
// Sort by created date
711-
sort: { createdAt: 1 },
712-
limit: batchSize
713-
});
714689

715-
pendingNotifications.forEach(function(notification) {
716-
try {
717-
sendNotification(notification);
718-
} catch(error) {
719-
if (typeof Push.Log === 'function') {
720-
Push.Log('Push: Could not send notification id: "' + notification._id + '", Error:', error.message);
721-
}
722-
if (Push.debug) {
723-
console.log('Push: Could not send notification id: "' + notification._id + '", Error: ' + error.message);
690+
try {
691+
692+
// Set send fence
693+
isSendingNotification = true;
694+
695+
// var countSent = 0;
696+
var batchSize = options.sendBatchSize || 1;
697+
698+
var now = +new Date();
699+
700+
// Find notifications that are not being or already sent
701+
var pendingNotifications = Push.notifications.find({ $and: [
702+
// Message is not sent
703+
{ sent : false },
704+
// And not being sent by other instances
705+
{ sending: { $lt: now } },
706+
// And not queued for future
707+
{ $or: [
708+
{ delayUntil: { $exists: false } },
709+
{ delayUntil: { $lte: new Date() } }
710+
]
711+
}
712+
]}, {
713+
// Sort by created date
714+
sort: { createdAt: 1 },
715+
limit: batchSize
716+
});
717+
718+
pendingNotifications.forEach(function(notification) {
719+
try {
720+
sendNotification(notification);
721+
} catch(error) {
722+
if (typeof Push.Log === 'function') {
723+
Push.Log('Push: Could not send notification id: "' + notification._id + '", Error:', error.message);
724+
}
725+
if (Push.debug) {
726+
console.log('Push: Could not send notification id: "' + notification._id + '", Error: ' + error.message);
727+
}
724728
}
725-
}
726-
}); // EO forEach
729+
}); // EO forEach
730+
} finally {
727731

728-
// Remove the send fence
729-
isSendingNotification = false;
732+
// Remove the send fence
733+
isSendingNotification = false;
734+
}
730735
}, options.sendInterval || 15000); // Default every 15th sec
731736

732737
} else {

0 commit comments

Comments
 (0)