Reduce boilerplate

This commit is contained in:
Daniil Gentili 2020-02-26 15:15:37 +01:00
parent 86672d6683
commit b0d512b77d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 43 additions and 20 deletions

2
docs

@ -1 +1 @@
Subproject commit e44490c9dce88994398746b579c0debabd0f1e4a
Subproject commit 648b0a1250e3949c631421b8f1666371567f74c7

View File

@ -103,7 +103,7 @@ foreach ([
'user2.madeline' => 'Userbot login (2)'
] as $session => $message) {
Logger::log($message, Logger::WARNING);
$MadelineProtos []= (new API($session))->startAndLoopBackground(MyEventHandler::class);
$MadelineProtos []= new API($session);
}
Tools::wait(Tools::all($MadelineProtos));
API::startAndLoopMulti($MadelineProtos, MyEventHandler::class);

View File

@ -225,21 +225,45 @@ class API extends InternalDoc
*/
public function startAndLoop(string $eventHandler): void
{
Tools::wait($this->startAndLoopAsync($eventHandler));
while (true) {
try {
Tools::wait($this->startAndLoopAsync($eventHandler));
return;
} catch (\Throwable $e) {
$this->logger((string) $e, Logger::FATAL_ERROR);
$this->report("Surfaced: $e");
}
}
}
/**
* Start MadelineProto and the event handler in background (enables async).
* Start multiple instances of MadelineProto and the event handlers (enables async).
*
* Also initializes error reporting, catching and reporting all errors surfacing from the event loop.
* @param API[] $instances Instances of madeline
* @param string[]|string $eventHandler Event handler(s)
*
* @param string $eventHandler Event handler class name
*
* @return void
* @return Promise
*/
public function startAndLoopBackground(string $eventHandler): Promise
public static function startAndLoopMulti(array $instances, $eventHandler): void
{
$this->start(['async' => false]);
return Tools::call($this->startAndLoopAsync($eventHandler));
if (\is_string($eventHandler)) {
$eventHandler = \array_fill_keys(\array_keys($instances), $eventHandler);
}
$instanceOne = array_values($instances)[0];
while (true) {
try {
$promises = [];
foreach ($instances as $k => $instance) {
$instance->start(['async' => false]);
$promises []= Tools::call($instance->startAndLoopAsync($eventHandler[$k]));
}
Tools::wait(Tools::all($promises));
return;
} catch (\Throwable $e) {
$instanceOne->logger((string) $e, Logger::FATAL_ERROR);
$instanceOne->report("Surfaced: $e");
}
}
}
/**
* Start MadelineProto and the event handler (enables async).
@ -253,17 +277,15 @@ class API extends InternalDoc
private function startAndLoopAsync(string $eventHandler): \Generator
{
$this->async(true);
do {
$thrown = false;
while (true) {
try {
yield $this->start();
yield $this->setEventHandler($eventHandler);
yield from $this->API->loop();
return yield from $this->API->loop();
} catch (\Throwable $e) {
$thrown = true;
$this->logger((string) $e, Logger::FATAL_ERROR);
$this->report("Surfaced: $e");
}
} while ($thrown);
}
}
}

View File

@ -1730,8 +1730,7 @@ class MTProto extends AsyncConstruct implements TLCallback
return;
}
$file = null;
if ($this->settings['logger']['logger'] === Logger::FILE_LOGGER) {
$path = $this->settings['logger']['logger_param'];
if ($this->settings['logger']['logger'] === Logger::FILE_LOGGER && $path = $this->settings['logger']['logger_param']) {
StatCache::clear($path);
if (!yield exists($path)) {
$message = "!!! WARNING !!!\nThe logfile does not exist, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message";
@ -1757,7 +1756,9 @@ class MTProto extends AsyncConstruct implements TLCallback
foreach ($this->reportDest as $id) {
try {
yield from $this->methodCallAsyncRead('messages.sendMessage', ['peer' => $id, 'message' => $message]);
yield from $this->methodCallAsyncRead('messages.sendMedia', ['peer' => $id, 'media' => $file]);
if ($file) {
yield from $this->methodCallAsyncRead('messages.sendMedia', ['peer' => $id, 'media' => $file]);
}
$sent &= true;
} catch (\Throwable $e) {
$sent &= false;