Skip to content

Commit

Permalink
Exeptions for alredy running and not running master process
Browse files Browse the repository at this point in the history
  • Loading branch information
luzrain committed Sep 17, 2024
1 parent 6ad15ae commit d6986ca
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/Exception/AlreadyRunningException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Exception;

use Luzrain\PHPStreamServer\Server;

final class AlreadyRunningException extends \RuntimeException
{
public function __construct()
{
parent::__construct(\sprintf('%s already running', Server::NAME));
}
}
15 changes: 15 additions & 0 deletions src/Exception/NotRunningException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Luzrain\PHPStreamServer\Exception;

use Luzrain\PHPStreamServer\Server;

final class NotRunningException extends \RuntimeException
{
public function __construct()
{
parent::__construct(\sprintf('%s is not running', Server::NAME));
}
}
11 changes: 5 additions & 6 deletions src/Internal/MasterProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Luzrain\PHPStreamServer\Internal;

use Luzrain\PHPStreamServer\Console\StdoutHandler;
use Luzrain\PHPStreamServer\Exception\AlreadyRunningException;
use Luzrain\PHPStreamServer\Exception\NotRunningException;
use Luzrain\PHPStreamServer\Exception\PHPStreamServerException;
use Luzrain\PHPStreamServer\Internal\MessageBus\MessageHandler;
use Luzrain\PHPStreamServer\Internal\MessageBus\SocketFileMessageBus;
Expand Down Expand Up @@ -102,8 +104,7 @@ public function addPlugin(Plugin ...$plugins): void
public function run(bool $daemonize = false): int
{
if ($this->isRunning()) {
$this->logger->error('Master process already running');
return 1;
throw new AlreadyRunningException();
}

if ($daemonize && $this->doDaemonize()) {
Expand Down Expand Up @@ -229,8 +230,7 @@ private function onMasterShutdown(): void
public function stop(int $code = 0): void
{
if (!$this->isRunning()) {
$this->logger->error(Server::NAME . ' is not running');
return;
throw new NotRunningException();
}

// If it called from outside working process
Expand Down Expand Up @@ -263,8 +263,7 @@ public function stop(int $code = 0): void
public function reload(): void
{
if (!$this->isRunning()) {
$this->logger->error(Server::NAME . ' is not running');
return;
throw new NotRunningException();
}

// If it called from outside working process
Expand Down
1 change: 0 additions & 1 deletion src/Plugin/System/Command/ConnectionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function execute(Options $options): int
\array_push($connections, ...$process->connections);
}


echo "❯ Connections\n";

if (\count($connections) > 0) {
Expand Down
8 changes: 7 additions & 1 deletion src/Plugin/System/Command/ReloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Luzrain\PHPStreamServer\Console\Command;
use Luzrain\PHPStreamServer\Console\Options;
use Luzrain\PHPStreamServer\Exception\NotRunningException;

final class ReloadCommand extends Command
{
Expand All @@ -14,7 +15,12 @@ final class ReloadCommand extends Command

public function execute(Options $options): int
{
$this->masterProcess->reload();
try {
$this->masterProcess->reload();
} catch (NotRunningException $e) {
echo \sprintf("<color;bg=red>%s</>\n", $e->getMessage());
return 1;
}

return 0;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Plugin/System/Command/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Luzrain\PHPStreamServer\Console\Command;
use Luzrain\PHPStreamServer\Console\Options;
use Luzrain\PHPStreamServer\Console\Table;
use Luzrain\PHPStreamServer\Exception\AlreadyRunningException;
use Luzrain\PHPStreamServer\Internal\ServerStatus\ServerStatus;
use Luzrain\PHPStreamServer\Internal\ServerStatus\WorkerProcessInfo;
use Luzrain\PHPStreamServer\Server;
Expand Down Expand Up @@ -63,6 +64,11 @@ public function execute(Options $options): int
echo "Press Ctrl+C to stop.\n";
}

return $this->masterProcess->run($isDaemon);
try {
return $this->masterProcess->run($isDaemon);
} catch (AlreadyRunningException $e) {
echo \sprintf("<color;bg=red>%s</>\n", $e->getMessage());
return 1;
}
}
}
9 changes: 7 additions & 2 deletions src/Plugin/System/Command/StopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Luzrain\PHPStreamServer\Console\Command;
use Luzrain\PHPStreamServer\Console\Options;
use Luzrain\PHPStreamServer\Exception\NotRunningException;

final class StopCommand extends Command
{
Expand All @@ -14,8 +15,12 @@ final class StopCommand extends Command

public function execute(Options $options): int
{
// TODO: throw excepton if server already runs
$this->masterProcess->stop();
try {
$this->masterProcess->stop();
} catch (NotRunningException $e) {
echo \sprintf("<color;bg=red>%s</>\n", $e->getMessage());
return 1;
}

return 0;
}
Expand Down

0 comments on commit d6986ca

Please sign in to comment.