-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Missing transporter name on messenger events #37736
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
Comments
Thank you for this issue. |
Friendly ping? Should this still be open? I will close if I don't hear anything. |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
lyrixx
added a commit
that referenced
this issue
Sep 22, 2021
…kwinza) This PR was merged into the 5.4 branch. Discussion ---------- [Messenger] Add `WorkerMetadata` to `Worker` class. | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fixes #37736 | License | MIT | Doc PR | - At the moment, there is no clean way to access the values of `transportNames` or recently introduced `queueNames` that the worker was configured with, although such data might be quite useful for logging/monitoring or other tasks. This PR attempts to fix that by adding a new and extensible way to provide additional information about a particular `Worker` object. So far, the following PRs could benefit from this change: - #43133 - #42723 **Use case example:** ---- - As I developer - When a message was consumed from transport with name `async`. - And the worker state is `idle`. - Then I want to reset services. **Before this PR**, the only solution not relying on using Reflection API would look like this: ```php private $servicesResetter; private $receiversName; private $actualReceiverName = null; public function __construct(ServicesResetter $servicesResetter, array $receiversName) { $this->servicesResetter = $servicesResetter; $this->receiversName = $receiversName; } public function saveReceiverName(AbstractWorkerMessageEvent $event): void { $this->actualReceiverName = $event->getReceiverName(); } public function resetServices(WorkerRunningEvent $event): void { if (!$event->isWorkerIdle() && \in_array($this->actualReceiverName, $this->receiversName, true)) { $this->servicesResetter->reset(); } $this->actualReceiverName = null; } public static function getSubscribedEvents(): array { return [ WorkerMessageHandledEvent::class => ['saveReceiverName'], WorkerMessageFailedEvent::class => ['saveReceiverName'], WorkerRunningEvent::class => ['resetServices'], ]; } ``` **With this PR**, one could simply use this to retrieve the transport name. ```php $event->getWorker()->getWorkerMetadata()->getTransportName() === $this->transportName; ``` So the whole solution would look like this: ```php private $servicesResetter; private $receiversName; public function __construct(ServicesResetter $servicesResetter, array $receiversName) { $this->servicesResetter = $servicesResetter; $this->receiversName = $receiversName; } public function resetServices(WorkerRunningEvent $event): void { $actualTransportName = $event->getWorker()->getWorkerMetadata()->getTransportName(); if (!$event->isWorkerIdle() || !in_array($actualTransportName, $this->receiversName, true)) { return; } $this->servicesResetter->reset(); } public static function getSubscribedEvents(): array { return [ WorkerRunningEvent::class => ['resetServices'], ]; } ``` Commits ------- 583f85b [Messenger] Add WorkerMetadata to Worker class
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
At the moment you don't receive any transporter name when a worker is started, running, message failed or stopped which would be handy when you want to monitor if a messenger is running and want to do some action if the messenger is closed for some reason.
Example
Please add the receiver names to the events for example for the following:
vendor/symfony/messenger/Worker.php#63
$this->dispatchEvent(new WorkerStartedEvent($this));
$this->dispatchEvent(new WorkerStartedEvent($this, $this->receivers));
vendor/symfony/messenger/Worker.php#78
$this->dispatchEvent(new WorkerRunningEvent($this, false));
$this->dispatchEvent(new WorkerRunningEvent($this, false, $transportName));
vendor/symfony/messenger/Worker.php#94
$this->dispatchEvent(new WorkerRunningEvent($this, true));
$this->dispatchEvent(new WorkerRunningEvent($this, true, $this->receivers));
vendor/symfony/messenger/Worker.php#100
$this->dispatchEvent(new WorkerStoppedEvent($this));
$this->dispatchEvent(new WorkerStoppedEvent($this, $this->receivers));
The text was updated successfully, but these errors were encountered: