From: Vincent Vanwaelscappel Date: Mon, 29 Jul 2024 17:50:04 +0000 (+0200) Subject: wip #7019 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=6bc749d4823fc0960a3b9d85c81839ab9cd3ca84;p=fluidbook-toolbox.git wip #7019 @2 --- diff --git a/app/Console/Commands/FluidbookFarmPing2.php b/app/Console/Commands/FluidbookFarmPing2.php new file mode 100644 index 000000000..53eed68de --- /dev/null +++ b/app/Console/Commands/FluidbookFarmPing2.php @@ -0,0 +1,19 @@ +option('force', false)); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4e92c7a5d..0dad1f2a4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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(); diff --git a/app/Fluidbook/Farm.php b/app/Fluidbook/Farm.php index 25562d277..5fc32e23b 100644 --- a/app/Fluidbook/Farm.php +++ b/app/Fluidbook/Farm.php @@ -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)); + } }