Skip to content

Commit

Permalink
Server loop stop fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Jan 26, 2020
1 parent abe9653 commit 2e9f2c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,17 @@ public function addSession(string $session, bool $startSession = false)
$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();
});
}
}
Expand All @@ -106,7 +108,7 @@ public function removeSession($session)
}

$this->instances[$session]->stop();
unset($this->instances[$session]);
unset($this->instances[$session], EventHandler::$instances[$session]);
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/EventObservers/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Logger::getInstance()->warning("Event observer CONSTRUCTED: {$this->sessionName}");
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)
Expand Down
20 changes: 15 additions & 5 deletions src/MadelineProtoExtensions/SystemApiExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -22,8 +21,7 @@ public function addSession(string $session)
return $this->getSessionList();
}


public function removeSession(string $session):array
public function removeSession(string $session): array
{
$this->client->removeSession($session);
return $this->getSessionList();
Expand Down Expand Up @@ -62,7 +60,10 @@ public function getSessionList(): array
];
}

return $sessions;
return [
'sessions' => $sessions,
'memory' => $this->bytesToHuman(memory_get_usage(true)),
];
}

public function removeSessionFile($session)
Expand All @@ -75,4 +76,13 @@ public function removeSessionFile($session)
}
});
}

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];
}
}
13 changes: 10 additions & 3 deletions src/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand All @@ -32,8 +35,12 @@ public function __construct(Client $client, array $options, ?array $sessionFiles
yield $server->start();

$this->registerShutdown($server);

});

while (true) {
Amp\Loop::run();
}

}

private static function getServerAddresses(array $config): array
Expand All @@ -53,9 +60,9 @@ private static function getServerAddresses(array $config): array
*/
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();
});
Expand Down

0 comments on commit 2e9f2c0

Please sign in to comment.