From: Vincent Vanwaelscappel Date: Thu, 14 Sep 2023 16:15:04 +0000 (+0200) Subject: wip #6269 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=d365a89ffb208762ff459c9a2d9278b8a4cc254b;p=fluidbook-toolbox.git wip #6269 @0.5 --- diff --git a/app/Console/Commands/FluidbookLoadbalancerPing.php b/app/Console/Commands/FluidbookLoadbalancerPing.php new file mode 100644 index 000000000..0ced4cbc0 --- /dev/null +++ b/app/Console/Commands/FluidbookLoadbalancerPing.php @@ -0,0 +1,20 @@ +option('force', false)); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 425e48c6d..6e4f2f6c3 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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(); } diff --git a/app/Fluidbook/HostingLoadBalancer.php b/app/Fluidbook/HostingLoadBalancer.php index 7b6515522..71bc3f7c7 100644 --- a/app/Fluidbook/HostingLoadBalancer.php +++ b/app/Fluidbook/HostingLoadBalancer.php @@ -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'; + } }