Refactoring, logging improvements

This commit is contained in:
Chris 2024-03-12 02:21:42 +01:00
parent 69fd46b73a
commit e10fd6c471
10 changed files with 38 additions and 16 deletions

View File

@ -9,14 +9,14 @@ file_exists(__DIR__."/../../../autoload.php") && require_once __DIR__."/../../..
if (file_exists(__DIR__."/../src/meta")) { if (file_exists(__DIR__."/../src/meta")) {
$meta = require_once(__DIR__."/../src/meta"); $meta = require_once(__DIR__."/../src/meta");
define("MERCUREACT_VERSION", $meta['version']??'0.0.0'); define("MERCUREACT_VERSION", $meta['version']?:'0.0.0');
define("MERCUREACT_BUILDTIME", $meta['buildtime']); define("MERCUREACT_BUILDTIME", $meta['buildtime']);
} else { } else {
define("MERCUREACT_VERSION", "DEV"); define("MERCUREACT_VERSION", "DEV");
define("MERCUREACT_BUILDTIME", null); define("MERCUREACT_BUILDTIME", null);
} }
$opts = getopt("c:C:h"); $opts = getopt("c:C:hvV");
if (isset($opts['h'])) { if (isset($opts['h'])) {
$info = "v".MERCUREACT_VERSION.(MERCUREACT_BUILDTIME?("\nBuilt on ".MERCUREACT_BUILDTIME):""); $info = "v".MERCUREACT_VERSION.(MERCUREACT_BUILDTIME?("\nBuilt on ".MERCUREACT_BUILDTIME):"");
@ -26,6 +26,8 @@ if (isset($opts['h'])) {
Options: Options:
-V Print version and exit
-v Verbose debug logging
-c config Read configuration from file -c config Read configuration from file
-C config Write a new configuration to file and open with editor -C config Write a new configuration to file and open with editor
@ -34,6 +36,11 @@ if (isset($opts['h'])) {
exit(0); exit(0);
} }
if (isset($opts['V'])) {
fprintf(STDOUT, "Mercureact %s\nBuilt on %s\n", MERCUREACT_VERSION, MERCUREACT_BUILDTIME);
exit(0);
}
if (isset($opts['C'])) { if (isset($opts['C'])) {
$file = $opts['C']; $file = $opts['C'];
if (file_exists($file)) { if (file_exists($file)) {
@ -78,5 +85,7 @@ if (isset($opts['c'])) {
->setJwtSecret("!ChangeThisMercureHubJWTSecretKey!"); ->setJwtSecret("!ChangeThisMercureHubJWTSecretKey!");
} }
$daemon = new Daemon($config); $verbose = isset($opts['v']);
$daemon = new Daemon($config, $verbose);
$daemon->start(); $daemon->start();

View File

@ -9,4 +9,6 @@ interface SubscriberInterface
public function isAuthorized(): bool; public function isAuthorized(): bool;
public function getPayload(): ?array; public function getPayload(): ?array;
public function getId(): string;
} }

View File

@ -2,6 +2,7 @@
namespace NoccyLabs\Mercureact\Broker; namespace NoccyLabs\Mercureact\Broker;
use Psr\Log\LoggerInterface;
use SplObjectStorage; use SplObjectStorage;
class TopicManager class TopicManager
@ -13,7 +14,7 @@ class TopicManager
private SplObjectStorage $subscribers; private SplObjectStorage $subscribers;
public function __construct() public function __construct(private LoggerInterface $logger)
{ {
$this->subscribers = new SplObjectStorage(); $this->subscribers = new SplObjectStorage();
} }
@ -21,6 +22,7 @@ class TopicManager
public function getTopic(string $topic): Topic public function getTopic(string $topic): Topic
{ {
if (!isset($this->topics[$topic])) { if (!isset($this->topics[$topic])) {
$this->logger->debug("Created topic: {$topic}");
$this->topics[$topic] = new Topic($topic); $this->topics[$topic] = new Topic($topic);
} }
return $this->topics[$topic]; return $this->topics[$topic];
@ -30,12 +32,14 @@ class TopicManager
{ {
$this->lastEventId = $message->id; $this->lastEventId = $message->id;
foreach ($message->topic as $topic) { foreach ($message->topic as $topic) {
$this->logger->debug("Publish: {$message->id}".json_encode($message->topic,JSON_UNESCAPED_SLASHES));
$this->getTopic($topic)->publish($message); $this->getTopic($topic)->publish($message);
} }
} }
public function subscribe(SubscriberInterface $subscriber, array $topics): void public function subscribe(SubscriberInterface $subscriber, array $topics): void
{ {
$this->logger->debug("Subscribed: ".$subscriber->getId()." + ".json_encode($topics,JSON_UNESCAPED_SLASHES));
$this->subscribers->attach($subscriber); $this->subscribers->attach($subscriber);
foreach ($topics as $topic) { foreach ($topics as $topic) {
$this->getTopic($topic)->addSubscriber($subscriber); $this->getTopic($topic)->addSubscriber($subscriber);
@ -44,8 +48,9 @@ class TopicManager
public function unsubscribe(SubscriberInterface $subscriber, ?array $topics=null): void public function unsubscribe(SubscriberInterface $subscriber, ?array $topics=null): void
{ {
$this->subscribers->detach($subscriber); $this->logger->debug("Unsubscribed: ".$subscriber->getId()." - ".json_encode($topics,JSON_UNESCAPED_SLASHES));
if (!$topics) { if (!$topics) {
$this->subscribers->detach($subscriber);
foreach ($this->topics as $topic) { foreach ($this->topics as $topic) {
$topic->removeSubscriber($subscriber); $topic->removeSubscriber($subscriber);
} }

View File

@ -3,6 +3,7 @@
namespace NoccyLabs\Mercureact; namespace NoccyLabs\Mercureact;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger; use Monolog\Logger;
use NoccyLabs\Mercureact\Http\Server; use NoccyLabs\Mercureact\Http\Server;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -20,17 +21,17 @@ class Daemon
private LoggerInterface $logger; private LoggerInterface $logger;
public function __construct(Configuration $config, ?LoopInterface $loop=null) public function __construct(Configuration $config, bool $verbose=false, ?LoopInterface $loop=null)
{ {
$this->config = $config; $this->config = $config;
$this->loop = $loop??Loop::get(); $this->loop = $loop??Loop::get();
$this->logger = $this->createLogger(); $this->logger = $this->createLogger($verbose);
} }
private function createLogger(): Logger private function createLogger(bool $verbose): Logger
{ {
$handlers = [ $handlers = [
new StreamHandler(STDOUT) new StreamHandler(STDOUT, $verbose?Level::Debug:Level::Info)
]; ];
$logger = new Logger("main", $handlers); $logger = new Logger("main", $handlers);
return $logger; return $logger;

View File

@ -1,6 +1,6 @@
<?php <?php
namespace NoccyLabs\Mercureact\Http\Exception; namespace NoccyLabs\Mercureact\Exception;
use Exception; use Exception;

View File

@ -1,6 +1,6 @@
<?php <?php
namespace NoccyLabs\Mercureact\Http\Exception; namespace NoccyLabs\Mercureact\Exception;
use Exception; use Exception;

View File

@ -6,8 +6,8 @@ use NoccyLabs\Mercureact\Broker\Message;
use NoccyLabs\Mercureact\Broker\SseSubscriber; use NoccyLabs\Mercureact\Broker\SseSubscriber;
use NoccyLabs\Mercureact\Broker\TopicManager; use NoccyLabs\Mercureact\Broker\TopicManager;
use NoccyLabs\Mercureact\Configuration; use NoccyLabs\Mercureact\Configuration;
use NoccyLabs\Mercureact\Http\Exception\RequestException; use NoccyLabs\Mercureact\Exception\RequestException;
use NoccyLabs\Mercureact\Http\Exception\SecurityException; use NoccyLabs\Mercureact\Exception\SecurityException;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop; use React\EventLoop\Loop;

View File

@ -3,7 +3,7 @@
namespace NoccyLabs\Mercureact\Http\Middleware; namespace NoccyLabs\Mercureact\Http\Middleware;
use NoccyLabs\Mercureact\Configuration; use NoccyLabs\Mercureact\Configuration;
use NoccyLabs\Mercureact\Http\Exception\SecurityException; use NoccyLabs\Mercureact\Exception\SecurityException;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;

View File

@ -3,7 +3,7 @@
namespace NoccyLabs\Mercureact\Http\Middleware; namespace NoccyLabs\Mercureact\Http\Middleware;
use NoccyLabs\Mercureact\Configuration; use NoccyLabs\Mercureact\Configuration;
use NoccyLabs\Mercureact\Http\Exception\SecurityException; use NoccyLabs\Mercureact\Exception\SecurityException;
use NoccyLabs\SimpleJWT\JWTToken; use NoccyLabs\SimpleJWT\JWTToken;
use NoccyLabs\SimpleJWT\Key\JWTPlaintextKey; use NoccyLabs\SimpleJWT\Key\JWTPlaintextKey;
use NoccyLabs\SimpleJWT\Validator\JWTValidator; use NoccyLabs\SimpleJWT\Validator\JWTValidator;

View File

@ -54,8 +54,13 @@ class Server
$this->config = $config; $this->config = $config;
$this->logger = $logger ?? new NullLogger(); $this->logger = $logger ?? new NullLogger();
if ($logger instanceof Logger) {
$topicLogger = $logger->withName("broker");
} else {
$topicLogger = $this->logger;
}
$this->topicManager = new TopicManager(); $this->topicManager = new TopicManager($topicLogger);
$this->loop->addPeriodicTimer(30, function () { $this->loop->addPeriodicTimer(30, function () {
$this->topicManager->garbageCollect(); $this->topicManager->garbageCollect();
}); });