Skip to content

[Messenger] Add WorkerMetadata to Worker class. #42335

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

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* Add `StopWorkerExceptionInterface` and its implementation `StopWorkerException` to stop the worker.
* Add support for resetting container services after each messenger message.
* Added `WorkerMetadata` class which allows you to access the configuration details of a worker, like `queueNames` and `transportNames` it consumes from.
* New method `getMetadata()` was added to `Worker` class which returns the `WorkerMetadata` object.

5.3
---
Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/Messenger/Tests/WorkerMetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\WorkerMetadata;

/**
* @author Oleg Krasavin <okwinza@gmail.com>
*/
class WorkerMetadataTest extends TestCase
{
public function testItReturnsDefaultValuesIfNoneProvided()
{
$metadata = new WorkerMetadata([]);

$this->assertNull($metadata->getQueueNames());
$this->assertSame([], $metadata->getTransportNames());
}

public function testItReturnsProvidedMetadata()
{
$data = [
'queueNames' => ['c', 'b', 'a'],
'transportNames' => ['a', 'b', 'c'],
];

$metadata = new WorkerMetadata($data);

$this->assertSame($data['queueNames'], $metadata->getQueueNames());
$this->assertSame($data['transportNames'], $metadata->getTransportNames());
}

public function testItSetsMetadataViaSetter()
{
$data = [
'queueNames' => ['c', 'b', 'a'],
'transportNames' => ['a', 'b', 'c'],
];

$metadata = new WorkerMetadata([]);

$metadata->set($data);

$this->assertSame($data['queueNames'], $metadata->getQueueNames());
$this->assertSame($data['transportNames'], $metadata->getTransportNames());
}
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ public function testWorkerDispatchesEventsOnError()
$worker->run();
}

public function testWorkerContainsMetadata()
{
$envelope = new Envelope(new DummyMessage('Hello'));
$receiver = new DummyQueueReceiver([[$envelope]]);

$bus = $this->createMock(MessageBusInterface::class);
$bus->method('dispatch')->willReturn($envelope);

$dispatcher = new EventDispatcher();
$dispatcher->addListener(WorkerRunningEvent::class, function (WorkerRunningEvent $event) {
$event->getWorker()->stop();
});

$worker = new Worker(['dummyReceiver' => $receiver], $bus, $dispatcher);
$worker->run(['queues' => ['queue1', 'queue2']]);

$workerMetadata = $worker->getMetadata();

$this->assertSame(['queue1', 'queue2'], $workerMetadata->getQueueNames());
$this->assertSame(['dummyReceiver'], $workerMetadata->getTransportNames());
}

public function testTimeoutIsConfigurable()
{
$apiMessage = new DummyMessage('API');
Expand Down Expand Up @@ -359,3 +381,11 @@ public function getAcknowledgedEnvelopes(): array
return $this->acknowledgedEnvelopes;
}
}

class DummyQueueReceiver extends DummyReceiver implements QueueReceiverInterface
{
public function getFromQueues(array $queueNames): iterable
{
return $this->get();
}
}
17 changes: 14 additions & 3 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Worker
private $eventDispatcher;
private $logger;
private $shouldStop = false;
private $metadata;

/**
* @param ReceiverInterface[] $receivers Where the key is the transport name
Expand All @@ -52,6 +53,9 @@ public function __construct(array $receivers, MessageBusInterface $bus, EventDis
$this->bus = $bus;
$this->logger = $logger;
$this->eventDispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($eventDispatcher) : $eventDispatcher;
$this->metadata = new WorkerMetadata([
'transportNames' => array_keys($receivers),
]);
}

/**
Expand All @@ -63,12 +67,14 @@ public function __construct(array $receivers, MessageBusInterface $bus, EventDis
*/
public function run(array $options = []): void
{
$this->dispatchEvent(new WorkerStartedEvent($this));

$options = array_merge([
'sleep' => 1000000,
], $options);
$queueNames = $options['queues'] ?? false;
$queueNames = $options['queues'] ?? null;

$this->metadata->set(['queueNames' => $queueNames]);

$this->dispatchEvent(new WorkerStartedEvent($this));

if ($queueNames) {
// if queue names are specified, all receivers must implement the QueueReceiverInterface
Expand Down Expand Up @@ -173,6 +179,11 @@ public function stop(): void
$this->shouldStop = true;
}

public function getMetadata(): WorkerMetadata
{
return $this->metadata;
}

private function dispatchEvent(object $event): void
{
if (null === $this->eventDispatcher) {
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Messenger/WorkerMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger;

/**
* @author Oleg Krasavin <okwinza@gmail.com>
*/
final class WorkerMetadata
{
private $metadata;

public function __construct(array $metadata)
{
$this->metadata = $metadata;
}

public function set(array $newMetadata): void
{
$this->metadata = array_merge($this->metadata, $newMetadata);
}

/**
* Returns the queue names the worker consumes from, if "--queues" option was used.
* Returns null otherwise.
*/
public function getQueueNames(): ?array
{
return $this->metadata['queueNames'] ?? null;
}

/**
* Returns an array of unique identifiers for transport receivers the worker consumes from.
*/
public function getTransportNames(): array
{
return $this->metadata['transportNames'] ?? [];
}
}