Skip to content

Commit d43798b

Browse files
committed
multi client configuration
1 parent 152e9ca commit d43798b

File tree

7 files changed

+133
-49
lines changed

7 files changed

+133
-49
lines changed

pkg/enqueue-bundle/DependencyInjection/Configuration.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Enqueue\Bundle\DependencyInjection;
44

5+
use Enqueue\AsyncCommand\RunCommandProcessor;
56
use Enqueue\Monitoring\Symfony\DependencyInjection\MonitoringFactory;
67
use Enqueue\Symfony\Client\DependencyInjection\ClientFactory;
78
use Enqueue\Symfony\DependencyInjection\TransportFactory;
9+
use Enqueue\Symfony\MissingComponentFactory;
10+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
811
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
912
use Symfony\Component\Config\Definition\ConfigurationInterface;
1013

@@ -56,17 +59,14 @@ public function getConfigTreeBuilder(): TreeBuilder
5659
->append(TransportFactory::getConfiguration())
5760
->append(TransportFactory::getQueueConsumerConfiguration())
5861
->append(ClientFactory::getConfiguration($this->debug))
59-
->append(MonitoringFactory::getConfiguration())
62+
->append($this->getMonitoringConfiguration())
63+
->append($this->getAsyncCommandsConfiguration())
6064
->arrayNode('extensions')->addDefaultsIfNotSet()->children()
6165
->booleanNode('doctrine_ping_connection_extension')->defaultFalse()->end()
6266
->booleanNode('doctrine_clear_identity_map_extension')->defaultFalse()->end()
6367
->booleanNode('signal_extension')->defaultValue(function_exists('pcntl_signal_dispatch'))->end()
6468
->booleanNode('reply_extension')->defaultTrue()->end()
6569
->end()->end()
66-
->arrayNode('async_commands')
67-
->addDefaultsIfNotSet()
68-
->canBeEnabled()
69-
->end()
7070
->end()
7171
->end()
7272
;
@@ -81,4 +81,25 @@ public function getConfigTreeBuilder(): TreeBuilder
8181

