From 948afe28c873e4b8165ae25e46f26bf9c7ca7d9b Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Fri, 19 Aug 2022 14:25:10 +0200
Subject: [PATCH] request fetcher now waits for fpm to stop before shutting
 down

---
 metager/app/Console/Commands/FPMGracefulStop.php | 16 ++++++++++++++--
 metager/app/Console/Commands/RequestFetcher.php  |  6 +++---
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/metager/app/Console/Commands/FPMGracefulStop.php b/metager/app/Console/Commands/FPMGracefulStop.php
index 1d752bd8c..37626e1e5 100644
--- a/metager/app/Console/Commands/FPMGracefulStop.php
+++ b/metager/app/Console/Commands/FPMGracefulStop.php
@@ -2,10 +2,13 @@
 
 namespace App\Console\Commands;
 
+use ErrorException;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
 
 class FPMGracefulStop extends Command
 {
+    const REDIS_FPM_STOPPED_KEY = "fpm_stopped";
     /**
      * The name and signature of the console command.
      *
@@ -33,6 +36,11 @@ class FPMGracefulStop extends Command
         } while ($active_fpm_processes === null || $active_fpm_processes > 1);
 
         $this->info("Only one FPM process left. Ready to stop fpm...");
+        // The Request fetcher won't stop before FPM is not stopped with processing requests
+        // because there could be last jobs flying in
+        // This Redis Value will tell him that it's good to stop
+        Redis::set(self::REDIS_FPM_STOPPED_KEY, "true");
+        $this->info("Set Redis Key");
         return 0;
     }
 
@@ -51,8 +59,12 @@ class FPMGracefulStop extends Command
                 "header" => "Authorization: Basic $auth",
             ],
         ]);
-
-        $fpm_info = \file_get_contents($url, false, $context);
+        try {
+            $fpm_info = \file_get_contents($url, false, $context);
+        } catch (ErrorException $e) {
+            // Webserver could not be reached. Probably already shut down so there are no active connections anyways
+            return 0;
+        }
         if ($fpm_info !== false) {
             $fpm_info = \json_decode($fpm_info);
         } else {
diff --git a/metager/app/Console/Commands/RequestFetcher.php b/metager/app/Console/Commands/RequestFetcher.php
index 40c9eb310..b646cb6f4 100644
--- a/metager/app/Console/Commands/RequestFetcher.php
+++ b/metager/app/Console/Commands/RequestFetcher.php
@@ -84,7 +84,8 @@ class RequestFetcher extends Command
                 if ($newJobs === 0 && $answersRead === 0) {
                     usleep(10 * 1000);
                 }
-                if (!$this->shouldRun && $operationsRunning === 0) {
+
+                if (!$this->shouldRun && $operationsRunning === 0 && Redis::get(FPMGracefulStop::REDIS_FPM_STOPPED_KEY) !== NULL) {
                     break;
                 }
             }
@@ -234,8 +235,7 @@ class RequestFetcher extends Command
 
     public function sig_handler($sig)
     {
-        $this->info("Received Shutdown signal");
         $this->shouldRun = false;
-        echo ("Terminating Process\n");
+        $this->info("Terminating Process\n");
     }
 }
-- 
GitLab