mirror of
https://github.com/danog/TelegramApiServer.git
synced 2024-11-30 04:19:13 +01:00
Server loop stop fix
This commit is contained in:
parent
abe9653c6d
commit
2e9f2c0ec1
@ -86,15 +86,17 @@ class Client
|
||||
$instance = new MadelineProto\API($file, $settings);
|
||||
$instance->async(true);
|
||||
$this->instances[$session] = $instance;
|
||||
if($startSession === true) {
|
||||
|
||||
$isLoggedIn = ($instance->API->authorized ?? MTProto::NOT_LOGGED_IN) === MTProto::LOGGED_IN;
|
||||
if($isLoggedIn || $startSession === true) {
|
||||
$instance->loop(function() use($instance) {
|
||||
yield $instance->start();
|
||||
});
|
||||
}
|
||||
if (($instance->API->authorized ?? MTProto::NOT_LOGGED_IN) === MTProto::LOGGED_IN) {
|
||||
if ($isLoggedIn) {
|
||||
$instance->setEventHandler(EventHandler::class);
|
||||
Loop::defer(static function() use($instance) {
|
||||
$instance->loop(['async' => true]);
|
||||
$instance->loop();
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -106,7 +108,7 @@ class Client
|
||||
}
|
||||
|
||||
$this->instances[$session]->stop();
|
||||
unset($this->instances[$session]);
|
||||
unset($this->instances[$session], EventHandler::$instances[$session]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,17 +11,22 @@ class EventHandler extends \danog\MadelineProto\EventHandler
|
||||
/** @var callable[] */
|
||||
public static array $eventListeners = [];
|
||||
private string $sessionName;
|
||||
public static array $instances = [];
|
||||
|
||||
public function __construct(API $MadelineProto)
|
||||
{
|
||||
parent::__construct($MadelineProto);
|
||||
$this->sessionName = Client::getSessionName($MadelineProto->session);
|
||||
if (!isset(static::$instances[$this->sessionName])) {
|
||||
static::$instances[$this->sessionName] = true;
|
||||
parent::__construct($MadelineProto);
|
||||
Logger::getInstance()->warning("Event observer CONSTRUCTED: {$this->sessionName}");
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
Logger::getInstance()->warning("Event observer DESTRUCTED {$this->sessionName}");
|
||||
unset(static::$instances[$this->sessionName]);
|
||||
Logger::getInstance()->warning("Event observer DESTRUCTED: {$this->sessionName}");
|
||||
}
|
||||
|
||||
public static function addEventListener($clientId, callable $callback)
|
||||
|
@ -2,10 +2,9 @@
|
||||
|
||||
namespace TelegramApiServer\MadelineProtoExtensions;
|
||||
|
||||
use Amp\Loop;
|
||||
use danog\MadelineProto;
|
||||
use TelegramApiServer\Client;
|
||||
use function Amp\call;
|
||||
use \danog\MadelineProto;
|
||||
|
||||
class SystemApiExtensions
|
||||
{
|
||||
@ -22,8 +21,7 @@ class SystemApiExtensions
|
||||
return $this->getSessionList();
|
||||
}
|
||||
|
||||
|
||||
public function removeSession(string $session):array
|
||||
public function removeSession(string $session): array
|
||||
{
|
||||
$this->client->removeSession($session);
|
||||
return $this->getSessionList();
|
||||
@ -62,7 +60,10 @@ class SystemApiExtensions
|
||||
];
|
||||
}
|
||||
|
||||
return $sessions;
|
||||
return [
|
||||
'sessions' => $sessions,
|
||||
'memory' => $this->bytesToHuman(memory_get_usage(true)),
|
||||
];
|
||||
}
|
||||
|
||||
public function removeSessionFile($session)
|
||||
@ -75,4 +76,13 @@ class SystemApiExtensions
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function bytesToHuman($bytes)
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
||||
for ($i = 0; $bytes > 1024; $i++) {
|
||||
$bytes /= 1024;
|
||||
}
|
||||
return round($bytes, 2) . ' ' . $units[$i];
|
||||
}
|
||||
}
|
@ -3,6 +3,9 @@
|
||||
namespace TelegramApiServer\Server;
|
||||
|
||||
use Amp;
|
||||
use Amp\Loop;
|
||||
use danog\MadelineProto\API;
|
||||
use danog\MadelineProto\Tools;
|
||||
use TelegramApiServer\Client;
|
||||
use TelegramApiServer\Config;
|
||||
use TelegramApiServer\Logger;
|
||||
@ -18,7 +21,7 @@ class Server
|
||||
*/
|
||||
public function __construct(Client $client, array $options, ?array $sessionFiles)
|
||||
{
|
||||
Amp\Loop::run(function () use ($client, $options, $sessionFiles) {
|
||||
Amp\Loop::defer(function () use ($client, $options, $sessionFiles) {
|
||||
$server = new Amp\Http\Server\Server(
|
||||
$this->getServerAddresses(static::getConfig($options)),
|
||||
(new Router($client))->getRouter(),
|
||||
@ -32,8 +35,12 @@ class Server
|
||||
yield $server->start();
|
||||
|
||||
$this->registerShutdown($server);
|
||||
|
||||
});
|
||||
|
||||
while (true) {
|
||||
Amp\Loop::run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static function getServerAddresses(array $config): array
|
||||
@ -53,9 +60,9 @@ class Server
|
||||
*/
|
||||
private static function registerShutdown(Amp\Http\Server\Server $server)
|
||||
{
|
||||
|
||||
if (defined('SIGINT')) {
|
||||
Amp\Loop::onSignal(SIGINT, static function (string $watcherId) use ($server) {
|
||||
Logger::getInstance()->emergency('Got SIGINT');
|
||||
Amp\Loop::cancel($watcherId);
|
||||
yield $server->stop();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user