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

V1 no send no retry #248

Merged
merged 22 commits into from
Jun 9, 2016
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
22a07f3
Remove sendNoRetry from being exposed
hypesystem Jun 1, 2016
3d8b4f6
Rename sendNoRetry -> sendMessage
hypesystem Jun 1, 2016
81c27c3
Remove unnecessary support for no callback (always called with callba…
hypesystem Jun 1, 2016
3d18769
Rewrite sendNoRetry tests to use send with retries: 0
hypesystem Jun 2, 2016
0e6d133
Extract sendMessageWithRetries function
hypesystem Jun 2, 2016
008e2fd
Modify retry to use sendMessageWithRetries
hypesystem Jun 2, 2016
cf7b494
Added a note about a case that seems odd
hypesystem Jun 2, 2016
0236fbf
Remove self argument from retry and sendMessageWithRetries
hypesystem Jun 2, 2016
d125924
Get request body early on, simply pass it through
hypesystem Jun 2, 2016
d4eec16
fix indentation
hypesystem Jun 2, 2016
d174ce7
Build requestOptions early and pass around (instead of building for e…
hypesystem Jun 2, 2016
e177346
Reorder functions
hypesystem Jun 2, 2016
c558a7c
Remove an irrelevant return
hypesystem Jun 2, 2016
7a0e08e
Removed redundant tests (now covered by send with retries: 0)
hypesystem Jun 2, 2016
e0a6969
Dont try to mock sendNoRetry
hypesystem Jun 2, 2016
e8dd0ff
Removed some more redundant tests
hypesystem Jun 2, 2016
9c32fb2
Make all send specs async
hypesystem Jun 2, 2016
79873f8
Break out of retry if there are no retries left!
hypesystem Jun 8, 2016
20fad62
Remove this one weird case that is good for naught
hypesystem Jun 8, 2016
41b2d94
Make send() tests work.
hypesystem Jun 8, 2016
a1343eb
merge two tests, so the same on now tests sender options and auth key
hypesystem Jun 8, 2016
7e55699
Remove two tests that no longer pass (and shouldnt!)
hypesystem Jun 8, 2016
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
Prev Previous commit
Next Next commit
Get request body early on, simply pass it through
  • Loading branch information
hypesystem committed Jun 2, 2016
commit d125924554f695cae19c7d4b1ce20444c12668fc
30 changes: 15 additions & 15 deletions lib/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ Sender.prototype.send = function(message, recipient, options, callback) {
}
options = cleanOptions(options);

getRequestBody(message, recipient, function(err, body) {
if(err) {
return callback(err);
}
if(options.retries == 0) {
return sendMessage(this.key, this.options, message, recipient, callback);
return sendMessage(this.key, this.options, body, callback);
}

sendMessageWithRetries(this.key, this.options, message, recipient, options, callback);
sendMessageWithRetries(this.key, this.options, body, options, callback);
}.bind(this));
};

function cleanOptions(options) {
Expand Down Expand Up @@ -55,14 +60,14 @@ function cleanOptions(options) {
return options;
}

function sendMessageWithRetries(key, senderOptions, message, recipient, messageOptions, callback) {
sendMessage(key, senderOptions, message, recipient, function(err, response, attemptedRegTokens) {
function sendMessageWithRetries(key, senderOptions, body, messageOptions, callback) {
sendMessage(key, senderOptions, body, function(err, response, attemptedRegTokens) {
if (err) {
if (typeof err === 'number' && err > 399 && err < 500) {
debug("Error 4xx -- no use retrying. Something is wrong with the request (probably authentication?)");
return callback(err);
}
return retry(key, senderOptions, message, recipient, messageOptions, callback);
return retry(key, senderOptions, body, messageOptions, callback);
}
//TODO: Figure out why this case exists -- should it be removed?
if(!response.results) {
Expand All @@ -78,7 +83,8 @@ function sendMessageWithRetries(key, senderOptions, message, recipient, messageO

debug("Retrying " + unsentRegTokens.length + " unsent registration tokens");

retry(key, senderOptions, message, unsentRegTokens, messageOptions, function(err, retriedResponse) {
body.registration_ids = unsentRegTokens;
retry(key, senderOptions, body, messageOptions, function(err, retriedResponse) {
if(err) {
return callback(null, response);
}
Expand All @@ -89,9 +95,9 @@ function sendMessageWithRetries(key, senderOptions, message, recipient, messageO
});
}

function retry(key, senderOptions, message, recipient, messageOptions, callback) {
function retry(key, senderOptions, body, messageOptions, callback) {
return setTimeout(function() {
sendMessageWithRetries(key, senderOptions, message, recipient, {
sendMessageWithRetries(key, senderOptions, body, {
retries: messageOptions.retries - 1,
backoff: messageOptions.backoff * 2
}, callback);
Expand Down Expand Up @@ -128,12 +134,7 @@ function updateResponseMetaData(response, retriedResponse, unsentRegTokens) {
response.failure -= unsentRegTokens.length - retriedResponse.failure;
}

function sendMessage(key, options, message, recipient, callback) {
getRequestBody(message, recipient, function(err, body) {
if(err) {
return callback(err);
}

function sendMessage(key, options, body, callback) {
//Build request options, allowing some to be overridden
var request_options = defaultsDeep({
method: 'POST',
Expand Down Expand Up @@ -164,7 +165,6 @@ function sendMessage(key, options, message, recipient, callback) {
}
callback(null, resBodyJSON, body.registration_ids || [ body.to ]);
});
});
}

function getRequestBody(message, recipient, callback) {
Expand Down