improved quit and restart coding

This commit is contained in:
Steffen Schröder 2014-08-02 20:51:48 +02:00
parent 297d251586
commit 58a668cf65
3 changed files with 108 additions and 80 deletions

View File

@ -169,26 +169,13 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
* @param bool $errorPrefix
*/
public function quit($message = null, $errorPrefix = false) {
if ($message) {
if ($errorPrefix) {
$message = '[ERROR] ' . $message;
}
$this->log($message);
}
$this->disconnect();
exit();
}
/**
* Close the Client Connection
*/
public function disconnect() {
Connection::delete($this->client);
$this->client = null;
SystemUtil::quit($message, $errorPrefix);
}
/**
* Check Connection
* Check connection
*/
public function checkConnection() {
if ($this->client->getIdleTime() > 180) {
@ -231,43 +218,21 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
$this->callbackManager->triggerCallback(Callbacks::ONSHUTDOWN);
// Announce restart
$this->chat->sendInformation('Restarting ManiaControl...');
if ($message) {
$this->log($message);
}
$this->chat->sendInformation('Restarting ManiaControl...');
$this->log('Restarting ManiaControl!');
// Hide widgets
if ($this->client) {
$this->client->sendHideManialinkPage();
}
$this->log('Restarting ManiaControl!');
// Start new instance
SystemUtil::restart();
// Execute start script in background
if (SystemUtil::isUnix()) {
// Unix
if (!SystemUtil::checkFunctionAvailability('exec')) {
$this->log("Can't restart ManiaControl because the function 'exec' is disabled!");
return;
}
$fileName = ManiaControlDir . 'ManiaControl.sh';
if (!is_readable($fileName)) {
$this->log("Can't restart ManiaControl because the file 'ManiaControl.sh' doesn't exist or isn't readable!");
return;
}
$command = 'sh ' . escapeshellarg($fileName) . ' > /dev/null &';
exec($command);
} else {
// Windows
if (!SystemUtil::checkFunctionAvailability('system')) {
$this->log("Can't restart ManiaControl because the function 'system' is disabled!");
return;
}
$command = escapeshellarg(ManiaControlDir . "ManiaControl.bat");
system($command); // TODO: windows gets stuck here as long controller is running
}
// Quit the old instance
// Quit old instance
$this->quit('Quitting ManiaControl to restart.');
}

View File

@ -12,19 +12,16 @@ namespace ManiaControl\Utils;
class CommandLineHelper {
/**
* Get the Command Line Parameter with the given Name
* Get the command line parameter value with the given name
*
* @param string $paramName
* @return string
*/
public static function getParameter($paramName) {
global $argv;
if (!is_array($argv)) {
return null;
}
$paramName = (string)$paramName;
foreach ($argv as $arg) {
$parts = explode('=', $arg, 2);
$params = self::getAllParameters();
foreach ($params as $param) {
$parts = explode('=', $param, 2);
if (count($parts) < 2) {
continue;
}
@ -35,4 +32,14 @@ class CommandLineHelper {
}
return null;
}
/**
* Get all command line parameters
*
* @return array
*/
public static function getAllParameters() {
global $argv;
return (array)$argv;
}
}

View File

@ -41,36 +41,7 @@ class SystemUtil {
}
/**
* Get whether ManiaControl is running on Unix
*
* @return bool
*/
public static function isUnix() {
return (self::getOS() === self::OS_UNIX);
}
/**
* Check whether the given Function is available
*
* @param string $functionName
* @return bool
*/
public static function checkFunctionAvailability($functionName) {
return (function_exists($functionName) && !in_array($functionName, self::getDisabledFunctions()));
}
/**
* Get the Array of Disabled Functions
*
* @return array
*/
protected static function getDisabledFunctions() {
$disabledText = ini_get('disable_functions');
return explode(',', $disabledText);
}
/**
* Check for the Requirements to run ManiaControl
* Check for the requirements to run ManiaControl
*/
public static function checkRequirements() {
$success = true;
@ -108,7 +79,92 @@ class SystemUtil {
if (!$success) {
// Missing requirements
exit;
self::quit();
}
}
/**
* Stop ManiaControl immediately
*
* @param string $message
* @param bool $errorPrefix
*/
public static function quit($message = null, $errorPrefix = false) {
if ($message) {
if ($errorPrefix) {
$message = '[ERROR] ' . $message;
}
Logger::log($message);
}
exit;
}
/**
* Restart ManiaControl immediately
*/
public static function restart() {
if (SystemUtil::isUnix()) {
self::restartUnix();
} else {
self::restartWindows();
}
}
/**
* Get whether ManiaControl is running on Unix
*
* @return bool
*/
public static function isUnix() {
return (self::getOS() === self::OS_UNIX);
}
/**
* Perform restart on Unix
*/
private static function restartUnix() {
if (!SystemUtil::checkFunctionAvailability('exec')) {
Logger::log("Can't restart ManiaControl because the function 'exec' is disabled!");
return;
}
$fileName = ManiaControlDir . 'ManiaControl.sh';
if (!is_readable($fileName)) {
Logger::log("Can't restart ManiaControl because the file 'ManiaControl.sh' doesn't exist or isn't readable!");
return;
}
$command = 'sh ' . escapeshellarg($fileName) . ' > /dev/null &';
exec($command);
}
/**
* Check whether the given function is available
*
* @param string $functionName
* @return bool
*/
public static function checkFunctionAvailability($functionName) {
return (function_exists($functionName) && !in_array($functionName, self::getDisabledFunctions()));
}
/**
* Get the array of disabled functions
*
* @return array
*/
protected static function getDisabledFunctions() {
$disabledText = ini_get('disable_functions');
return explode(',', $disabledText);
}
/**
* Perform restart on Windows
*/
private static function restartWindows() {
if (!SystemUtil::checkFunctionAvailability('system')) {
Logger::log("Can't restart ManiaControl because the function 'system' is disabled!");
return;
}
$command = escapeshellarg(ManiaControlDir . "ManiaControl.bat");
system($command); // TODO: windows gets stuck here as long controller is running
}
}