Skip to content

Commit

Permalink
Detection of broken sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Apr 12, 2020
1 parent 4bb3c63 commit ed4716c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private function loop(MadelineProto\API $instance, callable $callback = null): v
critical(
$e->getMessage(),
[
'session' => $sessionName,
'probable_session' => $sessionName,
'exception' => [
'exception' => get_class($e),
'code' => $e->getCode(),
Expand All @@ -162,11 +162,26 @@ private function loop(MadelineProto\API $instance, callable $callback = null): v
],
]
);
$this->removeSession($sessionName);
if (count($this->instances) === 0) {
throw new RuntimeException('Last session stopped. Need restart.');
foreach ($this->getBrokenSessions() as $session) {
$this->removeSession($session);
}
}
}

public function getBrokenSessions(): array
{
$brokenSessions = [];
foreach ($this->instances as $session => $instance) {
warning("Checking session: {$session}");
try {
$instance->getSelf(['async' => false]);
} catch (\Throwable $e) {
warning("Session is broken: {$session}");
$brokenSessions[] = $session;
}
}

return $brokenSessions;
}

}
5 changes: 4 additions & 1 deletion src/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public function __construct(Client $client, array $options, ?array $sessionFiles
'line' => $e->getLine(),
],
]);
exit;

foreach ($client->getBrokenSessions() as $session) {
$client->removeSession($session);
}
}
}

Expand Down

0 comments on commit ed4716c

Please sign in to comment.