Add startAndLoopBackground method

This commit is contained in:
Daniil Gentili 2020-02-26 14:14:26 +01:00
parent e50dd79e70
commit 86672d6683
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 42 additions and 33 deletions

2
docs

@ -1 +1 @@
Subproject commit 7a8bc742393a50f32acec02b8b0f8db31ada2028 Subproject commit e44490c9dce88994398746b579c0debabd0f1e4a

View File

@ -22,6 +22,7 @@
use danog\MadelineProto\API; use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler; use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger;
use danog\MadelineProto\RPCErrorException; use danog\MadelineProto\RPCErrorException;
/* /*
@ -95,7 +96,7 @@ class MyEventHandler extends EventHandler
} }
$settings = [ $settings = [
'logger' => [ 'logger' => [
'logger_level' => 4 'logger_level' => Logger::VERBOSE
], ],
'serialization' => [ 'serialization' => [
'serialization_interval' => 30, 'serialization_interval' => 30,

View File

@ -29,7 +29,7 @@ use danog\MadelineProto\Tools;
/* /*
* Various ways to load MadelineProto * Various ways to load MadelineProto
*/ */
if (\file_exists(__DIR__.'/vendor/autoload.php')) { if (\file_exists('vendor/autoload.php')) {
include 'vendor/autoload.php'; include 'vendor/autoload.php';
} else { } else {
if (!\file_exists('madeline.php')) { if (!\file_exists('madeline.php')) {
@ -103,25 +103,7 @@ foreach ([
'user2.madeline' => 'Userbot login (2)' 'user2.madeline' => 'Userbot login (2)'
] as $session => $message) { ] as $session => $message) {
Logger::log($message, Logger::WARNING); Logger::log($message, Logger::WARNING);
$MadelineProto = new API($session); $MadelineProtos []= (new API($session))->startAndLoopBackground(MyEventHandler::class);
$MadelineProto->async(true);
$MadelineProto->loop(function () use ($MadelineProto) {
yield $MadelineProto->start();
yield $MadelineProto->setEventHandler(MyEventHandler::class);
});
$MadelineProtos []= $MadelineProto->loopFork();
} }
do { Tools::wait(Tools::all($MadelineProtos));
$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);

View File

@ -19,6 +19,8 @@
namespace danog\MadelineProto; namespace danog\MadelineProto;
use Amp\Promise;
if (!\defined('MADELINEPROTO_TEST')) { if (!\defined('MADELINEPROTO_TEST')) {
\define('MADELINEPROTO_TEST', 'NOT PONY'); \define('MADELINEPROTO_TEST', 'NOT PONY');
} }
@ -222,16 +224,41 @@ class API extends InternalDoc
* @return void * @return void
*/ */
public function startAndLoop(string $eventHandler): 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); $this->async(true);
do { do {
$thrown = false; $thrown = false;
try { try {
$this->loop(function () use ($eventHandler) { yield $this->start();
yield $this->start(); yield $this->setEventHandler($eventHandler);
yield $this->setEventHandler($eventHandler); yield from $this->API->loop();
});
$this->loop();
} catch (\Throwable $e) { } catch (\Throwable $e) {
$thrown = true; $thrown = true;
$this->logger((string) $e, Logger::FATAL_ERROR); $this->logger((string) $e, Logger::FATAL_ERROR);

View File

@ -71,7 +71,7 @@ class Logger
*/ */
public $newline = "\n"; public $newline = "\n";
/** /**
* Logfile * Logfile.
* *
* @var ResourceOutputStream * @var ResourceOutputStream
*/ */

View File

@ -37,7 +37,6 @@ use danog\MadelineProto\TL\TLCallback;
use function Amp\File\exists; use function Amp\File\exists;
use function Amp\File\size; use function Amp\File\size;
use function Amp\File\unlink as unlinkAsync;
/** /**
* Manages all of the mtproto stuff. * 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"; $message = "!!! WARNING !!!\nThe logfile is empty, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message";
} else { } else {
$file = yield from $this->methodCallAsyncRead( $file = yield from $this->methodCallAsyncRead(
'messages.uploadMedia', 'messages.uploadMedia',
[ [
'peer' => $this->reportDest[0], 'peer' => $this->reportDest[0],
'media' => [ 'media' => [
'_' => 'inputMediaUploadedDocument', '_' => 'inputMediaUploadedDocument',
'file' => $path, 'file' => $path,
@ -1766,7 +1765,7 @@ class MTProto extends AsyncConstruct implements TLCallback
} }
} }
if ($sent && $file) { if ($sent && $file) {
ftruncate($this->logger->stdout->getResource(), 0); \ftruncate($this->logger->stdout->getResource(), 0);
$this->logger->logger("Reported!"); $this->logger->logger("Reported!");
} }
} }