Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure _PushStatus operations are run in order #2367

Merged
merged 1 commit into from
Jul 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace could be nicer here, like the line below

{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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add space before status:, while we are here

});
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