From 599224ebbdadd50910cd6d973f2ee1cd6631828d Mon Sep 17 00:00:00 2001 From: Netkas Date: Fri, 16 Jun 2023 02:04:59 -0400 Subject: [PATCH] Added monitor() method to \TamerLib\Classes > RedisServer & some applide some other minor changes. --- src/TamerLib/Classes/RedisServer.php | 41 ++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/TamerLib/Classes/RedisServer.php b/src/TamerLib/Classes/RedisServer.php index 941f1ba..7f0808a 100644 --- a/src/TamerLib/Classes/RedisServer.php +++ b/src/TamerLib/Classes/RedisServer.php @@ -10,6 +10,7 @@ use Symfony\Component\Process\Process; use TamerLib\Exceptions\ServerException; use TamerLib\Objects\ServerConfiguration; + use TamerLib\tm; class RedisServer { @@ -72,7 +73,7 @@ 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->start(); @@ -82,6 +83,7 @@ while(true) { + tm::monitor(-1); if($timeout_counter >= $timeout) { 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; } @@ -125,10 +127,43 @@ } $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; } + /** + * 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. */