Created
October 22, 2016 11:25
-
-
Save talkol/c2d67646b0f594bf46ee0f138c2d1b56 to your computer and use it in GitHub Desktop.
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
import _ from 'lodash'; | |
import fetch from 'node-fetch'; | |
import delay from 'delay'; | |
export function pingServers(servers) { | |
return _.reduce(servers, (failedServersAccumulator, url) => { | |
return failedServersAccumulator.then((failedServers) => { | |
return _.reduce(_.range(3), (failuresAccumulator) => { | |
return failuresAccumulator.then(delay(10000)).then((failures) => { | |
return fetch(url).then((response) => { | |
return response.ok ? failures : failures + 1; | |
}); | |
}); | |
}, Promise.resolve(0)).then((failures) => { | |
if (failures > 0) failedServers[url] = failures; | |
return failedServers; | |
}); | |
}); | |
}, Promise.resolve({})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment