namespace App\Util;
-use Cubist\Util\CommandLine;
use Cubist\Util\Files\Files;
+use hollodotme\FastCGI\Client;
+use hollodotme\FastCGI\Requests\PostRequest;
+use hollodotme\FastCGI\SocketConnections\NetworkSocket;
class FluidbookFarm
{
- const KEY = '/application/.ssh/id_rsa';
-
protected static $_farmServers = [
- ['name' => 'elephantman', 'host' => 'elephantman.cubedesigners.com', 'port' => 22822, 'weight' => 1],
- ['name' => 'fastandfurious', 'host' => 'fastandfurious.cubedesigners.com', 'port' => 22822, 'weight' => 1],
- ['name' => 'vmparis', 'host' => 'paris.cubedesigners.com', 'port' => 22922, 'weight' => 2],
- ['name' => 'vincent', 'host' => 'paris.cubedesigners.com', 'port' => 22923, 'weight' => 1],
- ['name' => 'godzilla', 'host' => 'godzilla.cubedesigners.com', 'weight' => 3],
+ ['name' => 'alien', 'host' => 'alien.cubedesigners.com', 'weight' => 1],
+ ['name' => 'brazil', 'host' => 'brazil.cubedesigners.com', 'weight' => 6],
+ //['name' => 'clockwork', 'host' => 'clockwork.cubedesigners.com', 'weight' => 2],
['name' => 'dracula', 'host' => 'dracula.cubedesigners.com', 'weight' => 3],
+ ['name' => 'elephantman', 'host' => 'elephantman.cubedesigners.com', 'weight' => 1],
+ ['name' => 'fastandfurious', 'host' => 'fastandfurious.cubedesigners.com', 'weight' => 1],
+ ['name' => 'godzilla', 'host' => 'godzilla.cubedesigners.com', 'weight' => 3],
['name' => 'her', 'host' => 'her2.cubedesigners.com', 'weight' => 4],
- ['name' => 'brazil', 'host' => 'brazil.cubedesigners.com', 'weight' =>6],
+ ['name' => 'isleofdogs', 'host' => 'paris.cubedesigners.com', 'port' => 9458, 'weight' => 2],
+ ['name' => 'jumanji', 'host' => 'paris.cubedesigners.com', 'port' => 9459, 'weight' => 2],
];
protected static function _pingCache()
return Files::mkdir(storage_path('fluidbookfarm')) . '/pings';
}
+ protected static function _serversCache()
+ {
+ return Files::mkdir(storage_path('fluidbookfarm')) . '/servers';
+ }
+
public static function getServers()
{
return self::$_farmServers;
}
$pings = json_decode(file_get_contents(self::_pingCache()));
+
foreach (self::$_farmServers as $k => $farmServer) {
if (!isset($pings[$k]) || !$pings[$k]) {
continue;
return self::$_farmServers[$i];
}
+ public static function getFCGIConnexion(array $farm, $timeout = 30): NetworkSocket
+ {
+ $timeout *= 1000;
+ return new NetworkSocket($farm['host'], $farm['port'] ?? 9457, $timeout, $timeout);
+ }
+
+ public static function sendRequest($farmer, $url, $params = [], $timeout = 30)
+ {
+ $client = new Client();
+ $response = $client->sendRequest(self::getFCGIConnexion($farmer, $timeout), new PostRequest($url, http_build_query($params)));
+ return trim($response->getBody());
+ }
+
public static function getFile($page, $format, $resolution, $withText, $withGraphics, $version, $resolutionRatio, $mobileFirstRatio, $path, $force = false)
{
$start = microtime(true);
$farmer = self::pickOneServer();
$params = ['page' => $page, 'format' => $format, 'resolution' => $resolution, 'withText' => $withText, 'withGraphics' => $withGraphics, 'version' => $version, 'force' => $force, 'out' => $path, 'resolutionRatio' => $resolutionRatio, 'mobileRatio' => $mobileFirstRatio];
- $cl = new CommandLine('/usr/local/fluidbook_processfarm/bin/process', null, true);
- $cl->setSSH($farmer['host'], 'fluidbookfarmer', '', $farmer['port'] ?? 22, '/home/extranet/.ssh/id_rsa');
- $cl->setManualArg(base64_encode(json_encode($params)));
- $cl->execute();
- $o = trim($cl->getOutput());
- if (preg_match('|/data1/extranet/www/[^\s]+|', $o, $matches)) {
+ $output = self::sendRequest($farmer, 'process.php', $params);
+ if (preg_match('|/data1/extranet/www/[^\s]+|', $output, $matches)) {
$o = $matches[0];
+ } else {
+ $o = $output;
}
if (file_exists($o)) {
$res = $o;
} else {
- echo $cl->getOutput();
+ echo $o;
$res = false;
}
$time = round(microtime(true) - $start, 4);
- $log = '[' . $farmer['name'] . ']' . "\t" . date('Y-m-d H:i:s') . "\t" . $time . "\t$page|$format|$resolution|$withText|$withGraphics|$version\t$res\t" . $cl->getOutput() . "\n";
+ $log = '[' . $farmer['name'] . ']' . "\t" . date('Y-m-d H:i:s') . "\t" . $time . "\t$page|$format|$resolution|$withText|$withGraphics|$version\t$res\t" . $output . "\n";
error_log($log);
return $res;
}
- public static function ping($echo = true)
+ public static function ping($echo = true, $force = false)
{
- $originalConnectionTimeout = ini_get('default_socket_timeout');
- ini_set('default_socket_timeout', 5);
$cache = self::_pingCache();
$servers = self::getServers();
$pings = [];
}
if (isset($pings[$id]) && !$pings[$id]) {
// If ping failed recently, we wait a bit before trying again.
- if (rand(0, 9) != 5) {
+ if (!$force && rand(0, 9) != 5) {
if ($echo) {
echo 'Skipped, will try again soon' . "\n";
}
continue;
}
}
+ try {
+ $res = self::sendRequest($farmer, 'ping.php', [], 5);
+ $ok = $res == '1';
+ } catch (\Exception $e) {
+ $res = $e->getMessage();
+ $ok = false;
+ }
- $cl = new CommandLine('/usr/local/fluidbook_processfarm/bin/ping');
- $cl->setSSH($farmer['host'], 'fluidbookfarmer', '', $farmer['port'] ?? 22, self::KEY);
- $cl->execute();
- $ok = trim($cl->output) == '1';
if ($echo) {
- echo ($ok ? 'OK' : 'KO') . ' : ' . trim($cl->getOutput()) . "\n";
+ echo ($ok ? 'OK' : 'KO') . ' : ' . trim($res) . "\n";
}
$pings[$id] = $ok;
}
file_put_contents($cache, json_encode($pings));
- ini_set('default_socket_timeout', $originalConnectionTimeout);
+ file_put_contents(self::_serversCache(), json_encode($servers));
}
public static function update()
{
foreach (self::getServers() as $id => $farmer) {
echo $farmer['host'] . ' (' . $id . ')' . "\n";
- $cl = new CommandLine('sudo /usr/local/fluidbook_processfarm/bin/update');
- $cl->setSSH($farmer['host'], 'fluidbookfarmer', '', $farmer['port'] ?? 22, self::KEY);
- $cl->execute();
- echo $cl->getCommand() . "\n\n--\n\n" . $cl->getOutput() . "\n\n";
+// $cl = new CommandLine('sudo /usr/local/fluidbook_processfarm/bin/update');
+// $cl->setSSH($farmer['host'], 'fluidbookfarmer', '', $farmer['port'] ?? 22, self::KEY);
+// $cl->execute();
+// echo $cl->getCommand() . "\n\n--\n\n" . $cl->getOutput() . "\n\n";
}
}
}