Skip to content

Commit 152e9ca

Browse files
committed
multi client configuration
1 parent 7c486c5 commit 152e9ca

File tree

4 files changed

+96
-59
lines changed

4 files changed

+96
-59
lines changed

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Enqueue\AsyncEventDispatcher\DependencyInjection\AsyncEventDispatcherExtension;
77
use Enqueue\Bundle\Consumption\Extension\DoctrineClearIdentityMapExtension;
88
use Enqueue\Bundle\Consumption\Extension\DoctrinePingConnectionExtension;
9+
use Enqueue\Bundle\Profiler\MessageQueueCollector;
910
use Enqueue\Client\CommandSubscriberInterface;
1011
use Enqueue\Client\TopicSubscriberInterface;
1112
use Enqueue\Consumption\Extension\ReplyExtension;
@@ -14,6 +15,7 @@
1415
use Enqueue\Monitoring\Symfony\DependencyInjection\MonitoringFactory;
1516
use Enqueue\Symfony\Client\DependencyInjection\ClientFactory;
1617
use Enqueue\Symfony\DependencyInjection\TransportFactory;
18+
use Enqueue\Symfony\DiUtils;
1719
use Symfony\Component\Config\FileLocator;
1820
use Symfony\Component\Config\Resource\FileResource;
1921
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -87,6 +89,7 @@ public function load(array $configs, ContainerBuilder $container): void
8789
$container->setParameter('enqueue.transports', $transportNames);
8890
$container->setParameter('enqueue.clients', $clientNames);
8991

92+
$this->loadMessageQueueCollector($config, $container);
9093
$this->setupAutowiringForProcessors($config, $container);
9194
$this->loadAsyncCommands($config, $container);
9295

@@ -96,8 +99,6 @@ public function load(array $configs, ContainerBuilder $container): void
9699
$this->loadSignalExtension($config, $container);
97100
$this->loadReplyExtension($config, $container);
98101

99-
// @todo register MessageQueueCollector
100-
101102
// if ($config['job']) {
102103
// if (!class_exists(Job::class)) {
103104
// throw new \LogicException('Seems "enqueue/job-queue" is not installed. Please fix this issue.');
@@ -301,4 +302,28 @@ private function loadAsyncCommands(array $config, ContainerBuilder $container):
301302
$extension = new AsyncCommandExtension();
302303
$extension->load(['clients' => $configNames], $container);
303304
}
305+
306+
private function loadMessageQueueCollector(array $config, ContainerBuilder $container)
307+
{
308+
$configNames = [];
309+
foreach ($config as $name => $modules) {
310+
if (isset($modules['client'])) {
311+
$configNames[] = $name;
312+
}
313+
}
314+
315+
if (false == $configNames) {
316+
return;
317+
}
318+
319+
$service = $container->register('enqueue.profiler.message_queue_collector', MessageQueueCollector::class);
320+
$service->addTag('data_collector', [
321+
'template' => '@Enqueue/Profiler/panel.html.twig',
322+
'id' => 'enqueue.message_queue'
323+
]);
324+
325+
foreach ($configNames as $configName) {
326+
$service->addMethodCall('addProducer', [$configName, DiUtils::create('client', $configName)->reference('producer')]);
327+
}
328+
}
304329
}

pkg/enqueue-bundle/Profiler/MessageQueueCollector.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,24 @@ class MessageQueueCollector extends DataCollector
1515
/**
1616
* @var ProducerInterface
1717
*/
18-
private $producer;
18+
private $producers;
1919

20-
/**
21-
* @param ProducerInterface $producer
22-
*/
23-
public function __construct(ProducerInterface $producer)
20+
public function addProducer(string $name, ProducerInterface $producer): void
2421
{
25-
$this->producer = $producer;
22+
$this->producers[$name] = $producer;
2623
}
2724

