From 86672d668343da880fd63f59befb17af5045ff6a Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 26 Feb 2020 14:14:26 +0100 Subject: [PATCH] Add startAndLoopBackground method --- docs | 2 +- examples/bot.php | 3 +- .../{combined_bot.php => combinedBot.php} | 24 ++---------- src/danog/MadelineProto/API.php | 37 ++++++++++++++++--- src/danog/MadelineProto/Logger.php | 2 +- src/danog/MadelineProto/MTProto.php | 7 ++-- 6 files changed, 42 insertions(+), 33 deletions(-) rename examples/{combined_bot.php => combinedBot.php} (83%) diff --git a/docs b/docs index 7a8bc742..e44490c9 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit 7a8bc742393a50f32acec02b8b0f8db31ada2028 +Subproject commit e44490c9dce88994398746b579c0debabd0f1e4a diff --git a/examples/bot.php b/examples/bot.php index 5ea4851c..d78f4a13 100755 --- a/examples/bot.php +++ b/examples/bot.php @@ -22,6 +22,7 @@ use danog\MadelineProto\API; use danog\MadelineProto\EventHandler; use danog\MadelineProto\Exception; +use danog\MadelineProto\Logger; use danog\MadelineProto\RPCErrorException; /* @@ -95,7 +96,7 @@ class MyEventHandler extends EventHandler } $settings = [ 'logger' => [ - 'logger_level' => 4 + 'logger_level' => Logger::VERBOSE ], 'serialization' => [ 'serialization_interval' => 30, diff --git a/examples/combined_bot.php b/examples/combinedBot.php similarity index 83% rename from examples/combined_bot.php rename to examples/combinedBot.php index c2a70895..b38524ee 100755 --- a/examples/combined_bot.php +++ b/examples/combinedBot.php @@ -29,7 +29,7 @@ use danog\MadelineProto\Tools; /* * Various ways to load MadelineProto */ -if (\file_exists(__DIR__.'/vendor/autoload.php')) { +if (\file_exists('vendor/autoload.php')) { include 'vendor/autoload.php'; } else { if (!\file_exists('madeline.php')) { @@ -103,25 +103,7 @@ foreach ([ 'user2.madeline' => 'Userbot login (2)' ] as $session => $message) { Logger::log($message, Logger::WARNING); - $MadelineProto = new API($session); - $MadelineProto->async(true); - $MadelineProto->loop(function () use ($MadelineProto) { - yield $MadelineProto->start(); - yield $MadelineProto->setEventHandler(MyEventHandler::class); - }); - $MadelineProtos []= $MadelineProto->loopFork(); + $MadelineProtos []= (new API($session))->startAndLoopBackground(MyEventHandler::class); } -do { - $thrown = false; - try { - Tools::wait(Tools::all($MadelineProtos)); - } catch (\Throwable $e) { - $thrown = true; - try { - $MadelineProto->report("Surfaced: $e"); - } catch (\Throwable $e) { - $MadelineProto->logger((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR); - } - } -} while ($thrown); +Tools::wait(Tools::all($MadelineProtos)); diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 61e123ec..e8f9e321 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -19,6 +19,8 @@ namespace danog\MadelineProto; +use Amp\Promise; + if (!\defined('MADELINEPROTO_TEST')) { \define('MADELINEPROTO_TEST', 'NOT PONY'); } @@ -222,16 +224,41 @@ class API extends InternalDoc * @return void */ public function startAndLoop(string $eventHandler): void + { + Tools::wait($this->startAndLoopAsync($eventHandler)); + } + /** + * Start MadelineProto and the event handler in background (enables async). + * + * Also initializes error reporting, catching and reporting all errors surfacing from the event loop. + * + * @param string $eventHandler Event handler class name + * + * @return void + */ + public function startAndLoopBackground(string $eventHandler): Promise + { + $this->start(['async' => false]); + return Tools::call($this->startAndLoopAsync($eventHandler)); + } + /** + * Start MadelineProto and the event handler (enables async). + * + * Also initializes error reporting, catching and reporting all errors surfacing from the event loop. + * + * @param string $eventHandler Event handler class name + * + * @return \Generator + */ + private function startAndLoopAsync(string $eventHandler): \Generator { $this->async(true); do { $thrown = false; try { - $this->loop(function () use ($eventHandler) { - yield $this->start(); - yield $this->setEventHandler($eventHandler); - }); - $this->loop(); + yield $this->start(); + yield $this->setEventHandler($eventHandler); + yield from $this->API->loop(); } catch (\Throwable $e) { $thrown = true; $this->logger((string) $e, Logger::FATAL_ERROR); diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index 67fd273e..46b1fac3 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -71,7 +71,7 @@ class Logger */ public $newline = "\n"; /** - * Logfile + * Logfile. * * @var ResourceOutputStream */ diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index a897dc3c..fe89c9d0 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -37,7 +37,6 @@ use danog\MadelineProto\TL\TLCallback; use function Amp\File\exists; use function Amp\File\size; -use function Amp\File\unlink as unlinkAsync; /** * Manages all of the mtproto stuff. @@ -1740,9 +1739,9 @@ class MTProto extends AsyncConstruct implements TLCallback $message = "!!! WARNING !!!\nThe logfile is empty, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message"; } else { $file = yield from $this->methodCallAsyncRead( - 'messages.uploadMedia', + 'messages.uploadMedia', [ - 'peer' => $this->reportDest[0], + 'peer' => $this->reportDest[0], 'media' => [ '_' => 'inputMediaUploadedDocument', 'file' => $path, @@ -1766,7 +1765,7 @@ class MTProto extends AsyncConstruct implements TLCallback } } if ($sent && $file) { - ftruncate($this->logger->stdout->getResource(), 0); + \ftruncate($this->logger->stdout->getResource(), 0); $this->logger->logger("Reported!"); } }