1
0
mirror of https://github.com/danog/MadelineProto.git synced 2025-01-23 04:11:11 +01:00

Avoid FILE_PART_0_MISSING issues when concurrently uploading report logs

This commit is contained in:
Daniil Gentili 2023-01-22 11:00:07 +01:00
parent ff1565c4cd
commit 8e27815b9e
6 changed files with 45 additions and 40 deletions

View File

@ -27,8 +27,6 @@ use danog\MadelineProto\Magic;
use danog\MadelineProto\SecurityException; use danog\MadelineProto\SecurityException;
use danog\MadelineProto\SessionPaths; use danog\MadelineProto\SessionPaths;
use danog\MadelineProto\Settings\Ipc; use danog\MadelineProto\Settings\Ipc;
use danog\MadelineProto\Shutdown;
use Revolt\EventLoop;
use Revolt\EventLoop\UncaughtThrowable; use Revolt\EventLoop\UncaughtThrowable;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;

View File

@ -240,7 +240,7 @@ final class Logger
$this->prefix = $prefix === '' ? '' : ', '.$prefix; $this->prefix = $prefix === '' ? '' : ', '.$prefix;
$this->mode = $settings->getType(); $this->mode = $settings->getType();
$this->level = $settings->getLevel(); $this->level = self::LEVEL_ULTRA_VERBOSE;
$optional = $settings->getExtra(); $optional = $settings->getExtra();

View File

@ -24,6 +24,7 @@ use Amp\DeferredFuture;
use Amp\Dns\DnsResolver; use Amp\Dns\DnsResolver;
use Amp\Future; use Amp\Future;
use Amp\Http\Client\HttpClient; use Amp\Http\Client\HttpClient;
use Amp\Sync\LocalMutex;
use danog\MadelineProto\Db\DbArray; use danog\MadelineProto\Db\DbArray;
use danog\MadelineProto\Db\DbPropertiesFactory; use danog\MadelineProto\Db\DbPropertiesFactory;
use danog\MadelineProto\Db\DbPropertiesTrait; use danog\MadelineProto\Db\DbPropertiesTrait;
@ -1752,6 +1753,7 @@ final class MTProto implements TLCallback, LoggerGetter
/** @var array<int> $userOrId */ /** @var array<int> $userOrId */
$this->reportDest = $userOrId; $this->reportDest = $userOrId;
} }
private ?LocalMutex $reportMutex = null;
/** /**
* Report an error to the previously set peer. * Report an error to the previously set peer.
* *
@ -1763,43 +1765,49 @@ final class MTProto implements TLCallback, LoggerGetter
if (!$this->reportDest) { if (!$this->reportDest) {
return; return;
} }
$file = null; $this->reportMutex ??= new LocalMutex;
if ($this->settings->getLogger()->getType() === Logger::FILE_LOGGER $lock = $this->reportMutex->acquire();
&& $path = $this->settings->getLogger()->getExtra()) { try {
touchAsync($path); $file = null;
if (!getSize($path)) { if ($this->settings->getLogger()->getType() === Logger::FILE_LOGGER
$message = "!!! WARNING !!!\nThe logfile is empty, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message"; && $path = $this->settings->getLogger()->getExtra()) {
} else { touchAsync($path);
$file = $this->methodCallAsyncRead( if (!getSize($path)) {
'messages.uploadMedia', $message = "!!! WARNING !!!\nThe logfile is empty, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message";
[ } else {
'peer' => $this->reportDest[0], $file = $this->methodCallAsyncRead(
'media' => [ 'messages.uploadMedia',
'_' => 'inputMediaUploadedDocument', [
'file' => $path, 'peer' => $this->reportDest[0],
'attributes' => [ 'media' => [
['_' => 'documentAttributeFilename', 'file_name' => 'MadelineProto.log'], '_' => 'inputMediaUploadedDocument',
'file' => $path,
'attributes' => [
['_' => 'documentAttributeFilename', 'file_name' => 'MadelineProto.log'],
],
], ],
], ],
], );
);
}
}
$sent = false;
foreach ($this->reportDest as $id) {
try {
$this->methodCallAsyncRead('messages.sendMessage', ['peer' => $id, 'message' => $message, 'parse_mode' => $parseMode]);
if ($file) {
$this->methodCallAsyncRead('messages.sendMedia', ['peer' => $id, 'media' => $file]);
} }
$sent = true;
} catch (Throwable $e) {
$this->logger("While reporting to $id: $e", Logger::FATAL_ERROR);
} }
} $sent = false;
if ($sent && $file) { foreach ($this->reportDest as $id) {
$this->logger->truncate(); try {
$this->logger->logger('Reported!'); $this->methodCallAsyncRead('messages.sendMessage', ['peer' => $id, 'message' => $message, 'parse_mode' => $parseMode]);
if ($file) {
$this->methodCallAsyncRead('messages.sendMedia', ['peer' => $id, 'media' => $file]);
}
$sent = true;
} catch (Throwable $e) {
$this->logger("While reporting to $id: $e", Logger::FATAL_ERROR);
}
}
if ($sent && $file) {
$this->logger->truncate();
$this->logger->logger('Reported!');
}
} finally {
$lock->release();
} }
} }
/** /**

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace danog\MadelineProto\MTProtoTools; namespace danog\MadelineProto\MTProtoTools;
use Amp\ByteStream\Payload;
use Amp\ByteStream\Pipe; use Amp\ByteStream\Pipe;
use Amp\ByteStream\ReadableResourceStream; use Amp\ByteStream\ReadableResourceStream;
use Amp\ByteStream\ReadableStream; use Amp\ByteStream\ReadableStream;

View File

@ -207,7 +207,7 @@ final class ReferenceDatabase implements TLCallback
throw new Exception("Unknown origin type provided: {$type}"); throw new Exception("Unknown origin type provided: {$type}");
} }
$originContext = self::CONSTRUCTOR_CONTEXT[$type]; $originContext = self::CONSTRUCTOR_CONTEXT[$type];
//$this->API->logger->logger("Adding origin context {$originContext} for {$type}!", \danog\MadelineProto\Logger::ULTRA_VERBOSE); $this->API->logger->logger("Adding origin context {$originContext} for {$type}!", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
$this->cacheContexts[] = $originContext; $this->cacheContexts[] = $originContext;
} }
public function addOrigin(array $data = []): void public function addOrigin(array $data = []): void
@ -218,7 +218,7 @@ final class ReferenceDatabase implements TLCallback
} }
$originType = \array_pop($this->cacheContexts); $originType = \array_pop($this->cacheContexts);
if (!isset($this->cache[$key])) { if (!isset($this->cache[$key])) {
//$this->API->logger->logger("Removing origin context {$originType} for {$data['_']}, nothing in the reference cache!", \danog\MadelineProto\Logger::ULTRA_VERBOSE); $this->API->logger->logger("Removing origin context {$originType} for {$data['_']}, nothing in the reference cache!", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
return; return;
} }
$cache = $this->cache[$key]; $cache = $this->cache[$key];

View File

@ -169,7 +169,7 @@ trait UpdateHandler
'offset' => $offset, 'offset' => $offset,
'limit' => $limit, 'limit' => $limit,
'timeout' => $timeout 'timeout' => $timeout
] = array_merge(['offset' => 0, 'limit' => null, 'timeout' => INF], $params); ] = \array_merge(['offset' => 0, 'limit' => null, 'timeout' => INF], $params);
if (!$this->updates) { if (!$this->updates) {
try { try {