2825
/**
2926
* {@inheritdoc}
3027
*/
3128
public function collect(Request $request, Response $response, \Exception $exception = null)
3229
{
33-
$this->data = [
34-
'sent_messages' => [],
35-
];
30+
$this->data = [];
3631

37-
if ($this->producer instanceof TraceableProducer) {
38-
$this->data['sent_messages'] = $this->producer->getTraces();
32+
foreach ($this->producers as $name => $producer) {
33+
if ($producer instanceof TraceableProducer) {
34+
$this->data[$name] = $producer->getTraces();
35+
}
3936
}
4037
}
4138

@@ -44,7 +41,7 @@ public function collect(Request $request, Response $response, \Exception $except
4441
*/
4542
public function getSentMessages()
4643
{
47-
return $this->data['sent_messages'];
44+
return $this->data;
4845
}
4946

5047
/**

pkg/enqueue-bundle/Resources/config/client.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

pkg/enqueue-bundle/Tests/Unit/Profiler/MessageQueueCollectorTest.php

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,64 +21,87 @@ public function testShouldExtendDataCollectorClass()
2121
$this->assertClassExtends(DataCollector::class, MessageQueueCollector::class);
2222
}
2323

24-
public function testCouldBeConstructedWithMessageProducerAsFirstArgument()
24+
public function testCouldBeConstructedWithEmptyConstructor()
2525
{
26-
new MessageQueueCollector($this->createProducerMock());
26+
new MessageQueueCollector();
2727
}
2828

2929
public function testShouldReturnExpectedName()
3030
{
31-
$collector = new MessageQueueCollector($this->createProducerMock());
31+
$collector = new MessageQueueCollector();
3232

3333
$this->assertEquals('enqueue.message_queue', $collector->getName());
3434
}
3535

3636
public function testShouldReturnEmptySentMessageArrayIfNotTraceableProducer()
3737
{
38-
$collector = new MessageQueueCollector($this->createProducerMock());
38+
$collector = new MessageQueueCollector();
39+
$collector->addProducer('default', $this->createProducerMock());
3940

4041
$collector->collect(new Request(), new Response());
4142

4243
$this->assertSame([], $collector->getSentMessages());
4344
}
4445

45-
public function testShouldReturnSentMessageArrayTakenFromTraceableProducer()
46+
public function testShouldReturnSentMessageArrayTakenFromTraceableProducers()
4647
{
47-
$producer = new TraceableProducer($this->createProducerMock());
48-
$producer->sendEvent('fooTopic', 'fooMessage');
49-
$producer->sendCommand('barCommand', 'barMessage');
48+
$producer1 = new TraceableProducer($this->createProducerMock());
49+
$producer1->sendEvent('fooTopic1', 'fooMessage');
50+
$producer1->sendCommand('barCommand1', 'barMessage');
5051

51-
$collector = new MessageQueueCollector($producer);
52+
$producer2 = new TraceableProducer($this->createProducerMock());
53+
$producer2->sendEvent('fooTopic2', 'fooMessage');
54+
55+
$collector = new MessageQueueCollector();
56+
$collector->addProducer('foo', $producer1);
57+
$collector->addProducer('bar', $producer2);
5258

5359
$collector->collect(new Request(), new Response());
5460

5561
$this->assertEquals(
5662
[
57-
[
58-
'topic' => 'fooTopic',
59-
'command' => null,
60-
'body' => 'fooMessage',
61-
'headers' => [],
62-
'properties' => [],
63-
'priority' => null,
64-
'expire' => null,
65-
'delay' => null,
66-
'timestamp' => null,
67-
'contentType' => null,
68-
'messageId' => null,
63+
'foo' => [
64+
[
65+
'topic' => 'fooTopic1',
66+
'command' => null,
67+
'body' => 'fooMessage',
68+
'headers' => [],
69+
'properties' => [],
70+
'priority' => null,
71+
'expire' => null,
72+
'delay' => null,
73+
'timestamp' => null,
74+
'contentType' => null,
75+
'messageId' => null,
76+
],
77+
[
78+
'topic' => null,
79+
'command' => 'barCommand1',
80+
'body' => 'barMessage',
81+
'headers' => [],
82+
'properties' => [],
83+
'priority' => null,
84+
'expire' => null,
85+
'delay' => null,
86+
'timestamp' => null,
87+
'contentType' => null,
88+
'messageId' => null,
89+
],
6990
],
70-
[
71-
'topic' => null,
72-
'command' => 'barCommand',
73-
'body' => 'barMessage',
74-
'headers' => [],
75-
'properties' => [],
76-
'priority' => null,
77-
'expire' => null,
78-
'delay' => null,
79-
'timestamp' => null,
80-
'contentType' => null,
81-
'messageId' => null,
91+
'bar' => [
92+
[
93+
'topic' => 'fooTopic2',
94+
'command' => null,
95+
'body' => 'fooMessage',
96+
'headers' => [],
97+
'properties' => [],
98+
'priority' => null,
99+
'expire' => null,
100+
'delay' => null,
101+
'timestamp' => null,
102+
'contentType' => null,
103+
'messageId' => null,
104+
],
82105
],
83106
],
84107
$collector->getSentMessages()
@@ -87,29 +110,29 @@ public function testShouldReturnSentMessageArrayTakenFromTraceableProducer()
87110

88111
public function testShouldPrettyPrintKnownPriority()
89112
{
90-
$collector = new MessageQueueCollector($this->createProducerMock());
113+
$collector = new MessageQueueCollector();
91114

92115
$this->assertEquals('normal', $collector->prettyPrintPriority(MessagePriority::NORMAL));
93116
}
94117

95118
public function testShouldPrettyPrintUnknownPriority()
96119
{
97-
$collector = new MessageQueueCollector($this->createProducerMock());
120+
$collector = new MessageQueueCollector();
98121

99122
$this->assertEquals('unknownPriority', $collector->prettyPrintPriority('unknownPriority'));
100123
}
101124

102125
public function testShouldEnsureStringKeepStringSame()
103126
{
104-
$collector = new MessageQueueCollector($this->createProducerMock());
127+
$collector = new MessageQueueCollector();
105128

106129
$this->assertEquals('foo', $collector->ensureString('foo'));
107130
$this->assertEquals('bar baz', $collector->ensureString('bar baz'));
108131
}
109132

110133
public function testShouldEnsureStringEncodeArrayToJson()
111134
{
112-
$collector = new MessageQueueCollector($this->createProducerMock());
135+
$collector = new MessageQueueCollector();
113136

114137
$this->assertEquals('["foo","bar"]', $collector->ensureString(['foo', 'bar']));
115138
}

0 commit comments

Comments
 (0)