Commit 99ebc69
committed
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
File tree
5 files changed
+150
-3
lines changed- src/Symfony/Component/Messenger
- Tests
5 files changed
+150
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
Lines changed: 57 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 169 | | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
170 | 192 | | |
171 | 193 | | |
172 | 194 | | |
| |||
359 | 381 | | |
360 | 382 | | |
361 | 383 | | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
55 | 59 | | |
56 | 60 | | |
57 | 61 | | |
| |||
63 | 67 | | |
64 | 68 | | |
65 | 69 | | |
66 | | - | |
67 | | - | |
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
71 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
72 | 78 | | |
73 | 79 | | |
74 | 80 | | |
| |||
173 | 179 | | |
174 | 180 | | |
175 | 181 | | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
176 | 187 | | |
177 | 188 | | |
178 | 189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
0 commit comments