Updated the start/stop server icons; added routes to handle server stop/start
This commit is contained in:
@ -20,7 +20,7 @@ class Server {
|
||||
$dirBits = explode('/', $dir);
|
||||
$this->rootDir = $dir;
|
||||
$this->serverName = $dirBits[count($dirBits) - 1];
|
||||
$this->pidFilePath = $dir . '/pid.txt';
|
||||
$this->pidFilePath = $this->rootDir . '/pid.txt';
|
||||
|
||||
// get server version
|
||||
$versionFile = join('/', array($this->rootDir, 'current_version.txt'));
|
||||
@ -28,22 +28,57 @@ class Server {
|
||||
$this->serverVersion = file($versionFile)[0];
|
||||
}
|
||||
|
||||
// search for server state
|
||||
// search for server PID file
|
||||
if (file_exists($this->pidFilePath)) {
|
||||
$this->state = true;
|
||||
// get and search for PID
|
||||
$pid = trim($this->getPid());
|
||||
if (file_exists('/proc/' . $pid)) {
|
||||
// set server state to true since it is running
|
||||
$this->state = true;
|
||||
} else {
|
||||
// delete the PID file since it's no longer needed
|
||||
unlink($this->pidFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// start the server
|
||||
public function start(): void {
|
||||
// TODO - for now just set the state to true
|
||||
$command = $this->rootDir . '/start.sh > /dev/null 2>&1 & echo $!';
|
||||
exec($command, $output);
|
||||
// use lsof to
|
||||
$pidResult = exec('lsof ' . $this->rootDir . '/server*.jar | grep .jar');
|
||||
$pid = preg_split('/\s+/', $pidResult, -1, PREG_SPLIT_NO_EMPTY)[1];
|
||||
|
||||
$pidFile = fopen($this->pidFilePath, 'w');
|
||||
fwrite($pidFile, $pid);
|
||||
fclose($pidFile);
|
||||
|
||||
// set server state to true
|
||||
$this->state = true;
|
||||
}
|
||||
|
||||
// stop the server
|
||||
public function stop(): void {
|
||||
// TODO - for now just set the state to false
|
||||
$this->state = false;
|
||||
$pid = $this->getPid();
|
||||
if ($pid != -1) {
|
||||
// kill the process gracefully
|
||||
exec('kill -15 ' . $pid);
|
||||
// set server state to false
|
||||
$this->state = false;
|
||||
}
|
||||
}
|
||||
|
||||
// get the server's PID
|
||||
private function getPid(): int {
|
||||
// search for server PID file
|
||||
if (file_exists($this->pidFilePath)) {
|
||||
// get and return server PID
|
||||
return trim(file($this->pidFilePath)[0]);
|
||||
}
|
||||
|
||||
// return a 'false' result
|
||||
return -1;
|
||||
}
|
||||
|
||||
// getters & setters
|
||||
|
@ -67,3 +67,33 @@ $app->post('/create', function (Request $request, Response $response, $args) {
|
||||
->withHeader('Location', '/')
|
||||
->withStatus(302);
|
||||
});
|
||||
|
||||
// server start route
|
||||
$app->get('/server/{serverName}/start', function (Request $request, Response $response, $args) {
|
||||
$config = $this->get('config');
|
||||
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
|
||||
|
||||
// create server object and start it
|
||||
$server = new Server($serverDir);
|
||||
$server->start();
|
||||
|
||||
// redirect the user back to the home page
|
||||
return $response
|
||||
->withHeader('Location', '/')
|
||||
->withStatus(302);
|
||||
})->setName('start');
|
||||
|
||||
// server stop route
|
||||
$app->get('/server/{serverName}/stop', function (Request $request, Response $response, $args) {
|
||||
$config = $this->get('config');
|
||||
$serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
|
||||
|
||||
// create server object and start it
|
||||
$server = new Server($serverDir);
|
||||
$server->stop();
|
||||
|
||||
// redirect the user back to the home page
|
||||
return $response
|
||||
->withHeader('Location', '/')
|
||||
->withStatus(302);
|
||||
})->setName('stop');
|
||||
|
Reference in New Issue
Block a user