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,55 +45,60 @@ 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) {
$backtrace = debug_backtrace(0); $needs_restart = true;
try { }
error_reporting(E_ALL); $backtrace = debug_backtrace(0);
ini_set("log_errors", 1); try {
ini_set("error_log", dirname(end($backtrace)['file'])."/MadelineProto.log"); error_reporting(E_ALL);
error_log('Enabled PHP logging'); ini_set("log_errors", 1);
} catch (\danog\MadelineProto\Exception $e) { ini_set("error_log", dirname(end($backtrace)['file']) . "/MadelineProto.log");
$this->logger->logger("Could not enable PHP logging"); error_log('Enabled PHP logging');
} } catch (\danog\MadelineProto\Exception $e) {
$this->logger->logger("Could not enable PHP logging");
$lockfile = dirname(end($backtrace)['file']).'/bot.lock'; }
unset($backtrace); $this->logger->logger($needs_restart ? 'Will self-restart' : 'Will not self-restart');
$try_locking = true;
if (!file_exists($lockfile)) { $lockfile = dirname(end($backtrace)['file']) . '/bot.lock';
touch($lockfile); unset($backtrace);
$lock = fopen('bot.lock', 'r+'); $try_locking = true;
} else if (isset($GLOBALS['lock'])) { if (!file_exists($lockfile)) {
$try_locking = false; touch($lockfile);
$lock = $GLOBALS['lock']; $lock = fopen('bot.lock', 'r+');
} else { } else if (isset($GLOBALS['lock'])) {
$lock = fopen('bot.lock', 'r+'); $try_locking = false;
} $lock = $GLOBALS['lock'];
if ($try_locking) { } else {
$try = 1; $lock = fopen('bot.lock', 'r+');
$locked = false; }
while (!$locked) { if ($try_locking) {
$locked = flock($lock, LOCK_EX | LOCK_NB); $try = 1;
if (!$locked) { $locked = false;
$this->closeConnection("Bot is already running"); while (!$locked) {
if ($try++ >= 30) { $locked = flock($lock, LOCK_EX | LOCK_NB);
exit; if (!$locked) {
} $this->closeConnection("Bot is already running");
sleep(1); if ($try++ >= 30) {
exit;
} }
sleep(1);
} }
} }
register_shutdown_function(function () use ($lock) {
flock($lock, LOCK_UN);
fclose($lock);
$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");
} }
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']) { 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');