8282
return $tb;
8383
}
84+
85+
private function getMonitoringConfiguration(): ArrayNodeDefinition
86+
{
87+
if (false === class_exists(MonitoringFactory::class)) {
88+
return MissingComponentFactory::getConfiguration('monitoring', ['enqueue/monitoring']);
89+
}
90+
91+
return MonitoringFactory::getConfiguration();
92+
}
93+
94+
private function getAsyncCommandsConfiguration(): ArrayNodeDefinition
95+
{
96+
if (false === class_exists(RunCommandProcessor::class)) {
97+
return MissingComponentFactory::getConfiguration('async_commands', ['enqueue/async-command']);
98+
}
99+
100+
return (new ArrayNodeDefinition('async_commands'))
101+
->addDefaultsIfNotSet()
102+
->canBeEnabled()
103+
;
104+
}
84105
}

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private function loadAsyncCommands(array $config, ContainerBuilder $container):
286286
{
287287
$configNames = [];
288288
foreach ($config as $name => $modules) {
289-
if ($modules['async_commands']['enabled']) {
289+
if (false === empty($modules['async_commands']['enabled'])) {
290290
$configNames[] = $name;
291291
}
292292
}

pkg/enqueue-bundle/Profiler/MessageQueueCollector.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public function collect(Request $request, Response $response, \Exception $except
3636
}
3737
}
3838

39+
public function getCount(): int
40+
{
41+
$count = 0;
42+
foreach ($this->data as $name => $messages) {
43+
$count += count($messages);
44+
}
45+
46+
return $count;
47+
}
48+
3949
/**
4050
* @return array
4151
*/

pkg/enqueue-bundle/Resources/views/Profiler/panel.html.twig

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{% extends '@WebProfiler/Profiler/layout.html.twig' %}
22

33
{% block toolbar %}
4-
{% if collector.sentMessages|length > 0 %}
4+
{% if collector.count > 0 %}
55
{% set icon %}
66
{{ include('@Enqueue/Icon/icon.svg') }}
77
<span class="sf-toolbar-value">
8-
{{ collector.sentMessages|length }}</span>
8+
{{ collector.count }}</span>
99
{% endset %}
1010

1111
{% set text %}
1212
<div class="sf-toolbar-info-piece">
1313
<b>Sent messages</b>
14-
<span class="sf-toolbar-status">{{ collector.sentMessages|length }}</span>
14+
<span class="sf-toolbar-status">{{ collector.count }}</span>
1515
</div>
1616
{% endset %}
1717

@@ -20,54 +20,63 @@
2020
{% endblock %}
2121

2222
{% block menu %}
23-
<span class="label {{ not collector.sentMessages ? 'disabled' }}">
23+
<span class="label {{ not collector.count ? 'disabled' }}">
2424
<span class="icon">{{ include('@Enqueue/Icon/icon.svg') }}</span>
2525
<strong>Message Queue</strong>
2626
</span>
2727
{% endblock %}
2828

2929
{% block panel %}
30+
{% if collector.count > 0 %}
3031
<h2>Sent messages</h2>
31-
{% if collector.sentMessages|length > 0 %}
32-
<table>
33-
<thead>
34-
<tr>
35-
<th>#</th>
36-
<th>Topic</th>
37-
<th>Command</th>
38-
<th>Message</th>
39-
<th>Priority</th>
40-
</tr>
41-
</thead>
42-
<tbody>
43-
{% for sentMessage in collector.sentMessages %}
44-
<tr>
45-
<td>{{ loop.index }}</td>
46-
<td>{{ sentMessage.topic|default(null) }}</td>
47-
<td>{{ sentMessage.command|default(null) }}</td>
48-
<td style="width: 70%" class="metadata">
49-
<span>
50-
{% set body = collector.ensureString(sentMessage.body) %}
51-
{{ body|length > 40 ? body|slice(0, 40) ~ '...' : body }}
52-
</span>
53-
{% if body|length > 40 %}
54-
<a class="btn btn-link text-small sf-toggle"
55-
data-toggle-selector="#message-body-{{ loop.index }}"
56-
data-toggle-alt-content="Hide body"
57-
>Show body</a>
58-
<div id="message-body-{{ loop.index }}"
59-
class="context sf-toggle-content sf-toggle-hidden">
60-
<pre>{{ body }}</pre>
61-
</div>
62-
{% endif %}
63-
<td>
64-
<span title="{{ sentMessage.priority }}">{{ collector.prettyPrintPriority(sentMessage.priority) }}</span>
65-
</td>
66-
</tr>
67-
{% endfor %}
68-
</tbody>
32+
{% for clientName, sentMessages in collector.sentMessages %}
33+
{% if sentMessages|length > 0 %}
34+
<h3>Client: {{ clientName }}</h3>
35+
<table>
36+
<thead>
37+
<tr>
38+
<th>#</th>
39+
<th>Topic</th>
40+
<th>Command</th>
41+
<th>Message</th>
42+
<th>Priority</th>
43+
<th>Time</th>
44+
</tr>
45+
</thead>
46+
<tbody>
47+
{% for sentMessage in sentMessages %}
48+
<tr>
49+
<td>{{ loop.index }}</td>
50+
<td>{{ sentMessage.topic|default(null) }}</td>
51+
<td>{{ sentMessage.command|default(null) }}</td>
52+
<td style="width: 70%" class="metadata">
53+
<span>
54+
{% set body = collector.ensureString(sentMessage.body) %}
55+
{{ body|length > 40 ? body|slice(0, 40) ~ '...' : body }}
56+
</span>
57+
{% if body|length > 40 %}
58+
<a class="btn btn-link text-small sf-toggle"
59+
data-toggle-selector="#message-body-{{ loop.index }}"
60+
data-toggle-alt-content="Hide body"
61+
>Show body</a>
62+
<div id="message-body-{{ loop.index }}"
63+
class="context sf-toggle-content sf-toggle-hidden">
64+
<pre>{{ body }}</pre>
65+
</div>
66+
{% endif %}
67+
<td>
68+
<span title="{{ sentMessage.priority }}">{{ collector.prettyPrintPriority(sentMessage.priority) }}</span>
69+
</td>
70+
<td style="white-space:nowrap;">
71+
{{ sentMessage.sentAt|date('i:s.v') }}
72+
</td>
73+
</tr>
74+
{% endfor %}
75+
</tbody>
6976

70-
</table>
77+
</table>
78+
{% endif %}
79+
{% endfor %}
7180
{% else %}
7281
<div class="empty">
7382
<p>No messages were sent.</p>

pkg/enqueue/Client/TraceableProducer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private function collectTrace(string $topic = null, string $command = null, $mes
8585
'timestamp' => null,
8686
'contentType' => null,
8787
'messageId' => null,
88+
'sentAt' => (new \DateTime())->format('Y-m-d H:i:s.u'),
8889
];
8990

9091
if ($message instanceof Message) {

pkg/enqueue/Symfony/Client/DependencyInjection/ClientFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function build(ContainerBuilder $container, array $config, bool $default
107107
;
108108

109109
$container->register($this->format('producer'), Producer::class)
110+
->setPublic(true)
110111
->addArgument($this->reference('driver'))
111112
->addArgument($this->reference('rpc_factory'))
112113
->addArgument($this->reference('client_extensions'))
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Enqueue\Symfony;
4+
5+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6+
7+
class MissingComponentFactory
8+
{
9+
public static function getConfiguration(string $componentName, array $packages): ArrayNodeDefinition
10+
{
11+
if (1 == count($packages)) {
12+
$message = sprintf(
13+
'In order to use the component "%s" install a package "%s"',
14+
$componentName,
15+
implode('", "', $packages)
16+
);
17+
} else {
18+
$message = sprintf(
19+
'In order to use the component "%s" install one of the packages "%s"',
20+
$componentName,
21+
implode('", "', $packages)
22+
);
23+
}
24+
25+
$node = new ArrayNodeDefinition($componentName);
26+
$node
27+
->info($message)
28+
->beforeNormalization()
29+
->always(function () {
30+
return [];
31+
})
32+
->end()
33+
->validate()
34+
->always(function () use ($message) {
35+
throw new \InvalidArgumentException($message);
36+
})
37+
->end()
38+
;
39+
40+
return $node;
41+
}
42+
}

0 commit comments

Comments
 (0)