Skip to content

Commit eed7f15

Browse files
flovilmartRafael Santos
authored and
Rafael Santos
committed
Make sure _PushStatus operations are run in order (parse-community#2367)
1 parent 6abba9c commit eed7f15

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/pushStatusHandler.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default function pushStatusHandler(config) {
2020
let pushStatus;
2121
let objectId = newObjectId();
2222
let database = config.database;
23-
23+
let lastPromise;
2424
let setInitial = function(body = {}, where, options = {source: 'rest'}) {
2525
let now = new Date();
2626
let data = body.data || {};
@@ -41,19 +41,23 @@ export default function pushStatusHandler(config) {
4141
ACL: {}
4242
}
4343

44-
return database.create(PUSH_STATUS_COLLECTION, object).then(() => {
44+
lastPromise = database.create(PUSH_STATUS_COLLECTION, object).then(() => {
4545
pushStatus = {
4646
objectId
4747
};
4848
return Promise.resolve(pushStatus);
4949
});
50+
return lastPromise;
5051
}
5152

5253
let setRunning = function(installations) {
5354
logger.verbose('sending push to %d installations', installations.length);
54-
return database.update(PUSH_STATUS_COLLECTION,
55-
{status:"pending", objectId: objectId},
56-
{status: "running", updatedAt: new Date() });
55+
lastPromise = lastPromise.then(() => {
56+
return database.update(PUSH_STATUS_COLLECTION,
57+
{status:"pending", objectId: objectId},
58+
{status: "running", updatedAt: new Date() });
59+
});
60+
return lastPromise;
5761
}
5862

5963
let complete = function(results) {
@@ -87,7 +91,10 @@ export default function pushStatusHandler(config) {
8791
}, update);
8892
}
8993
logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed);
90-
return database.update(PUSH_STATUS_COLLECTION, {status:"running", objectId }, update);
94+
lastPromise = lastPromise.then(() => {
95+
return database.update(PUSH_STATUS_COLLECTION, {status:"running", objectId }, update);
96+
});
97+
return lastPromise;
9198
}
9299

93100
let fail = function(err) {
@@ -97,7 +104,10 @@ export default function pushStatusHandler(config) {
97104
updatedAt: new Date()
98105
}
99106
logger.info('warning: error while sending push', err);
100-
return database.update(PUSH_STATUS_COLLECTION, { objectId }, update);
107+
lastPromise = lastPromise.then(() => {
108+
return database.update(PUSH_STATUS_COLLECTION, { objectId }, update);
109+
});
110+
return lastPromise;
101111
}
102112

103113
return Object.freeze({

0 commit comments

Comments
 (0)