1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 06:59:01 +01:00

Fix windows phar, plus multiple other fixes

This commit is contained in:
Daniil Gentili 2021-05-09 22:33:59 +02:00
parent 5693fc0bd8
commit a0f6402d0a
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 65 additions and 17 deletions

View File

@ -410,11 +410,21 @@ class API extends InternalDoc
*/
public function startAndLoop(string $eventHandler): void
{
$errors = [];
$started = false;
while (true) {
try {
Tools::wait($this->startAndLoopAsync($eventHandler));
Tools::wait($this->startAndLoopAsyncInternal($eventHandler, $started));
return;
} catch (\Throwable $e) {
$t = \time();
$errors = [$t => $errors[$t] ?? 0];
$errors[$t]++;
if ($errors[$t] > 10 && (!$this->inited() || !$started)) {
$this->logger->logger("More than 10 errors in a second and not inited, exiting!", Logger::FATAL_ERROR);
return;
}
echo $e;
$this->logger->logger((string) $e, Logger::FATAL_ERROR);
$this->report("Surfaced: $e");
}
@ -434,17 +444,27 @@ class API extends InternalDoc
$eventHandler = \array_fill_keys(\array_keys($instances), $eventHandler);
}
$errors = [];
$started = array_fill_keys(array_keys($instances), false);
$instanceOne = \array_values($instances)[0];
while (true) {
try {
$promises = [];
foreach ($instances as $k => $instance) {
$instance->start(['async' => false]);
$promises []= $instance->startAndLoopAsync($eventHandler[$k]);
$promises []= $instance->startAndLoopAsyncInternal($eventHandler[$k], $started[$k]);
}
Tools::wait(Tools::all($promises));
return;
} catch (\Throwable $e) {
$t = \time();
$errors = [$t => $errors[$t] ?? 0];
$errors[$t]++;
if ($errors[$t] > 10 && array_sum($started) !== count($eventHandler)) {
$instanceOne->logger("More than 10 errors in a second and not inited, exiting!", Logger::FATAL_ERROR);
return;
}
echo $e;
$instanceOne->logger((string) $e, Logger::FATAL_ERROR);
$instanceOne->report("Surfaced: $e");
}
@ -461,14 +481,28 @@ class API extends InternalDoc
*/
public function startAndLoopAsync(string $eventHandler): \Generator
{
$errors = [];
$started = false;
return $this->startAndLoopAsyncInternal($eventHandler, $started);
}
/**
* 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 startAndLoopAsyncInternal(string $eventHandler, bool &$started): \Generator
{
$this->async(true);
if (!yield from $this->reconnectFull()) {
return;
}
$started = false;
$errors = [];
while (true) {
try {
yield $this->start();

View File

@ -214,7 +214,7 @@ final class APIWrapper
// Truncate legacy session
yield (yield open($this->session->getLegacySessionPath(), 'w'))->close();
if (Magic::$enablePeriodicLogging) {
if (!Magic::$suspendPeriodicLogging) {
Logger::log('Saved session!');
}
return true;

View File

@ -14,6 +14,7 @@ trait NullCacheTrait
*/
protected function getCache(string $key, $default = null)
{
return $default;
}
/**

View File

@ -183,7 +183,7 @@ class Lang
'apiManualInstructions1' => 'Go to API development tools',
'apiManualInstructions2' => 'Click on create application',
'apiAppInstructionsManual0' => 'your app\'s name, can be anything',
'apiAppInstructionsManual1' => 'your app\'s short name, alphanumeric',
'apiAppInstructionsManual1' => 'your app\'s short name, alphanumeric, 5-32 characters',
'apiAppInstructionsManual2' => 'your app/website\'s URL, or t.me/yourusername',
'apiAppInstructionsManual3' => 'anything',
'apiAppInstructionsManual4' => 'Describe your app here',
@ -195,7 +195,7 @@ class Lang
'apiAutoPrompt1' => 'Enter the verification code you received in Telegram: ',
'apiAppWeb' => 'Enter API information',
'apiAppInstructionsAuto0' => 'Enter the app\'s name, can be anything: ',
'apiAppInstructionsAuto1' => 'Enter the app\'s short name, alphanumeric: ',
'apiAppInstructionsAuto1' => 'Enter the app\'s short name, alphanumeric, 5-32 characters: ',
'apiAppInstructionsAuto2' => 'Enter the app/website\'s URL, or t.me/yourusername: ',
'apiAppInstructionsAuto3' => 'Enter the app platform: ',
'apiAppInstructionsAuto4' => 'Describe your app: ',
@ -334,7 +334,7 @@ class Lang
'apiManualInstructions1' => 'Go to API development tools',
'apiManualInstructions2' => 'Click on create application',
'apiAppInstructionsManual0' => 'your app\'s name, can be anything',
'apiAppInstructionsManual1' => 'your app\'s short name, alphanumeric',
'apiAppInstructionsManual1' => 'your app\'s short name, alphanumeric, 5-32 characters',
'apiAppInstructionsManual2' => 'your app/website\'s URL, or t.me/yourusername',
'apiAppInstructionsManual3' => 'anything',
'apiAppInstructionsManual4' => 'Describe your app here',
@ -346,7 +346,7 @@ class Lang
'apiAutoPrompt1' => 'Enter the verification code you received in Telegram: ',
'apiAppWeb' => 'Enter API information',
'apiAppInstructionsAuto0' => 'Enter the app\'s name, can be anything: ',
'apiAppInstructionsAuto1' => 'Enter the app\'s short name, alphanumeric: ',
'apiAppInstructionsAuto1' => 'Enter the app\'s short name, alphanumeric, 5-32 characters: ',
'apiAppInstructionsAuto2' => 'Enter the app/website\'s URL, or t.me/yourusername: ',
'apiAppInstructionsAuto3' => 'Enter the app platform: ',
'apiAppInstructionsAuto4' => 'Describe your app: ',

View File

@ -353,7 +353,11 @@ class Logger
*/
public function logger($param, int $level = self::NOTICE, string $file = ''): void
{
if ($level > $this->level || $this->mode === self::NO_LOGGER) {
if ($level > $this->level) {
return;
}
if (Magic::$suspendPeriodicLogging) {
Magic::$suspendPeriodicLogging->promise()->onResolve(fn () => $this->logger($param, $level, $file));
return;
}
if (!self::$printed) {

View File

@ -46,7 +46,7 @@ class GarbageCollector
\gc_collect_cycles();
static::$memoryConsumption = static::getMemoryConsumption();
$cleanedMemory = $currentMemory - static::$memoryConsumption;
if (Magic::$enablePeriodicLogging) {
if (!Magic::$suspendPeriodicLogging) {
Logger::log("gc_collect_cycles done. Cleaned memory: $cleanedMemory Mb", Logger::VERBOSE);
}
}
@ -56,7 +56,7 @@ class GarbageCollector
private static function getMemoryConsumption(): int
{
$memory = \round(\memory_get_usage()/1024/1024, 1);
if (Magic::$enablePeriodicLogging) {
if (!Magic::$suspendPeriodicLogging) {
Logger::log("Memory consumption: $memory Mb", Logger::ULTRA_VERBOSE);
}
return (int) $memory;

View File

@ -19,6 +19,7 @@
namespace danog\MadelineProto;
use Amp\Deferred;
use Amp\Loop;
use Amp\Loop\Driver;
use danog\MadelineProto\TL\Conversion\Extension;
@ -222,9 +223,9 @@ class Magic
/**
* Whether to suspend certain stdout log printing, when reading input.
*
* @var bool
* @var ?Deferred
*/
public static $enablePeriodicLogging = true;
public static $suspendPeriodicLogging;
/**
* All mime types.
*
@ -470,7 +471,13 @@ class Magic
*/
public static function togglePeriodicLogging(): void
{
self::$enablePeriodicLogging = !self::$enablePeriodicLogging;
if (self::$suspendPeriodicLogging) {
$deferred = self::$suspendPeriodicLogging;
self::$suspendPeriodicLogging = null;
$deferred->resolve();
} else {
self::$suspendPeriodicLogging = new Deferred;
}
}
/**

View File

@ -245,7 +245,7 @@ abstract class Serialization
*/
public static function tryConnect(string $ipcPath, Promise $cancelConnect, ?Deferred $cancelFull = null): \Generator
{
for ($x = 0; $x < 30; $x++) {
for ($x = 0; $x < 60; $x++) {
Logger::log("Trying to connect to IPC socket...");
try {
\clearstatcache(true, $ipcPath);

View File

@ -79,6 +79,8 @@ abstract class SettingsAbstract
/**
* Get whether this setting was changed, also applies changes.
*
* @internal
*
* @return boolean
*/
public function hasChanged(): bool

View File

@ -19,7 +19,7 @@ if (!isset($argv[3])) {
@\unlink($argv[2]);
$p = new Phar(__DIR__.'/../'.$argv[2], 0, $argv[2]);
$p->buildFromDirectory(\realpath($argv[1]), '/^((?!tests).)*(\.php|\.py|\.tl|\.json|\.dat|\.h)$/i');
$p->buildFromDirectory(\realpath($argv[1]));
$p->addFromString('vendor/danog/madelineproto/.git/refs/heads/master', $argv[3]);
$p->addFromString('.git/refs/heads/master', $argv[3]);