From 763c7d3e663f8cb028ca6ae5360be8ce5e37f626 Mon Sep 17 00:00:00 2001 From: Hakan Aktas Date: Wed, 20 Mar 2024 09:55:35 +0200 Subject: [PATCH 1/5] Stop ack-ing unprocessed pubsub messages and log warnings --- src/Commands/AppEventsListener.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Commands/AppEventsListener.php b/src/Commands/AppEventsListener.php index 2c07748..da5f1dd 100644 --- a/src/Commands/AppEventsListener.php +++ b/src/Commands/AppEventsListener.php @@ -102,9 +102,11 @@ private function runIteration(Subscription $subscription) $job = AppEventFactory::fromMessage($message); } catch (UnserializableProtoException $e) { if (! $this->option('silent')) { - $this->info('No implementation registered for message type: ' . $e->protoMessageType); + Log::warning('Proto is not registered', [ + 'proto' => $e->protoMessageType, + ]); + $this->warning('No implementation registered for message type: ' . $e->protoMessageType); } - $handledMessages[] = $message; continue; } catch (UnsupportedEventException $e) { Log::debug('Unsupported pubsub event', [ From 97f9c2aa942cb0cfdaa99bf9fb1daea8900e0f4c Mon Sep 17 00:00:00 2001 From: Hakan Aktas Date: Fri, 3 May 2024 17:46:47 +0300 Subject: [PATCH 2/5] Add Hakan Aktas to authors --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index af56172..1db0804 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,10 @@ { "name": "Leo Sjöberg", "email": "leo@decahedron.io" + }, + { + "name": "Hakan Aktas", + "email": "hakan.aktas@jobilla.com" } ], "require": { From f0e05530fc618802b0a3b32454c999e7f7a23484 Mon Sep 17 00:00:00 2001 From: Hakan Aktas Date: Fri, 3 May 2024 17:47:38 +0300 Subject: [PATCH 3/5] Drop support for PHP <8.2 and Laravel <9 --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 1db0804..65aa4d6 100644 --- a/composer.json +++ b/composer.json @@ -13,13 +13,13 @@ } ], "require": { - "php": ">= 7.3", + "php": ">=8.2", "google/cloud": "0.*", - "illuminate/config": "^6.0|^7.0|^8.0|^9.0|^10.0", - "illuminate/console": "^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/config": "^9.0|^10.0", + "illuminate/console": "^9.0|^10.0", "kainxspirits/laravel-pubsub-queue": "~0.4.0|~0.5.0|~0.6.0|~0.7.0|~0.8.0", "google/protobuf": "^3.6.0", - "illuminate/bus": "^6.0|^7.0|^8.0|^9.0|^10.0", + "illuminate/bus": "^9.0|^10.0", "ext-json": "*" }, "autoload": { From 1fe75e8b8f6bc7f6577bbb38e8796f07974470cf Mon Sep 17 00:00:00 2001 From: Hakan Aktas Date: Fri, 3 May 2024 17:49:37 +0300 Subject: [PATCH 4/5] Change namespace to Jobilla --- composer.json | 6 +++--- src/AppEvent.php | 2 +- src/AppEventFactory.php | 2 +- src/AppEventsProvider.php | 4 ++-- src/Commands/AppEventsListener.php | 10 +++++----- src/PubSubConnector.php | 2 +- src/PubSubQueue.php | 2 +- src/SubscriptionTopicMismatchException.php | 2 +- src/UnserializableProtoException.php | 2 +- src/UnsupportedEventException.php | 2 +- tests/AppEventsListenerTest.php | 8 ++++---- tests/PubSubQueueTest.php | 8 ++++---- .../Decahedron/AppEvents/Tests/Proto/Metadata/Test.php | 2 +- .../fixtures/Decahedron/AppEvents/Tests/Proto/Test.php | 2 +- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/composer.json b/composer.json index 65aa4d6..e3bd648 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,12 @@ }, "autoload": { "psr-4": { - "Decahedron\\AppEvents\\": "src/" + "Jobilla\\AppEvents\\": "src/" } }, "autoload-dev": { "psr-4": { - "Decahedron\\AppEvents\\Tests\\Proto\\": "tests/fixtures/Decahedron/AppEvents/Tests/Proto" + "Jobilla\\AppEvents\\Tests\\Proto\\": "tests/fixtures/Jobilla/AppEvents/Tests/Proto" } }, "extra": { @@ -38,7 +38,7 @@ "Kainxspirits\\PubSubQueue\\PubSubQueueServiceProvider" ], "providers": [ - "Decahedron\\AppEvents\\AppEventsProvider" + "Jobilla\\AppEvents\\AppEventsProvider" ] } }, diff --git a/src/AppEvent.php b/src/AppEvent.php index 3b3317c..58618fe 100644 --- a/src/AppEvent.php +++ b/src/AppEvent.php @@ -1,6 +1,6 @@ Date: Fri, 3 May 2024 17:49:56 +0300 Subject: [PATCH 5/5] Update listener finding & running logic --- src/AppEvent.php | 11 ----------- src/AppEventFactory.php | 20 ++++++++++++++++++-- src/Commands/AppEventsListener.php | 9 ++------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/AppEvent.php b/src/AppEvent.php index 58618fe..1477247 100644 --- a/src/AppEvent.php +++ b/src/AppEvent.php @@ -34,15 +34,4 @@ public function __construct(string $event, Message $payload) $this->payload = $payload; $this->event = $event; } - - public function handle() - { - foreach (Config::get('app-events.handlers') as $event => $handler) { - if ($this->event !== $event) { - continue; - } - - Container::getInstance()->make($handler)->handle($this->payload, $event); - } - } } diff --git a/src/AppEventFactory.php b/src/AppEventFactory.php index 1cb4adc..30aa990 100644 --- a/src/AppEventFactory.php +++ b/src/AppEventFactory.php @@ -12,9 +12,25 @@ class AppEventFactory * @throws UnserializableProtoException * @throws UnsupportedEventException */ - public static function fromMessage(Message $message): AppEvent + public static function fromMessage(Message $message): mixed { - return new AppEvent($message->attribute('event_type'), static::resolveProtobufInstance($message)); + $handler = static::resolveHandler($message->attribute('event_type')); + + return new $handler(static::resolveProtobufInstance($message)); + } + + /** + * @throws UnsupportedEventException + */ + protected static function resolveHandler(string $eventName): string + { + $configKey = 'app-events.handlers.' . $eventName; + + if (! Config::has($configKey)) { + throw new UnsupportedEventException($eventName); + } + + return Config::get($configKey); } /** diff --git a/src/Commands/AppEventsListener.php b/src/Commands/AppEventsListener.php index 2a8aa2c..1a6c8fd 100644 --- a/src/Commands/AppEventsListener.php +++ b/src/Commands/AppEventsListener.php @@ -101,12 +101,7 @@ private function runIteration(Subscription $subscription) try { $job = AppEventFactory::fromMessage($message); } catch (UnserializableProtoException $e) { - if (! $this->option('silent')) { - Log::warning('Proto is not registered', [ - 'proto' => $e->protoMessageType, - ]); - $this->warning('No implementation registered for message type: ' . $e->protoMessageType); - } + $handledMessages[] = $message; continue; } catch (UnsupportedEventException $e) { Log::debug('Unsupported pubsub event', [ @@ -114,9 +109,9 @@ private function runIteration(Subscription $subscription) 'message' => $message->info(), 'published_at' => $message->publishTime(), ]); - $handledMessages[] = $message; continue; } + if (! $this->option('silent')) { $this->info('Handling message: '.$job->event); }