Added monitor() method to \TamerLib\Classes > RedisServer & some applide some other minor changes.

This commit is contained in:
Netkas 2023-06-16 02:04:59 -04:00
parent f0ceb736d1
commit 599224ebbd
No known key found for this signature in database
GPG key ID: 5DAF58535614062B

View file

@ -10,6 +10,7 @@
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use TamerLib\Exceptions\ServerException; use TamerLib\Exceptions\ServerException;
use TamerLib\Objects\ServerConfiguration; use TamerLib\Objects\ServerConfiguration;
use TamerLib\tm;
class RedisServer class RedisServer
{ {
@ -72,7 +73,7 @@
return true; return true;
} }
Log::verbose('net.nosial.tamerlib', 'Starting server on port ' . $this->configuration->getPort() . '.'); Log::verbose(Utilities::getName(), 'Starting server on port ' . $this->configuration->getPort() . '.');
$this->server_process = new Process([$this->cmd, '--port', $this->configuration->getPort()]); $this->server_process = new Process([$this->cmd, '--port', $this->configuration->getPort()]);
$this->server_process->start(); $this->server_process->start();
@ -82,6 +83,7 @@
while(true) while(true)
{ {
tm::monitor(-1);
if($timeout_counter >= $timeout) if($timeout_counter >= $timeout)
{ {
throw new ServerException('Redis server failed to start within ' . $timeout . ' seconds.'); throw new ServerException('Redis server failed to start within ' . $timeout . ' seconds.');
@ -108,7 +110,7 @@
} }
} }
Log::verbose('net.nosial.tamerlib', sprintf('Server listening on %s:%s.', $this->configuration->getHost(), $this->configuration->getPort())); Log::verbose(Utilities::getName(), sprintf('Server listening on %s:%s.', $this->configuration->getHost(), $this->configuration->getPort()));
return true; return true;
} }
@ -125,10 +127,43 @@
} }
$this->server_process->stop(); $this->server_process->stop();
Log::verbose('net.nosial.tamerlib', sprintf('Server stopped on %s:%s.', $this->configuration->getHost(), $this->configuration->getPort())); Log::verbose(Utilities::getName(), sprintf('Server stopped on %s:%s.', $this->configuration->getHost(), $this->configuration->getPort()));
return true; return true;
} }
/**
* Monitors the server process and outputs the latest output.
*
* @param int $timeout
* @return void
* @throws ServerException
*/
public function monitor(int $timeout=0): void
{
$start_time = time();
while(true)
{
print(Utilities::getLatestOutput($this->server_process));
if(!$this->isRunning())
{
Log::warning(Utilities::getName(), sprintf('Server on %s:%s is not running, restarting.', $this->configuration->getHost(), $this->configuration->getPort()));
$this->start();
}
if($timeout < 0)
{
return;
}
if($timeout > 0 && (time() - $start_time) >= $timeout)
{
return;
}
}
}
/** /**
* Terminates the Redis server. * Terminates the Redis server.
*/ */