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,20 +45,24 @@ trait Loop
$this->loop_callback = null; $this->loop_callback = null;
} }
if (php_sapi_name() !== 'cli') { if (php_sapi_name() !== 'cli') {
$needs_restart = false;
try { try {
set_time_limit(-1); set_time_limit(-1);
} catch (\danog\MadelineProto\Exception $e) { } catch (\danog\MadelineProto\Exception $e) {
$needs_restart = true;
}
$backtrace = debug_backtrace(0); $backtrace = debug_backtrace(0);
try { try {
error_reporting(E_ALL); error_reporting(E_ALL);
ini_set("log_errors", 1); ini_set("log_errors", 1);
ini_set("error_log", dirname(end($backtrace)['file'])."/MadelineProto.log"); ini_set("error_log", dirname(end($backtrace)['file']) . "/MadelineProto.log");
error_log('Enabled PHP logging'); error_log('Enabled PHP logging');
} catch (\danog\MadelineProto\Exception $e) { } catch (\danog\MadelineProto\Exception $e) {
$this->logger->logger("Could not enable PHP logging"); $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'; $lockfile = dirname(end($backtrace)['file']) . '/bot.lock';
unset($backtrace); unset($backtrace);
$try_locking = true; $try_locking = true;
if (!file_exists($lockfile)) { if (!file_exists($lockfile)) {
@ -85,16 +89,17 @@ trait Loop
} }
} }
register_shutdown_function(function () use ($lock) { register_shutdown_function(function () use ($lock, $needs_restart) {
flock($lock, LOCK_UN); flock($lock, LOCK_UN);
fclose($lock); fclose($lock);
$a = fsockopen((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'tls' : 'tcp').'://'.$_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT']); if ($needs_restart) {
fwrite($a, $_SERVER['REQUEST_METHOD'].' '.$_SERVER['REQUEST_URI'].' '.$_SERVER['SERVER_PROTOCOL']."\r\n".'Host: '.$_SERVER['SERVER_NAME']."\r\n\r\n"); $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"); $this->closeConnection("Bot was started");
} }
}
if (!$this->settings['updates']['handle_updates']) { if (!$this->settings['updates']['handle_updates']) {
$this->settings['updates']['handle_updates'] = true; $this->settings['updates']['handle_updates'] = true;
} }
@ -137,7 +142,7 @@ trait Loop
header('Connection: close'); header('Connection: close');
ignore_user_abort(true); ignore_user_abort(true);
ob_start(); ob_start();
echo '<html><body><h1>'.$message.'</h1></body</html>'; echo '<html><body><h1>' . $message . '</h1></body</html>';
$size = ob_get_length(); $size = ob_get_length();
header("Content-Length: $size"); header("Content-Length: $size");
header('Content-Type: text/html'); header('Content-Type: text/html');