From 45067af0468f0a4234f3226a5ac7d5511c6228b4 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 12 Jul 2020 01:12:20 +0200 Subject: [PATCH] Final fixes --- src/danog/MadelineProto/FastAPI.php | 8 +++---- src/danog/MadelineProto/Ipc/Client.php | 2 +- .../Ipc/Runner/ProcessRunner.php | 22 ++++--------------- .../Ipc/Runner/RunnerAbstract.php | 13 ++++------- .../MadelineProto/Ipc/Runner/WebRunner.php | 16 ++++---------- src/danog/MadelineProto/Ipc/Runner/entry.php | 1 + src/danog/MadelineProto/Ipc/Server.php | 16 ++++++++------ src/danog/MadelineProto/Serialization.php | 4 ++++ src/danog/MadelineProto/Wrappers/Start.php | 2 +- 9 files changed, 32 insertions(+), 52 deletions(-) diff --git a/src/danog/MadelineProto/FastAPI.php b/src/danog/MadelineProto/FastAPI.php index 39c18e12..d5072d6a 100644 --- a/src/danog/MadelineProto/FastAPI.php +++ b/src/danog/MadelineProto/FastAPI.php @@ -74,17 +74,17 @@ class FastAPI extends API } catch (\Throwable $e) { } StatCache::clear($session->getIpcPath()); - yield from Server::startMe($session); + Server::startMe($session); $inited = false; - for ($x = 0; $x < 3; $x++) { - $this->logger->logger("Waiting for IPC server to start..."); + $this->logger->logger("Waiting for IPC server to start..."); + for ($x = 0; $x < 30; $x++) { yield Tools::sleep(1); StatCache::clear($session->getIpcPath()); if ($client = yield from $this->checkInit($session, $settings)) { $inited = true; break; } - yield from Server::startMe($session); + Server::startMe($session); } if (!$client) { throw new Exception("The IPC server isn't running, please check logs!"); diff --git a/src/danog/MadelineProto/Ipc/Client.php b/src/danog/MadelineProto/Ipc/Client.php index 848d0893..84a34eaf 100644 --- a/src/danog/MadelineProto/Ipc/Client.php +++ b/src/danog/MadelineProto/Ipc/Client.php @@ -122,7 +122,7 @@ class Client { $this->requests []= $deferred = new Deferred; yield $this->server->send([$function, $arguments]); - return $deferred->promise(); + return yield $deferred->promise(); } /** * Placeholder. diff --git a/src/danog/MadelineProto/Ipc/Runner/ProcessRunner.php b/src/danog/MadelineProto/Ipc/Runner/ProcessRunner.php index 708a19d2..6eb3fbfe 100644 --- a/src/danog/MadelineProto/Ipc/Runner/ProcessRunner.php +++ b/src/danog/MadelineProto/Ipc/Runner/ProcessRunner.php @@ -11,14 +11,12 @@ final class ProcessRunner extends RunnerAbstract /** @var string|null Cached path to located PHP binary. */ private static $binaryPath; - /** @var \Amp\Process\Process */ - private $process; /** - * Constructor. + * Runner. * * @param string $session Session path */ - public function __construct(string $session) + public static function start(string $session): void { if (\PHP_SAPI === "cli") { $binary = \PHP_BINARY; @@ -41,11 +39,9 @@ final class ProcessRunner extends RunnerAbstract $runner, 'madeline-ipc', \escapeshellarg($session), - '&' + '&>/dev/null &' ]); - var_dump($command); - - $this->process = new BaseProcess($command, Magic::getcwd()); + shell_exec($command); } private static function locateBinary(): string { @@ -75,14 +71,4 @@ final class ProcessRunner extends RunnerAbstract return \implode(" ", $result); } - - /** - * Starts the process. - * - * @return Promise Resolved with the PID - */ - public function start(): Promise - { - return $this->process->start(); - } } diff --git a/src/danog/MadelineProto/Ipc/Runner/RunnerAbstract.php b/src/danog/MadelineProto/Ipc/Runner/RunnerAbstract.php index 9e5c9afd..7616f95b 100644 --- a/src/danog/MadelineProto/Ipc/Runner/RunnerAbstract.php +++ b/src/danog/MadelineProto/Ipc/Runner/RunnerAbstract.php @@ -59,16 +59,11 @@ abstract class RunnerAbstract return $scriptPath; } /** - * Constructor. + * Runner. * - * @param string $session Sessio path - */ - abstract public function __construct(string $session); - - /** - * Starts the execution process. + * @param string $session Session path * - * @return Promise Resolves with the PID + * @return void */ - abstract public function start(): Promise; + abstract public static function start(string $session): void; } diff --git a/src/danog/MadelineProto/Ipc/Runner/WebRunner.php b/src/danog/MadelineProto/Ipc/Runner/WebRunner.php index 111fbd86..7bbbe8e9 100644 --- a/src/danog/MadelineProto/Ipc/Runner/WebRunner.php +++ b/src/danog/MadelineProto/Ipc/Runner/WebRunner.php @@ -25,11 +25,11 @@ final class WebRunner extends RunnerAbstract */ private $res; /** - * Constructor. + * Start. * * @param string $session Session path */ - public function __construct(string $session) + public static function start(string $session): void { if (!isset($_SERVER['SERVER_NAME'])) { throw new ContextException("Could not initialize web runner!"); @@ -83,15 +83,7 @@ final class WebRunner extends RunnerAbstract 'argv' => ['pony', 'madeline-ipc', $session], 'cwd' => Magic::getcwd() ]; - } - /** - * Start process. - * - * @return Promise - */ - public function start(): Promise - { $params = \http_build_query($this->params); $address = ($_SERVER['HTTPS'] ?? false ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME']; @@ -103,7 +95,7 @@ final class WebRunner extends RunnerAbstract // We don't care for results or timeouts here, PHP doesn't count IOwait time as execution time anyway // Technically should use amphp/socket, but I guess it's OK to not introduce another dependency just for a socket that will be used once. - $this->res = new ResourceOutputStream(\fsockopen($address, $port)); - return $this->res->write($payload); + fwrite($res = \fsockopen($address, $port), $payload); + fclose($res); } } diff --git a/src/danog/MadelineProto/Ipc/Runner/entry.php b/src/danog/MadelineProto/Ipc/Runner/entry.php index b2194c7a..2e87d0ef 100644 --- a/src/danog/MadelineProto/Ipc/Runner/entry.php +++ b/src/danog/MadelineProto/Ipc/Runner/entry.php @@ -78,6 +78,7 @@ use danog\MadelineProto\Tools; Magic::classExists(); Magic::$script_cwd = $_GET['cwd'] ?? Magic::getcwd(); $API = new API($ipcPath); + $API->init(); if ($API->hasEventHandler()) { unset($API); \gc_collect_cycles(); diff --git a/src/danog/MadelineProto/Ipc/Server.php b/src/danog/MadelineProto/Ipc/Server.php index 99e925e8..a3ab35b0 100644 --- a/src/danog/MadelineProto/Ipc/Server.php +++ b/src/danog/MadelineProto/Ipc/Server.php @@ -59,19 +59,19 @@ class Server extends SignalLoop * * @param string $session Session path * - * @return \Generator + * @return void */ - public static function startMe(string $session): \Generator + public static function startMe(string $session): void { try { Logger::log("Starting IPC server $session (process)"); - yield (new ProcessRunner($session))->start(); + ProcessRunner::start($session); return; } catch (\Throwable $e) { Logger::log($e); } Logger::log("Starting IPC server $session (web)"); - yield (new WebRunner($session))->start(); + WebRunner::start($session); } /** * Main loop. @@ -97,9 +97,11 @@ class Server extends SignalLoop $this->API->logger("Accepted IPC client connection!"); $id = 0; - while ($payload = yield $socket->receive()) { - Tools::callFork($this->clientRequest($socket, $id++, $payload)); - } + try { + while ($payload = yield $socket->receive()) { + Tools::callFork($this->clientRequest($socket, $id++, $payload)); + } + } catch (\Throwable $e) {} } /** * Handle client request. diff --git a/src/danog/MadelineProto/Serialization.php b/src/danog/MadelineProto/Serialization.php index 666632c6..b6841612 100644 --- a/src/danog/MadelineProto/Serialization.php +++ b/src/danog/MadelineProto/Serialization.php @@ -45,6 +45,10 @@ class Serialization Logger::log('Waiting for exclusive session lock...'); $warningId = Loop::delay(1000, static function () use (&$warningId) { Logger::log("It seems like the session is busy."); + if (\defined(\MADELINE_WORKER::class)) { + Logger::log("Exiting since we're in a worker"); + Magic::shutdown(1); + } Logger::log("Telegram does not support starting multiple instances of the same session, make sure no other instance of the session is running."); $warningId = Loop::repeat(5000, fn () => Logger::log('Still waiting for exclusive session lock...')); Loop::unreference($warningId); diff --git a/src/danog/MadelineProto/Wrappers/Start.php b/src/danog/MadelineProto/Wrappers/Start.php index d5b213e6..6fca52e0 100644 --- a/src/danog/MadelineProto/Wrappers/Start.php +++ b/src/danog/MadelineProto/Wrappers/Start.php @@ -34,7 +34,7 @@ trait Start */ public function start(): \Generator { - if (yield $this->getAuthorization() === MTProto::LOGGED_IN) { + if ((yield $this->getAuthorization()) === MTProto::LOGGED_IN) { return yield from $this->fullGetSelf(); } if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {