Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop ack-ing unprocessed pubsub messages and log warnings #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
{
"name": "Leo Sjöberg",
"email": "[email protected]"
},
{
"name": "Hakan Aktas",
"email": "[email protected]"
}
],
"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": {
"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": {
Expand All @@ -34,7 +38,7 @@
"Kainxspirits\\PubSubQueue\\PubSubQueueServiceProvider"
],
"providers": [
"Decahedron\\AppEvents\\AppEventsProvider"
"Jobilla\\AppEvents\\AppEventsProvider"
]
}
},
Expand Down
13 changes: 1 addition & 12 deletions src/AppEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Illuminate\Bus\Queueable;
use Illuminate\Container\Container;
Expand Down Expand Up @@ -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);
}
}
}
22 changes: 19 additions & 3 deletions src/AppEventFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Google\Cloud\PubSub\Message;
use Google\Protobuf\Internal\Message as ProtobufMessage;
Expand All @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/AppEventsProvider.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Decahedron\AppEvents\Commands\AppEventsListener;
use Jobilla\AppEvents\Commands\AppEventsListener;
use Google\Cloud\PubSub\PubSubClient;
use Illuminate\Support\ServiceProvider;

Expand Down
15 changes: 6 additions & 9 deletions src/Commands/AppEventsListener.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php

namespace Decahedron\AppEvents\Commands;
namespace Jobilla\AppEvents\Commands;

use Decahedron\AppEvents\UnsupportedEventException;
use Jobilla\AppEvents\UnsupportedEventException;
use Exception;
use Google\Cloud\Core\Exception\BadRequestException;
use Google\Cloud\PubSub\Subscription;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Google\Cloud\PubSub\PubSubClient;
use Decahedron\AppEvents\AppEventFactory;
use Jobilla\AppEvents\AppEventFactory;
use Illuminate\Contracts\Config\Repository;
use Decahedron\AppEvents\UnserializableProtoException;
use Decahedron\AppEvents\SubscriptionTopicMismatchException;
use Jobilla\AppEvents\UnserializableProtoException;
use Jobilla\AppEvents\SubscriptionTopicMismatchException;

class AppEventsListener extends Command
{
Expand Down Expand Up @@ -101,9 +101,6 @@ private function runIteration(Subscription $subscription)
try {
$job = AppEventFactory::fromMessage($message);
} catch (UnserializableProtoException $e) {
if (! $this->option('silent')) {
$this->info('No implementation registered for message type: ' . $e->protoMessageType);
}
$handledMessages[] = $message;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's plenty of service that don't and won't get a handler for the message, meaning message will never get acked and will be retried many times. Don't think that's the aim here.

Copy link
Member Author

@hkan hkan Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right. The goal of this PR is to stop ack-ing the pubsub messages for the events that are registered in handlers array of app-events.php but does not have the relevant proto class in mappings array of the same file. But I just noticed that I need to change the AppEventFactory for that, too. Will add changes in a minute. I'll see what I can change to reach that goal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a bit more change to make that happen and I fall into the trap of updating everything unrelated but necessary.

continue;
} catch (UnsupportedEventException $e) {
Expand All @@ -112,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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PubSubConnector.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Google\Cloud\PubSub\PubSubClient;
use Kainxspirits\PubSubQueue\Connectors\PubSubConnector as BaseConnector;
Expand Down
2 changes: 1 addition & 1 deletion src/PubSubQueue.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Google\Cloud\PubSub\Message;
use Kainxspirits\PubSubQueue\Jobs\PubSubJob;
Expand Down
2 changes: 1 addition & 1 deletion src/SubscriptionTopicMismatchException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Exception;
use Google\Cloud\PubSub\Subscription;
Expand Down
2 changes: 1 addition & 1 deletion src/UnserializableProtoException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/UnsupportedEventException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Decahedron\AppEvents;
namespace Jobilla\AppEvents;

use Exception;

Expand Down
8 changes: 4 additions & 4 deletions tests/AppEventsListenerTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Decahedron\AppEvents\Tests;
namespace Jobilla\AppEvents\Tests;

use Decahedron\AppEvents\Commands\AppEventsListener;
use Decahedron\AppEvents\Tests\Proto\Test;
use Jobilla\AppEvents\Commands\AppEventsListener;
use Jobilla\AppEvents\Tests\Proto\Test;
use Google\Cloud\PubSub\Message;
use Google\Cloud\PubSub\PubSubClient;
use Google\Cloud\PubSub\Subscription;
Expand Down Expand Up @@ -108,4 +108,4 @@ public function handle()
{
throw new \Exception('failure');
}
}
}
8 changes: 4 additions & 4 deletions tests/PubSubQueueTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Decahedron\AppEvents\Tests;
namespace Jobilla\AppEvents\Tests;

use Decahedron\AppEvents\AppEvent;
use Decahedron\AppEvents\PubSubQueue;
use Decahedron\AppEvents\Tests\Proto\Test;
use Jobilla\AppEvents\AppEvent;
use Jobilla\AppEvents\PubSubQueue;
use Jobilla\AppEvents\Tests\Proto\Test;
use Google\Cloud\PubSub\Message;
use Google\Cloud\PubSub\PubSubClient;
use Google\Cloud\PubSub\Subscription;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/fixtures/Decahedron/AppEvents/Tests/Proto/Test.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.