Minor fixes

This commit is contained in:
Daniil Gentili 2019-06-13 19:46:58 +02:00
parent b2bd441f42
commit c09488aa12
8 changed files with 36 additions and 62 deletions

View File

@ -120,7 +120,10 @@ class API extends APIFactory
}
Logger::log((string) $e, Logger::ERROR);
if (!$changed) throw $e;
if (!$changed) {
throw $e;
}
$unserialized = \danog\Serialization::unserialize($tounserialize);
} catch (\Throwable $e) {
Logger::log((string) $e, Logger::ERROR);

View File

@ -214,6 +214,9 @@ class Connection
$this->API->logger->logger("Reconnecting DC {$this->datacenter}");
$this->disconnect();
yield $this->API->datacenter->dcConnectAsync($this->ctx->getDc());
if ($this->API->hasAllAuth() && !$this->hasPendingCalls()) {
yield $this->API->method_call_async_read('ping', ['ping_id' => $this->random_int()], ['datacenter' => $this->datacenter]);
}
}
public function hasPendingCalls()

View File

@ -49,7 +49,7 @@ class FeedLoop extends ResumableSignalLoop
return false;
}
while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) {
if (yield $this->waitSignal($this->pause())) {
return;
}
@ -57,7 +57,7 @@ class FeedLoop extends ResumableSignalLoop
$this->state = $this->channelId === false ? (yield $API->load_update_state_async()) : $API->loadChannelState($this->channelId);
while (true) {
while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) {
if (yield $this->waitSignal($this->pause())) {
return;
}
@ -220,7 +220,10 @@ class FeedLoop extends ResumableSignalLoop
$this->API->logger->logger("Not enough data: for message update $log, getting difference...", \danog\MadelineProto\Logger::VERBOSE);
$update = ['_' => 'updateChannelTooLong'];
if ($channelId && $to) $channelId = false;
if ($channelId && $to) {
$channelId = false;
}
}
break;
default:
@ -264,21 +267,6 @@ class FeedLoop extends ResumableSignalLoop
}
}
public function has_all_auth()
{
if ($this->API->isInitingAuthorization()) {
return false;
}
foreach ($this->API->datacenter->sockets as $dc) {
if (!$dc->authorized || $dc->temp_auth_key === null) {
return false;
}
}
return true;
}
public function __toString(): string
{
return !$this->channelId ? 'update feed loop generic' : "update feed loop channel {$this->channelId}";

View File

@ -46,7 +46,7 @@ class SeqLoop extends ResumableSignalLoop
return false;
}
while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) {
if (yield $this->waitSignal($this->pause())) {
return;
}
@ -54,7 +54,7 @@ class SeqLoop extends ResumableSignalLoop
$this->state = yield $API->load_update_state_async();
while (true) {
while (!$this->API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
while (!$this->API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) {
if (yield $this->waitSignal($this->pause())) {
return;
}
@ -134,20 +134,6 @@ class SeqLoop extends ResumableSignalLoop
$this->pendingWakeups += $wakeups;
}
public function has_all_auth()
{
if ($this->API->isInitingAuthorization()) {
return false;
}
foreach ($this->API->datacenter->sockets as $dc) {
if (!$dc->authorized || $dc->temp_auth_key === null) {
return false;
}
}
return true;
}
public function __toString(): string
{

View File

@ -47,7 +47,7 @@ class UpdateLoop extends ResumableSignalLoop
$API = $this->API;
$feeder = $this->feeder = $API->feeders[$this->channelId];
while (!$API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
while (!$API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) {
if (yield $this->waitSignal($this->pause())) {
$API->logger->logger("Exiting $this due to signal");
@ -59,7 +59,7 @@ class UpdateLoop extends ResumableSignalLoop
$timeout = $API->settings['updates']['getdifference_interval'];
$first = true;
while (true) {
while (!$API->settings['updates']['handle_updates'] || !$this->has_all_auth()) {
while (!$API->settings['updates']['handle_updates'] || !$API->hasAllAuth()) {
if (yield $this->waitSignal($this->pause())) {
$API->logger->logger("Exiting $this due to signal");
@ -206,21 +206,6 @@ class UpdateLoop extends ResumableSignalLoop
$this->toPts = $toPts;
}
public function has_all_auth()
{
if ($this->API->isInitingAuthorization()) {
return false;
}
foreach ($this->API->datacenter->sockets as $dc) {
if (!$dc->authorized || $dc->temp_auth_key === null) {
return false;
}
}
return true;
}
public function __toString(): string
{
return !$this->channelId ? 'getUpdate loop generic' : "getUpdate loop channel {$this->channelId}";

View File

@ -264,6 +264,20 @@ class MTProto extends AsyncConstruct implements TLCallback
return $this->datacenter->fileGetContents($url);
}
public function hasAllAuth()
{
if ($this->isInitingAuthorization()) {
return false;
}
foreach ($this->datacenter->sockets as $dc) {
if (!$dc->authorized || $dc->temp_auth_key === null) {
return false;
}
}
return true;
}
public function __wakeup()
{
$backtrace = debug_backtrace(0, 3);
@ -885,11 +899,6 @@ class MTProto extends AsyncConstruct implements TLCallback
return in_array($this->datacenter->sockets[$datacenter]->getCtx()->getStreamName(), [HttpStream::getName(), HttpsStream::getName()]);
}
public function close_and_reopen($datacenter)
{
$this->wait($this->datacenter->sockets[$datacenter]->reconnect());
}
// Connects to all datacenters and if necessary creates authorization keys, binds them and writes client info
public function connect_to_all_dcs_async(): \Generator
{

View File

@ -144,11 +144,11 @@ class Magic
Logger::log('Got sigint', Logger::FATAL_ERROR);
die();
});
Loop::onSignal(SIGTERM, static function () {
/*Loop::onSignal(SIGTERM, static function () {
Logger::log('Got sigterm', Logger::FATAL_ERROR);
Loop::stop();
die();
});
});*/
}
$DohConfig = new DoHConfig(
[

View File

@ -66,17 +66,17 @@ trait Loop
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$lockfile = dirname(end($backtrace)['file']).'/bot.lock';
$lockfile = dirname(end($backtrace)['file']).'/bot'.$this->authorization['user']['id'].'.lock';
unset($backtrace);
$try_locking = true;
if (!file_exists($lockfile)) {
touch($lockfile);
$lock = fopen('bot.lock', 'r+');
$lock = fopen($lockfile, 'r+');
} elseif (isset($GLOBALS['lock'])) {
$try_locking = false;
$lock = $GLOBALS['lock'];
} else {
$lock = fopen('bot.lock', 'r+');
$lock = fopen($lockfile, 'r+');
}
if ($try_locking) {
$try = 1;