Enable shutdown function and lock manager even on unlimited webhosts

This commit is contained in:
Daniil Gentili 2018-12-27 22:24:45 +01:00
parent 6e366c5c08
commit aea8f0c3e3

View File

@ -45,9 +45,12 @@ trait Loop
$this->loop_callback = null;
}
if (php_sapi_name() !== 'cli') {
$needs_restart = false;
try {
set_time_limit(-1);
} catch (\danog\MadelineProto\Exception $e) {
$needs_restart = true;
}
$backtrace = debug_backtrace(0);
try {
error_reporting(E_ALL);
@ -57,6 +60,7 @@ trait Loop
} catch (\danog\MadelineProto\Exception $e) {
$this->logger->logger("Could not enable PHP logging");
}
$this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
$lockfile = dirname(end($backtrace)['file']) . '/bot.lock';
unset($backtrace);
@ -85,16 +89,17 @@ trait Loop
}
}
register_shutdown_function(function () use ($lock) {
register_shutdown_function(function () use ($lock, $needs_restart) {
flock($lock, LOCK_UN);
fclose($lock);
if ($needs_restart) {
$a = fsockopen((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp') . '://' . $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']);
fwrite($a, $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . "\r\n" . 'Host: ' . $_SERVER['SERVER_NAME'] . "\r\n\r\n");
}
});
$this->closeConnection("Bot was started");
}
}
if (!$this->settings['updates']['handle_updates']) {
$this->settings['updates']['handle_updates'] = true;
}