Skip to content

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

Closed
dvdknaap opened this issue Aug 4, 2020 · 3 comments · Fixed by #42335
Closed

Missing transporter name on messenger events #37736

dvdknaap opened this issue Aug 4, 2020 · 3 comments · Fixed by #42335

Comments

@dvdknaap
Copy link

dvdknaap commented Aug 4, 2020

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:

  1. Symfony\Component\Messenger\Event\WorkerMessageFailedEvent
  2. Symfony\Component\Messenger\Event\WorkerRunningEvent
  3. Symfony\Component\Messenger\Event\WorkerStartedEvent
  4. Symfony\Component\Messenger\Event\WorkerStoppedEvent

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));
@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link

Friendly ping? Should this still be open? I will close if I don't hear anything.

@carsonbot
Copy link

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants