Misc improvements

This commit is contained in:
Daniil Gentili 2020-02-28 15:19:11 +01:00
parent ac9a0958d1
commit 997b3d3b3b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
5 changed files with 36 additions and 35 deletions

View File

@ -81,7 +81,7 @@ trait DialogHandler
$last_id = $dialog['top_message']; $last_id = $dialog['top_message'];
} }
foreach ($res['messages'] as $message) { foreach ($res['messages'] as $message) {
if ($this->getId($message) === $last_peer && $last_id === $message['id']) { if ($message['_'] !== 'messageEmpty' && $this->getId($message) === $last_peer && $last_id === $message['id']) {
$last_date = $message['date']; $last_date = $message['date'];
break; break;
} }

View File

@ -16,13 +16,11 @@
* *
* @link https://docs.madelineproto.xyz MadelineProto documentation * @link https://docs.madelineproto.xyz MadelineProto documentation
*/ */
namespace danog\MadelineProto\Wrappers; namespace danog\MadelineProto\Wrappers;
use Amp\Promise; use Amp\Promise;
use danog\MadelineProto\Shutdown; use danog\MadelineProto\Shutdown;
use danog\MadelineProto\Tools; use danog\MadelineProto\Tools;
/** /**
* Manages logging in and out. * Manages logging in and out.
*/ */
@ -35,7 +33,6 @@ trait Loop
* @var boolean * @var boolean
*/ */
private $stopLoop = false; private $stopLoop = false;
/** /**
* Start MadelineProto's update handling loop, or run the provided async callable. * Start MadelineProto's update handling loop, or run the provided async callable.
* *
@ -43,15 +40,15 @@ trait Loop
* *
* @return mixed * @return mixed
*/ */
public function loop($callback = null): \Generator public function loop($callback = null) : \Generator
{ {
if (\is_callable($callback)) { if (\is_callable($callback)) {
$this->logger->logger('Running async callable'); $this->logger->logger('Running async callable');
return yield $callback(); return (yield $callback());
} }
if ($callback instanceof Promise) { if ($callback instanceof Promise) {
$this->logger->logger('Resolving async promise'); $this->logger->logger('Resolving async promise');
return yield $callback; return (yield $callback);
} }
if (!$this->authorized) { if (!$this->authorized) {
$this->logger->logger('Not authorized, not starting event loop', \danog\MadelineProto\Logger::FATAL_ERROR); $this->logger->logger('Not authorized, not starting event loop', \danog\MadelineProto\Logger::FATAL_ERROR);
@ -65,7 +62,6 @@ trait Loop
if (!\is_callable($this->loop_callback) || \is_array($this->loop_callback) && $this->loop_callback[1] === 'onLoop' && !\method_exists(...$this->loop_callback)) { if (!\is_callable($this->loop_callback) || \is_array($this->loop_callback) && $this->loop_callback[1] === 'onLoop' && !\method_exists(...$this->loop_callback)) {
$this->loop_callback = null; $this->loop_callback = null;
} }
static $inited = false; static $inited = false;
if (PHP_SAPI !== 'cli' && !$inited) { if (PHP_SAPI !== 'cli' && !$inited) {
$needs_restart = true; $needs_restart = true;
@ -75,11 +71,11 @@ trait Loop
$needs_restart = true; $needs_restart = true;
} }
if (isset($_REQUEST['MadelineSelfRestart'])) { if (isset($_REQUEST['MadelineSelfRestart'])) {
$this->logger->logger("Self-restarted, restart token ".$_REQUEST['MadelineSelfRestart']); $this->logger->logger("Self-restarted, restart token " . $_REQUEST['MadelineSelfRestart']);
} }
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart'); $this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
$backtrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $backtrace = \debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$lockfile = \dirname(\end($backtrace)['file']).'/bot'.$this->authorization['user']['id'].'.lock'; $lockfile = \dirname(\end($backtrace)['file']) . '/bot' . $this->authorization['user']['id'] . '.lock';
unset($backtrace); unset($backtrace);
$try_locking = true; $try_locking = true;
if (!\file_exists($lockfile)) { if (!\file_exists($lockfile)) {
@ -92,6 +88,7 @@ trait Loop
$lock = \fopen($lockfile, 'r+'); $lock = \fopen($lockfile, 'r+');
} }
if ($try_locking) { if ($try_locking) {
$this->logger->logger('Will try locking');
$try = 1; $try = 1;
$locked = false; $locked = false;
while (!$locked) { while (!$locked) {
@ -104,15 +101,21 @@ trait Loop
\sleep(1); \sleep(1);
} }
} }
$this->logger->logger('Locked!');
} }
Shutdown::addCallback(static function () use ($lock) { $this->logger->logger("Adding unlock callback!");
$logger =& $this->logger;
$id = Shutdown::addCallback(static function () use ($lock, $logger) {
$logger->logger("Unlocking lock!");
\flock($lock, LOCK_UN); \flock($lock, LOCK_UN);
\fclose($lock); \fclose($lock);
$logger->logger("Unlocked lock!");
}); });
$this->logger->logger("Added unlock callback with id $id!");
if ($needs_restart) { if ($needs_restart) {
$logger =& $this->logger; $this->logger->logger("Adding restart callback!");
Shutdown::addCallback(static function () use (&$logger) { $id = Shutdown::addCallback(static function () use(&$logger) {
$address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME']; $address = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp') . '://' . $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT']; $port = $_SERVER['SERVER_PORT'];
$uri = $_SERVER['REQUEST_URI']; $uri = $_SERVER['REQUEST_URI'];
$params = $_GET; $params = $_GET;
@ -120,7 +123,7 @@ trait Loop
$url = \explode('?', $uri, 2)[0] ?? ''; $url = \explode('?', $uri, 2)[0] ?? '';
$query = \http_build_query($params); $query = \http_build_query($params);
$uri = \implode('?', [$url, $query]); $uri = \implode('?', [$url, $query]);
$payload = $_SERVER['REQUEST_METHOD'].' '.$uri.' '.$_SERVER['SERVER_PROTOCOL']."\r\n".'Host: '.$_SERVER['SERVER_NAME']."\r\n\r\n"; $payload = $_SERVER['REQUEST_METHOD'] . ' ' . $uri . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\r\n" . 'Host: ' . $_SERVER['SERVER_NAME'] . "\r\n\r\n";
$logger->logger("Connecting to {$address}:{$port}"); $logger->logger("Connecting to {$address}:{$port}");
$a = \fsockopen($address, $port); $a = \fsockopen($address, $port);
$logger->logger("Sending self-restart payload"); $logger->logger("Sending self-restart payload");
@ -129,8 +132,11 @@ trait Loop
$logger->logger("Payload sent with token {$params['MadelineSelfRestart']}, waiting for self-restart"); $logger->logger("Payload sent with token {$params['MadelineSelfRestart']}, waiting for self-restart");
\sleep(10); \sleep(10);
\fclose($a); \fclose($a);
$logger->logger("Shutdown of self-restart callback");
}, 'restarter'); }, 'restarter');
$this->logger->logger("Added restart callback with ID $id!");
} }
$this->logger->logger("Done webhost init process!");
$this->closeConnection('Bot was started'); $this->closeConnection('Bot was started');
$inited = true; $inited = true;
} }
@ -166,7 +172,7 @@ trait Loop
* *
* @return void * @return void
*/ */
public function stop(): void public function stop()
{ {
$this->stopLoop = true; $this->stopLoop = true;
$this->signalUpdate(); $this->signalUpdate();
@ -176,7 +182,7 @@ trait Loop
* *
* @return void * @return void
*/ */
public function restart(): void public function restart()
{ {
$this->stop(); $this->stop();
} }
@ -185,7 +191,7 @@ trait Loop
* *
* @return Promise * @return Promise
*/ */
public function loopFork(): Promise public function loopFork() : Promise
{ {
return Tools::callFork($this->loop()); return Tools::callFork($this->loop());
} }
@ -196,14 +202,14 @@ trait Loop
* *
* @return void * @return void
*/ */
public function closeConnection($message = 'OK!'): void public function closeConnection($message = 'OK!')
{ {
if (PHP_SAPI === 'cli' || isset($GLOBALS['exited']) || \headers_sent()) { if (PHP_SAPI === 'cli' || isset($GLOBALS['exited']) || \headers_sent()) {
return; return;
} }
$this->logger->logger($message); $this->logger->logger($message);
$buffer = @\ob_get_clean() ?: ''; $buffer = @\ob_get_clean() ?: '';
$buffer .= '<html><body><h1>'.\htmlentities($message).'</h1></body></html>'; $buffer .= '<html><body><h1>' . \htmlentities($message) . '</h1></body></html>';
\ignore_user_abort(true); \ignore_user_abort(true);
\header('Connection: close'); \header('Connection: close');
\header('Content-Type: text/html'); \header('Content-Type: text/html');
@ -214,4 +220,4 @@ trait Loop
\fastcgi_finish_request(); \fastcgi_finish_request();
} }
} }
} }

