Created
December 2, 2020 08:44
-
-
Save flangofas/32f2c4db18bd9f7059e42c5dacac056b to your computer and use it in GitHub Desktop.
Health check monitor on URL, it times out when throttle time is being expired. [VANILLA PHP]
This file contains hidden or 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
<?php | |
const HEALTH_CHECK_INTERVAL_IN_SECONDS = '15'; | |
const HEALTH_CHECK_THROTTLE = '10 minutes'; | |
if (!isset($argv[1])) { | |
throw new InvalidArgumentException('Please provide one argument in JSON format'); | |
} | |
$data = json_decode($argv[1]); | |
if (json_last_error() !== JSON_ERROR_NONE) { | |
throw new InvalidArgumentException('JSON malformed'); | |
} | |
if (!isset($data->name)) { | |
throw new InvalidArgumentException('Missing blockchain name'); | |
} | |
$url = sprintf('https://%s.app.alleochain.io', $data->name); | |
$endOfHealthCheckTime = new DateTimeImmutable('+' . HEALTH_CHECK_THROTTLE); | |
while (true) { | |
$currentTime = new DateTimeImmutable(); | |
if ($endOfHealthCheckTime <= $currentTime) { | |
throw new RuntimeException('Blockchain heath check failed on ' . $url); | |
} | |
$resource = curl_init(); | |
curl_setopt($resource, CURLOPT_URL, $url); | |
# Should be a head request | |
curl_setopt($resource, CURLOPT_NOBODY, true); | |
# Have the response back | |
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true); | |
# Keep HEADERS in the response | |
curl_setopt($resource, CURLOPT_HEADER, true); | |
curl_exec($resource); | |
if (curl_getinfo($resource, CURLINFO_HTTP_CODE) === 200) { | |
exit(0); | |
} | |
curl_close($resource); | |
sleep(HEALTH_CHECK_INTERVAL_IN_SECONDS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment