]> _ Git - fluidbook-toolbox.git/commitdiff
wip #7019 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 29 Jul 2024 17:50:04 +0000 (19:50 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 29 Jul 2024 17:50:04 +0000 (19:50 +0200)
app/Console/Commands/FluidbookFarmPing2.php [new file with mode: 0644]
app/Console/Kernel.php
app/Fluidbook/Farm.php

diff --git a/app/Console/Commands/FluidbookFarmPing2.php b/app/Console/Commands/FluidbookFarmPing2.php
new file mode 100644 (file)
index 0000000..53eed68
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+
+namespace App\Console\Commands;
+
+use App\Console\Commands\Base\ToolboxCommand;
+use App\Fluidbook\Farm;
+
+
+class FluidbookFarmPing2 extends ToolboxCommand
+{
+    protected $signature = 'fluidbook:farm:ping2 {--force}';
+    protected $description = 'Ping servers';
+
+    public function handle()
+    {
+        Farm::ping2(true, $this->option('force', false));
+    }
+}
index 4e92c7a5dd13039e8f11dfe3e567e62380dc938f..0dad1f2a416cc059de17a45ad9654c114216a315 100644 (file)
@@ -52,6 +52,7 @@ class Kernel extends \Cubist\Backpack\Console\Kernel
         $schedule->command('job:dispatchNow \\\\Cubedesigners\\\\UserDatabase\\\\Jobs\\\\ApplyPermissionsToUsers')->everyFifteenMinutes();
 
         $schedule->command('fluidbook:farm:ping --force')->everyMinute();
+        $schedule->command('fluidbook:farm:ping2 --force')->everyMinute();
         $schedule->command('fluidbook:loadbalancer:ping --force')->everyMinute();
         $schedule->command('cubist:magic:precache')->everyThirtyMinutes();
         $schedule->command('toolbox:precache')->everyThirtyMinutes();
index 25562d2773d0bd3c101373044edf33caf70e709f..5fc32e23bbbbc6370700e455a52689ebc69fe576 100644 (file)
@@ -58,6 +58,11 @@ class Farm
         return Files::mkdir(storage_path('fluidbookfarm')) . '/pings';
     }
 
+    protected static function _ping2Cache()
+    {
+        return Files::mkdir(storage_path('fluidbookfarm')) . '/ping2';
+    }
+
     protected static function _serversCache()
     {
         return Files::mkdir(storage_path('fluidbookfarm')) . '/servers';
@@ -68,7 +73,7 @@ class Farm
         return self::$_farmServers;
     }
 
-    public static function pickOneServer($preferLocal)
+    public static function pickOneServer($preferLocal,$params)
     {
         $hat = [];
         $pingCache = self::_pingCache();
@@ -293,7 +298,7 @@ class Farm
     protected static function _getFile($params, $attempts = 3, $checkOutput = true, $preferLocal = false)
     {
         $start = microtime(true);
-        $farmer = self::pickOneServer($preferLocal);
+        $farmer = self::pickOneServer($preferLocal, $params);
 
         $params['toolbox'] = '1';
 
@@ -384,6 +389,50 @@ class Farm
         file_put_contents($cache, json_encode($pings));
         file_put_contents(self::_serversCache(), json_encode($servers));
     }
+
+    public static function ping2($echo = true, $force = false)
+    {
+        $cache = self::_ping2Cache();
+        $servers = self::getServers();
+        $pings = [];
+        if (file_exists($cache)) {
+            $cached = json_decode(file_get_contents($cache));
+            if (is_countable($cached) && count($cached) === count($servers)) {
+                $pings = $cached;
+            }
+        }
+
+        foreach ($servers as $id => $farmer) {
+            if ($echo) {
+                echo $farmer['name'] . ' (' . $id . ') || ';
+            }
+            if (isset($pings[$id]) && !$pings[$id]) {
+                // If ping failed recently, we wait a bit before trying again.
+                if (!$force && rand(0, 9) != 5) {
+                    if ($echo) {
+                        echo 'Skipped, will try again soon' . "\n";
+                    }
+                    continue;
+                }
+            }
+            try {
+                $res = self::sendRequest($farmer, 'ping2.php', [], 5);
+                $res = json_decode($res, true);
+                $ok = $res['status'] == "1";
+            } catch (\Exception $e) {
+                $res = $e->getMessage();
+                $ok = false;
+            }
+
+            if ($echo) {
+                echo ($ok ? 'OK' : 'KO') . ' : ' . json_encode($res) . "\n";
+            }
+
+            $pings[$id] = $res;
+        }
+        file_put_contents($cache, json_encode($pings));
+        file_put_contents(self::_serversCache(), json_encode($servers));
+    }
 }