Make sure _PushStatus operations are run in order (#2367)

This commit is contained in:
Florent Vilmart
2016-07-23 21:20:04 +02:00
committed by Tyler Brock
parent 7964d0a999
commit 15a07859b0

View File

@@ -20,7 +20,7 @@ export default function pushStatusHandler(config) {
let pushStatus; let pushStatus;
let objectId = newObjectId(); let objectId = newObjectId();
let database = config.database; let database = config.database;
let lastPromise;
let setInitial = function(body = {}, where, options = {source: 'rest'}) { let setInitial = function(body = {}, where, options = {source: 'rest'}) {
let now = new Date(); let now = new Date();
let data = body.data || {}; let data = body.data || {};
@@ -41,19 +41,23 @@ export default function pushStatusHandler(config) {
ACL: {} ACL: {}
} }
return database.create(PUSH_STATUS_COLLECTION, object).then(() => { lastPromise = database.create(PUSH_STATUS_COLLECTION, object).then(() => {
pushStatus = { pushStatus = {
objectId objectId
}; };
return Promise.resolve(pushStatus); return Promise.resolve(pushStatus);
}); });
return lastPromise;
} }
let setRunning = function(installations) { let setRunning = function(installations) {
logger.verbose('sending push to %d installations', installations.length); logger.verbose('sending push to %d installations', installations.length);
return database.update(PUSH_STATUS_COLLECTION, lastPromise = lastPromise.then(() => {
{status:"pending", objectId: objectId}, return database.update(PUSH_STATUS_COLLECTION,
{status: "running", updatedAt: new Date() }); {status:"pending", objectId: objectId},
{status: "running", updatedAt: new Date() });
});
return lastPromise;
} }
let complete = function(results) { let complete = function(results) {
@@ -87,7 +91,10 @@ export default function pushStatusHandler(config) {
}, update); }, update);
} }
logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed); logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed);
return database.update(PUSH_STATUS_COLLECTION, {status:"running", objectId }, update); lastPromise = lastPromise.then(() => {
return database.update(PUSH_STATUS_COLLECTION, {status:"running", objectId }, update);
});
return lastPromise;
} }
let fail = function(err) { let fail = function(err) {
@@ -97,7 +104,10 @@ export default function pushStatusHandler(config) {
updatedAt: new Date() updatedAt: new Date()
} }
logger.info('warning: error while sending push', err); logger.info('warning: error while sending push', err);
return database.update(PUSH_STATUS_COLLECTION, { objectId }, update); lastPromise = lastPromise.then(() => {
return database.update(PUSH_STATUS_COLLECTION, { objectId }, update);
});
return lastPromise;
} }
return Object.freeze({ return Object.freeze({