-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor(history): split
HistoryListener
- Loading branch information
Showing
10 changed files
with
311 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/messenger-monitor-bundle package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Messenger\Monitor\EventListener; | ||
|
||
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; | ||
use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class AddMonitorStampListener | ||
{ | ||
public function __invoke(SendMessageToTransportsEvent $event): void | ||
{ | ||
$event->setEnvelope($event->getEnvelope()->with(new MonitorStamp())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,68 +9,31 @@ | |
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Messenger\Monitor\History; | ||
namespace Zenstruck\Messenger\Monitor\EventListener; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
use Symfony\Component\Scheduler\Messenger\ScheduledStamp; | ||
use Zenstruck\Messenger\Monitor\History\Model\Result; | ||
use Zenstruck\Messenger\Monitor\History\Model\Results; | ||
use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp; | ||
use Zenstruck\Messenger\Monitor\History\ResultNormalizer; | ||
use Zenstruck\Messenger\Monitor\History\Storage; | ||
use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; | ||
use Zenstruck\Messenger\Monitor\Stamp\TagStamp; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
* | ||
* @phpstan-import-type Structure from Result | ||
*/ | ||
final class HistoryListener | ||
final class HandleMonitorStampListener | ||
{ | ||
/** | ||
* @param class-string[] $excludedClasses | ||
*/ | ||
public function __construct( | ||
private Storage $storage, | ||
private ResultNormalizer $normalizer, | ||
private array $excludedClasses, | ||
) { | ||
} | ||
|
||
public function addMonitorStamp(SendMessageToTransportsEvent $event): void | ||
{ | ||
$event->setEnvelope($event->getEnvelope()->with(new MonitorStamp())); | ||
} | ||
|
||
public function receiveMessage(WorkerMessageReceivedEvent $event): void | ||
{ | ||
$envelope = $event->getEnvelope(); | ||
|
||
if ($this->isMonitoringDisabled($envelope)) { | ||
return; | ||
} | ||
|
||
$stamp = $envelope->last(MonitorStamp::class); | ||
|
||
if (\class_exists(ScheduledStamp::class) && $scheduledStamp = $envelope->last(ScheduledStamp::class)) { | ||
// scheduler transport doesn't trigger SendMessageToTransportsEvent | ||
$stamp = new MonitorStamp($scheduledStamp->messageContext->triggeredAt); | ||
|
||
$event->addStamps(TagStamp::forSchedule($scheduledStamp)); | ||
} | ||
|
||
if ($stamp instanceof MonitorStamp) { | ||
$event->addStamps($stamp->markReceived($event->getReceiverName())); | ||
} | ||
} | ||
|
||
public function handleSuccess(WorkerMessageHandledEvent $event): void | ||
{ | ||
if (!$stamp = $event->getEnvelope()->last(MonitorStamp::class)) { | ||
|
@@ -107,29 +70,6 @@ public function handleFailure(WorkerMessageFailedEvent $event): void | |
); | ||
} | ||
|
||
private function isMonitoringDisabled(Envelope $envelope): bool | ||
{ | ||
$messageClass = $envelope->getMessage()::class; | ||
|
||
foreach ($this->excludedClasses as $excludedClass) { | ||
if (\is_a($messageClass, $excludedClass, true)) { | ||
return true; | ||
} | ||
} | ||
|
||
$stamp = $envelope->last(DisableMonitoringStamp::class) ?? DisableMonitoringStamp::getFor($messageClass); | ||
|
||
if (!$stamp) { | ||
return false; | ||
} | ||
|
||
if ($stamp->onlyWhenNoHandler && !$this->hasNoHandlers($envelope)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function createResults(Envelope $envelope, ?HandlerFailedException $exception = null): Results | ||
{ | ||
$results = []; | ||
|
@@ -157,9 +97,4 @@ private function createResults(Envelope $envelope, ?HandlerFailedException $exce | |
|
||
return new Results($results); | ||
} | ||
|
||
private function hasNoHandlers(Envelope $envelope): bool | ||
{ | ||
return [] === $envelope->all(HandledStamp::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/messenger-monitor-bundle package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Messenger\Monitor\EventListener; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
use Symfony\Component\Scheduler\Messenger\ScheduledStamp; | ||
use Zenstruck\Messenger\Monitor\Stamp\DisableMonitoringStamp; | ||
use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; | ||
use Zenstruck\Messenger\Monitor\Stamp\TagStamp; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class ReceiveMonitorStampListener | ||
{ | ||
/** | ||
* @param class-string[] $excludedClasses | ||
*/ | ||
public function __construct(private array $excludedClasses) | ||
{ | ||
} | ||
|
||
public function __invoke(WorkerMessageReceivedEvent $event): void | ||
{ | ||
$envelope = $event->getEnvelope(); | ||
|
||
if ($this->isMonitoringDisabled($envelope)) { | ||
return; | ||
} | ||
|
||
$stamp = $envelope->last(MonitorStamp::class); | ||
|
||
if (\class_exists(ScheduledStamp::class) && $scheduledStamp = $envelope->last(ScheduledStamp::class)) { | ||
// scheduler transport doesn't trigger SendMessageToTransportsEvent | ||
$stamp = new MonitorStamp($scheduledStamp->messageContext->triggeredAt); | ||
|
||
$event->addStamps(TagStamp::forSchedule($scheduledStamp)); | ||
} | ||
|
||
if ($stamp instanceof MonitorStamp) { | ||
$event->addStamps($stamp->markReceived($event->getReceiverName())); | ||
} | ||
} | ||
|
||
private function isMonitoringDisabled(Envelope $envelope): bool | ||
{ | ||
$messageClass = $envelope->getMessage()::class; | ||
|
||
foreach ($this->excludedClasses as $excludedClass) { | ||
if (\is_a($messageClass, $excludedClass, true)) { | ||
return true; | ||
} | ||
} | ||
|
||
$stamp = $envelope->last(DisableMonitoringStamp::class) ?? DisableMonitoringStamp::getFor($messageClass); | ||
|
||
if (!$stamp) { | ||
return false; | ||
} | ||
|
||
if ($stamp->onlyWhenNoHandler && !$this->hasNoHandlers($envelope)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function hasNoHandlers(Envelope $envelope): bool | ||
{ | ||
return [] === $envelope->all(HandledStamp::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/messenger-monitor-bundle package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Messenger\Monitor\Tests\Unit\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent; | ||
use Zenstruck\Messenger\Monitor\EventListener\AddMonitorStampListener; | ||
use Zenstruck\Messenger\Monitor\Stamp\MonitorStamp; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class AddMonitorStampListenerTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function adds_monitor_stamp(): void | ||
{ | ||
$listener = new AddMonitorStampListener(); | ||
$envelope = new Envelope(new \stdClass()); | ||
$event = new SendMessageToTransportsEvent($envelope, []); | ||
|
||
$this->assertNull($event->getEnvelope()->last(MonitorStamp::class)); | ||
|
||
$listener->__invoke($event); | ||
|
||
$this->assertInstanceOf(MonitorStamp::class, $event->getEnvelope()->last(MonitorStamp::class)); | ||
} | ||
} |
Oops, something went wrong.