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';
return self::$_farmServers;
}
- public static function pickOneServer($preferLocal)
+ public static function pickOneServer($preferLocal,$params)
{
$hat = [];
$pingCache = self::_pingCache();
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';
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));
+ }
}