From 5802212dd8d56d61ea2cf518fdc64d32ed5b9813 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Sep 2021 16:08:49 +0545 Subject: [PATCH] fix usage daemon first time startup --- app/tasks/usage.php | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app/tasks/usage.php b/app/tasks/usage.php index e66bec113f..4ff5e8cad9 100644 --- a/app/tasks/usage.php +++ b/app/tasks/usage.php @@ -238,10 +238,27 @@ $cli /** * Aggregate InfluxDB every 30 seconds + * @var InfluxDB\Client $client */ $client = $register->get('influxdb'); if ($client) { + $attempts = 0; + $max = 10; + $sleep = 1; + $database = $client->selectDB('telegraf'); + do { // check if telegraf database is ready + $attempts++; + if(!in_array('telegraf', $client->listDatabases())) { + Console::warning("InfluxDB not ready. Retrying connection ({$attempts})..."); + if($attempts >= $max) { + throw new \Exception('InfluxDB database not ready yet'); + } + sleep($sleep); + } else { + break; // leave the do-while if successful + } + } while ($attempts < $max); // sync data foreach ($globalMetrics as $metric => $options) { //for each metrics @@ -318,7 +335,23 @@ $cli $latestProject = null; do { // Loop over all the projects - $projects = $dbForConsole->find('projects', [], 100, orderAfter:$latestProject); + $attempts = 0; + $max = 10; + $sleep = 1; + + do { // list projects + try { + $attempts++; + $projects = $dbForConsole->find('projects', [], 100, orderAfter:$latestProject); + break; // leave the do-while if successful + } catch (\Exception $e) { + Console::warning("Console DB not ready yet. Retrying ({$attempts})..."); + if ($attempts >= $max) { + throw new \Exception('Failed access console db: ' . $e->getMessage()); + } + sleep($sleep); + } + } while ($attempts < $max); if (empty($projects)) { continue;