Add startAndLoopBackground method
This commit is contained in:
parent
e50dd79e70
commit
86672d6683
2
docs
2
docs
@ -1 +1 @@
|
||||
Subproject commit 7a8bc742393a50f32acec02b8b0f8db31ada2028
|
||||
Subproject commit e44490c9dce88994398746b579c0debabd0f1e4a
|
@ -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,
|
||||
|
@ -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));
|
@ -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);
|
||||
|
@ -71,7 +71,7 @@ class Logger
|
||||
*/
|
||||
public $newline = "\n";
|
||||
/**
|
||||
* Logfile
|
||||
* Logfile.
|
||||
*
|
||||
* @var ResourceOutputStream
|
||||
*/
|
||||
|
@ -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!");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user