-
-
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.
Merge pull request #120 from kbond/features
More Features
- Loading branch information
Showing
29 changed files
with
946 additions
and
229 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
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,60 +9,29 @@ | |
* 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 | ||
{ | ||
public function __construct(private Storage $storage, private ResultNormalizer $normalizer) | ||
{ | ||
} | ||
|
||
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 __construct( | ||
private Storage $storage, | ||
private ResultNormalizer $normalizer, | ||
) { | ||
} | ||
|
||
public function handleSuccess(WorkerMessageHandledEvent $event): void | ||
|
@@ -101,35 +70,6 @@ public function handleFailure(WorkerMessageFailedEvent $event): void | |
); | ||
} | ||
|
||
private function isMonitoringDisabled(Envelope $envelope): bool | ||
{ | ||
if ($stamp = $envelope->last(DisableMonitoringStamp::class)) { | ||
if (false === $stamp->onlyWhenNoHandler) { | ||
return true; | ||
} | ||
|
||
return $this->hasNoHandlers($envelope); | ||
} | ||
|
||
$reflection = new \ReflectionClass($envelope->getMessage()); | ||
$attributes = []; | ||
|
||
while (false !== $reflection && [] === $attributes) { | ||
$attributes = $reflection->getAttributes(DisableMonitoringStamp::class); | ||
$reflection = $reflection->getParentClass(); | ||
} | ||
|
||
if ([] !== $attributes) { | ||
if (false === $attributes[0]->newInstance()->onlyWhenNoHandler) { | ||
return true; | ||
} | ||
|
||
return $this->hasNoHandlers($envelope); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
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,83 @@ | ||
<?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; | ||
} | ||
} | ||
|
||
if (!$stamp = DisableMonitoringStamp::firstFrom($envelope)) { | ||
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
Oops, something went wrong.