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) {
}
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!");

View File

@ -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.

View File

@ -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<int> Resolved with the PID
*/
public function start(): Promise
{
return $this->process->start();
}
}

View File

@ -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<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;
/**
* 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);
}
}

View File

@ -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();

View File

@ -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.

View File

@ -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);

View File

@ -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') {