-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from spidasoftware/retry-timedout-requests-1662…
…14942 [Delivers #166214942] Retry failed requests
- Loading branch information
Showing
4 changed files
with
84 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const Promise = require('bluebird'); | ||
const request = Promise.promisify(require('request')); | ||
|
||
const REQUEST_RETRY_COUNT = 5; | ||
const RETRY_TIMEOUT_MS = 5000; | ||
const REQUEST_TIMEOUT_MS = 60000; | ||
|
||
function rejectDelay() { | ||
return new Promise(function(resolve, reject) { | ||
setTimeout(resolve, RETRY_TIMEOUT_MS); | ||
}) | ||
} | ||
|
||
function requestAndRetry(requestParams, responseHandler = (response) => response, retryCount = 0){ | ||
requestParams.timeout = REQUEST_TIMEOUT_MS; | ||
return request(requestParams).then(responseHandler).catch((err) => { | ||
if(retryCount < REQUEST_RETRY_COUNT){ | ||
console.log(`Could not complete last request. Retrying: ${retryCount+1}/${REQUEST_RETRY_COUNT}`) | ||
return rejectDelay().then(() => requestAndRetry(requestParams, responseHandler, ++retryCount)); | ||
} else { | ||
console.log("Could not complete action."); | ||
throw err | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
requestAndRetry | ||
}; |