From 32129f5164cba605bc4902a90ddadc1018620337 Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Thu, 20 Feb 2020 14:38:15 +0100
Subject: [PATCH] added liveness and readiness for fetcher

---
 app/Console/Commands/RequestFetcher.php | 10 ++++++++--
 chart/templates/deployment.yaml         | 14 ++++++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/app/Console/Commands/RequestFetcher.php b/app/Console/Commands/RequestFetcher.php
index f932b92b0..f6305c391 100644
--- a/app/Console/Commands/RequestFetcher.php
+++ b/app/Console/Commands/RequestFetcher.php
@@ -50,10 +50,17 @@ class RequestFetcher extends Command
      */
     public function handle()
     {
+        $pidFile = "/tmp/fetcher";
         pcntl_signal(SIGINT, [$this, "sig_handler"]);
         pcntl_signal(SIGTERM, [$this, "sig_handler"]);
         pcntl_signal(SIGHUP, [$this, "sig_handler"]);
 
+        touch($pidFile);
+
+        if (!file_exists($pidFile)) {
+            return;
+        }
+
         try {
             $blocking = false;
             $redis = Redis::connection("cache");
@@ -108,9 +115,8 @@ class RequestFetcher extends Command
                     usleep(50 * 1000);
                 }
             }
-        } catch (\Exception $e) {
-            Log::error($e->getMessage());
         } finally {
+            unlink($pidFile);
             curl_multi_close($this->multicurl);
         }
     }
diff --git a/chart/templates/deployment.yaml b/chart/templates/deployment.yaml
index f65ea5f47..977aee9cd 100644
--- a/chart/templates/deployment.yaml
+++ b/chart/templates/deployment.yaml
@@ -141,4 +141,18 @@ spec:
         command: ["su"]
         args: ["-s", "/bin/sh", "-c", "php artisan requests:fetcher", "nginx"]
         imagePullPolicy: {{ .Values.image.pullPolicy }}
+        livenessProbe:
+          exec:
+            command:
+            - cat
+            - /tmp/fetcher
+          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
+          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
+        readinessProbe:
+          exec:
+            command:
+            - cat
+            - /tmp/fetcher
+          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
+          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
 {{- end -}}
-- 
GitLab