Final fixes

This commit is contained in:
Daniil Gentili 2020-07-12 01:12:20 +02:00
parent 84ba71475d
commit 45067af046
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
9 changed files with 32 additions and 52 deletions

View File

@ -74,17 +74,17 @@ class FastAPI extends API
} catch (\Throwable $e) { } catch (\Throwable $e) {
} }
StatCache::clear($session->getIpcPath()); StatCache::clear($session->getIpcPath());
yield from Server::startMe($session); Server::startMe($session);
$inited = false; $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); yield Tools::sleep(1);
StatCache::clear($session->getIpcPath()); StatCache::clear($session->getIpcPath());
if ($client = yield from $this->checkInit($session, $settings)) { if ($client = yield from $this->checkInit($session, $settings)) {
$inited = true; $inited = true;
break; break;
} }
yield from Server::startMe($session); Server::startMe($session);
} }
if (!$client) { if (!$client) {
throw new Exception("The IPC server isn't running, please check logs!"); throw new Exception("The IPC server isn't running, please check logs!");

View File

@ -122,7 +122,7 @@ class Client
{ {
$this->requests []= $deferred = new Deferred; $this->requests []= $deferred = new Deferred;
yield $this->server->send([$function, $arguments]); yield $this->server->send([$function, $arguments]);
return $deferred->promise(); return yield $deferred->promise();
} }
/** /**
* Placeholder. * Placeholder.

View File

@ -11,14 +11,12 @@ final class ProcessRunner extends RunnerAbstract
/** @var string|null Cached path to located PHP binary. */ /** @var string|null Cached path to located PHP binary. */
private static $binaryPath; private static $binaryPath;
/** @var \Amp\Process\Process */
private $process;
/** /**
* Constructor. * Runner.
* *
* @param string $session Session path * @param string $session Session path
*/ */
public function __construct(string $session) public static function start(string $session): void
{ {
if (\PHP_SAPI === "cli") { if (\PHP_SAPI === "cli") {
$binary = \PHP_BINARY; $binary = \PHP_BINARY;
@ -41,11 +39,9 @@ final class ProcessRunner extends RunnerAbstract
$runner, $runner,
'madeline-ipc', 'madeline-ipc',
\escapeshellarg($session), \escapeshellarg($session),
'&' '&>/dev/null &'
]); ]);
var_dump($command); shell_exec($command);
$this->process = new BaseProcess($command, Magic::getcwd());
} }
private static function locateBinary(): string private static function locateBinary(): string
{ {
@ -75,14 +71,4 @@ final class ProcessRunner extends RunnerAbstract
return \implode(" ", $result); return \implode(" ", $result);
} }
/**
* Starts the process.
*
* @return Promise<int> Resolved with the PID
*/
public function start(): Promise
{
return $this->process->start();
}
} }

View File

@ -59,16 +59,11 @@ abstract class RunnerAbstract
return $scriptPath; return $scriptPath;
} }
/** /**
* Constructor. * Runner.
* *
* @param string $session Sessio path * @param string $session Session path
*/
abstract public function __construct(string $session);
/**
* Starts the execution process.
* *
* @return Promise<int> Resolves with the PID * @return void
*/ */
abstract public function start(): Promise; abstract public static function start(string $session): void;
} }

View File

@ -25,11 +25,11 @@ final class WebRunner extends RunnerAbstract
*/ */
private $res; private $res;
/** /**
* Constructor. * Start.
* *
* @param string $session Session path * @param string $session Session path
*/ */
public function __construct(string $session) public static function start(string $session): void
{ {
if (!isset($_SERVER['SERVER_NAME'])) { if (!isset($_SERVER['SERVER_NAME'])) {
throw new ContextException("Could not initialize web runner!"); throw new ContextException("Could not initialize web runner!");
@ -83,15 +83,7 @@ final class WebRunner extends RunnerAbstract
'argv' => ['pony', 'madeline-ipc', $session], 'argv' => ['pony', 'madeline-ipc', $session],
'cwd' => Magic::getcwd() 'cwd' => Magic::getcwd()
]; ];
}
/**
* Start process.
*
* @return Promise
*/
public function start(): Promise
{
$params = \http_build_query($this->params); $params = \http_build_query($this->params);
$address = ($_SERVER['HTTPS'] ?? false ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME']; $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 // 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. // 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)); fwrite($res = \fsockopen($address, $port), $payload);
return $this->res->write($payload); fclose($res);
} }
} }

View File

@ -78,6 +78,7 @@ use danog\MadelineProto\Tools;
Magic::classExists(); Magic::classExists();
Magic::$script_cwd = $_GET['cwd'] ?? Magic::getcwd(); Magic::$script_cwd = $_GET['cwd'] ?? Magic::getcwd();
$API = new API($ipcPath); $API = new API($ipcPath);
$API->init();
if ($API->hasEventHandler()) { if ($API->hasEventHandler()) {
unset($API); unset($API);
\gc_collect_cycles(); \gc_collect_cycles();

View File

@ -59,19 +59,19 @@ class Server extends SignalLoop
* *
* @param string $session Session path * @param string $session Session path
* *
* @return \Generator * @return void
*/ */
public static function startMe(string $session): \Generator public static function startMe(string $session): void
{ {
try { try {
Logger::log("Starting IPC server $session (process)"); Logger::log("Starting IPC server $session (process)");
yield (new ProcessRunner($session))->start(); ProcessRunner::start($session);
return; return;
} catch (\Throwable $e) { } catch (\Throwable $e) {
Logger::log($e); Logger::log($e);
} }
Logger::log("Starting IPC server $session (web)"); Logger::log("Starting IPC server $session (web)");
yield (new WebRunner($session))->start(); WebRunner::start($session);
} }
/** /**
* Main loop. * Main loop.
@ -97,9 +97,11 @@ class Server extends SignalLoop
$this->API->logger("Accepted IPC client connection!"); $this->API->logger("Accepted IPC client connection!");
$id = 0; $id = 0;
while ($payload = yield $socket->receive()) { try {
Tools::callFork($this->clientRequest($socket, $id++, $payload)); while ($payload = yield $socket->receive()) {
} Tools::callFork($this->clientRequest($socket, $id++, $payload));
}
} catch (\Throwable $e) {}
} }
/** /**
* Handle client request. * Handle client request.

View File

@ -45,6 +45,10 @@ class Serialization
Logger::log('Waiting for exclusive session lock...'); Logger::log('Waiting for exclusive session lock...');
$warningId = Loop::delay(1000, static function () use (&$warningId) { $warningId = Loop::delay(1000, static function () use (&$warningId) {
Logger::log("It seems like the session is busy."); 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."); 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...')); $warningId = Loop::repeat(5000, fn () => Logger::log('Still waiting for exclusive session lock...'));
Loop::unreference($warningId); Loop::unreference($warningId);

View File

@ -34,7 +34,7 @@ trait Start
*/ */
public function start(): \Generator public function start(): \Generator
{ {
if (yield $this->getAuthorization() === MTProto::LOGGED_IN) { if ((yield $this->getAuthorization()) === MTProto::LOGGED_IN) {
return yield from $this->fullGetSelf(); return yield from $this->fullGetSelf();
} }
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {