-
-
Notifications
You must be signed in to change notification settings - Fork 17
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 <kevinbond@gmail.com> | ||
* | ||
* 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 <kevinbond@gmail.com> | ||
* | ||
* @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
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 <kevinbond@gmail.com> | ||
* | ||
* 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 <kevinbond@gmail.com> | ||
* | ||
* @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.