Skip to content

Commit

Permalink
Make sure _PushStatus operations are run in order (parse-community#2367)
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart authored and Rafael Santos committed Mar 15, 2017
1 parent 6abba9c commit eed7f15
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/pushStatusHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function pushStatusHandler(config) {
let pushStatus;
let objectId = newObjectId();
let database = config.database;

let lastPromise;
let setInitial = function(body = {}, where, options = {source: 'rest'}) {
let now = new Date();
let data = body.data || {};
Expand All @@ -41,19 +41,23 @@ export default function pushStatusHandler(config) {
ACL: {}
}

return database.create(PUSH_STATUS_COLLECTION, object).then(() => {
lastPromise = database.create(PUSH_STATUS_COLLECTION, object).then(() => {
pushStatus = {
objectId
};
return Promise.resolve(pushStatus);
});
return lastPromise;
}

let setRunning = function(installations) {
logger.verbose('sending push to %d installations', installations.length);
return database.update(PUSH_STATUS_COLLECTION,
{status:"pending", objectId: objectId},
{status: "running", updatedAt: new Date() });
lastPromise = lastPromise.then(() => {
return database.update(PUSH_STATUS_COLLECTION,
{status:"pending", objectId: objectId},
{status: "running", updatedAt: new Date() });
});
return lastPromise;
}

let complete = function(results) {
Expand Down Expand Up @@ -87,7 +91,10 @@ export default function pushStatusHandler(config) {
}, update);
}
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) {
Expand All @@ -97,7 +104,10 @@ export default function pushStatusHandler(config) {
updatedAt: new Date()
}
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({
Expand Down

0 comments on commit eed7f15

Please sign in to comment.