View File

@ -25,18 +25,7 @@ function __destructure($list, $value)
} }
return $res; return $res;
} }
if (!\function_exists('is_iterable')) {
function is_iterable($var)
{
return \is_array($var) || $var instanceof Traversable;
}
}
if (!\function_exists('error_clear_last')) {
function error_clear_last()
{
@\trigger_error("");
}
}
trait MyCallableMaker trait MyCallableMaker
{ {
use \Amp\CallableMaker { use \Amp\CallableMaker {

View File

@ -36,7 +36,8 @@ final class DataCenterTest extends TestCase
'protocol' => $protocol, 'protocol' => $protocol,
'obfuscated' => $obfuscated, 'obfuscated' => $obfuscated,
'transport' => $transport, 'transport' => $transport,
'do_not_retry' => true 'do_not_retry' => true,
'timeout' => 10
], ],
], ],
'logger' => [ 'logger' => [

View File

@ -60,7 +60,12 @@ composer config platform.php "7.4"
composer clearcache composer clearcache
composer update composer update
composer require amphp/mysql composer require amphp/mysql
[ $PHP_MAJOR_VERSION -eq 5 ] && composer require dstuecken/php7ify [ $PHP_MAJOR_VERSION -eq 5 ] && composer require dstuecken/php7ify && composer require symfony/polyfill-php70
[ $PHP_MAJOR_VERSION -eq 7 ] && [ $PHP_MINOR_VERSION -eq 0 ] && {
composer require symfony/polyfill-php71
composer require symfony/polyfill-php72
composer require symfony/polyfill-php73
}
composer dumpautoload --optimize composer dumpautoload --optimize
cp -a $madelinePath/src vendor/danog/madelineproto cp -a $madelinePath/src vendor/danog/madelineproto
cd .. cd ..