]> _ Git - fluidbook-toolbox.git/commitdiff
wip #6269 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Sep 2023 16:15:04 +0000 (18:15 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 14 Sep 2023 16:15:04 +0000 (18:15 +0200)
app/Console/Commands/FluidbookLoadbalancerPing.php [new file with mode: 0644]
app/Console/Kernel.php
app/Fluidbook/HostingLoadBalancer.php

diff --git a/app/Console/Commands/FluidbookLoadbalancerPing.php b/app/Console/Commands/FluidbookLoadbalancerPing.php
new file mode 100644 (file)
index 0000000..0ced4cb
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+
+namespace App\Console\Commands;
+
+use App\Fluidbook\Farm;
+use App\Fluidbook\HostingLoadBalancer;
+use Cubist\Backpack\Console\Commands\CubistCommand;
+
+
+class FluidbookLoadbalancerPing extends CubistCommand
+{
+    protected $signature = 'fluidbook:loadbalancer:ping {--force}';
+    protected $description = 'Manage fluidbook loadbalanceer';
+
+    public function handle()
+    {
+        HostingLoadBalancer::ping(true, $this->option('force', false));
+    }
+}
index 425e48c6d02754b9cffa14bfe04c58b9e699a40d..6e4f2f6c3a45bb4d7f8dc37cde1e3c619ed9321e 100644 (file)
@@ -45,6 +45,7 @@ class Kernel extends \Cubist\Backpack\Console\Kernel
         $schedule->command('job:dispatchNow \\\\Cubedesigners\\\\UserDatabase\\\\Jobs\\\\ApplyPermissionsToUsers')->everyTwoHours();
 
         $schedule->command('fluidbook:farm:ping')->everyMinute();
+        $schedule->command('fluidbook:loadbalancer:ping')->everyMinute();
         $schedule->command('cubist:magic:precache')->everyFiveMinutes();
         $schedule->command('toolbox:precache')->everyThirtyMinutes();
     }
index 7b6515522e8d6ae8b11ba5a1b89648d50ae2fc5e..71bc3f7c7f10c837b3e10708517c6f53795e63dc 100644 (file)
@@ -2,10 +2,75 @@
 
 namespace App\Fluidbook;
 
+use Cubist\Util\Files\Files;
+
 class HostingLoadBalancer
 {
     protected static $_servers = [
-        ['name' => 's1', 'host' => 's1.lb.hosting.fluidbook.com'],
-        ['name' => 's2', 'host' => 's2.lb.hosting.fluidbook.com'],
+        ['name' => 's1', 'host' => 's1.lb.fluidbook.com'],
+        ['name' => 's2', 'host' => 's2.lb.fluidbook.com'],
     ];
+
+    protected static function _pingCache()
+    {
+        return Files::mkdir(storage_path('fluidbookhostingloadbalancer')) . '/pings';
+    }
+
+    public static function getServers()
+    {
+        return self::$_servers;
+    }
+
+    public static function ping($echo = true, $force = false)
+    {
+        $cache = self::_pingCache();
+        $servers = self::getServers();
+        $available = [];
+        $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 => $server) {
+            if ($echo) {
+                echo $server['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 = file_get_contents('https://' . $server['host'] . '/status');
+                $ok = $res == '1';
+            } catch (\Exception $e) {
+                $res = $e->getMessage();
+                $ok = false;
+            }
+
+            if ($echo) {
+                echo ($ok ? 'OK' : 'KO') . ' : ' . trim($res) . "\n";
+            }
+
+            $pings[$id] = $ok;
+            if ($ok) {
+                $available[] = $server['host'];
+            }
+        }
+        file_put_contents($cache, json_encode($pings));
+        file_put_contents(self::_serversCache(), json_encode($servers));
+        file_put_contents(public_path('lb.json'), json_encode($available));
+    }
+
+    protected static function _serversCache()
+    {
+        return Files::mkdir(storage_path('fluidbookhostingloadbalancer')) . '/servers';
+    }
 }