From cde328eeedb7b8805fd73a9fa2976f0bef95d6a1 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 16 May 2017 08:56:12 +0200 Subject: [PATCH] CoreApi: add checkServiceAndWaitForResult() Will be moved to icingadb soon, committing this to clean up my stash --- library/Director/Core/CoreApi.php | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/library/Director/Core/CoreApi.php b/library/Director/Core/CoreApi.php index 7589a7c7..22c565eb 100644 --- a/library/Director/Core/CoreApi.php +++ b/library/Director/Core/CoreApi.php @@ -180,6 +180,40 @@ class CoreApi implements DeploymentApiInterface return false; } + public function checkServiceAndWaitForResult($host, $service, $timeout = 10) + { + $now = microtime(true); + $this->checkServiceNow($host, $service); + + while (true) { + try { + $object = $this->getObject("$host!$service", 'services'); + if (isset($object->attrs->last_check_result)) { + $res = $object->attrs->last_check_result; + if ($res->execution_start > $now) { + return $res; + } + } else { + // no check result available + } + } catch (Exception $e) { + // Unable to fetch the requested object + throw new IcingaException( + 'Unable to fetch the requested service "%s" on "%s"', + $service, + $host + ); + } + if (microtime(true) > ($now + $timeout)) { + break; + } + + usleep(150000); + } + + return false; + } + public function getServiceOutput($host, $service) { try {