Skip to content

Commit

Permalink
Merge pull request #1269 from ParsePlatform/flovilmart.reportPushError
Browse files Browse the repository at this point in the history
Improves report for Push error in logs and _PushStatus
  • Loading branch information
gfosco committed Apr 1, 2016
2 parents c346e15 + 6055f2a commit 8c12f80
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
39 changes: 39 additions & 0 deletions spec/PushController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,45 @@ describe('PushController', () => {

});

it('should properly report failures in _PushStatus', (done) => {
var pushAdapter = {
send: function(body, installations) {
return installations.map((installation) => {
return Promise.resolve({
deviceType: installation.deviceType
})
})
},
getValidPushTypes: function() {
return ["ios"];
}
}
let where = { 'channels': {
'$ins': ['Giants', 'Mets']
}};
var payload = {data: {
alert: "Hello World!",
badge: 1,
}}
var config = new Config(Parse.applicationId);
var auth = {
isMaster: true
}
var pushController = new PushController(pushAdapter, Parse.applicationId);
pushController.sendPush(payload, where, config, auth).then(() => {
fail('should not succeed');
done();
}).catch(() => {
let query = new Parse.Query('_PushStatus');
query.find({useMasterKey: true}).then((results) => {
expect(results.length).toBe(1);
let pushStatus = results[0];
expect(pushStatus.get('status')).toBe('failed');
done();
});
})
});

it('should support full RESTQuery for increment', (done) => {
var payload = {data: {
alert: "Hello World!",
Expand Down
9 changes: 7 additions & 2 deletions src/Controllers/PushController.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class PushController extends AdaptableController {
let badgeUpdate = () => {
return Promise.resolve();
}

if (body.data && body.data.badge) {
let badge = body.data.badge;
let op = {};
Expand Down Expand Up @@ -89,10 +88,16 @@ export class PushController extends AdaptableController {
}).then(() => {
return rest.find(config, auth, '_Installation', where);
}).then((response) => {
pushStatus.setRunning();
if (!response.results) {
return Promise.reject({error: 'PushController: no results in query'})
}
pushStatus.setRunning(response.results);
return this.sendToAdapter(body, response.results, pushStatus, config);
}).then((results) => {
return pushStatus.complete(results);
}).catch((err) => {
pushStatus.fail(err);
return Promise.reject(err);
});
}

Expand Down
27 changes: 22 additions & 5 deletions src/pushStatusHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { md5Hash, newObjectId } from './cryptoUtils';
import { logger } from './logger';

export function flatten(array) {
return array.reduce((memo, element) => {
Expand All @@ -20,8 +21,9 @@ export default function pushStatusHandler(config) {
return config.database.adaptiveCollection('_PushStatus');
}

let setInitial = function(body, where, options = {source: 'rest'}) {
let setInitial = function(body = {}, where, options = {source: 'rest'}) {
let now = new Date();
let data = body.data || {};
let object = {
objectId: newObjectId(),
pushTime: now.toISOString(),
Expand All @@ -33,7 +35,7 @@ export default function pushStatusHandler(config) {
expiry: body.expiration_time,
status: "pending",
numSent: 0,
pushHash: md5Hash(JSON.stringify(body.data)),
pushHash: md5Hash(JSON.stringify(data)),
// lockdown!
_wperm: [],
_rperm: []
Expand All @@ -49,7 +51,8 @@ export default function pushStatusHandler(config) {
return initialPromise;
}

let setRunning = function() {
let setRunning = function(installations) {
logger.verbose('sending push to %d installations', installations.length);
return initialPromise.then(() => {
return collection();
}).then((collection) => {
Expand Down Expand Up @@ -86,17 +89,31 @@ export default function pushStatusHandler(config) {
return memo;
}, update);
}

logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed);
return initialPromise.then(() => {
return collection();
}).then((collection) => {
return collection.updateOne({status:"running", objectId: pushStatus.objectId}, {$set: update});
});
}

let fail = function(err) {
let update = {
errorMessage: JSON.stringify(err),
status: 'failed'
}
logger.error('error while sending push', err);
return initialPromise.then(() => {
return collection();
}).then((collection) => {
return collection.updateOne({objectId: pushStatus.objectId}, {$set: update});
});
}

return Object.freeze({
setInitial,
setRunning,
complete
complete,
fail
})
}

0 comments on commit 8c12f80

Please sign in to comment.