|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innmind\ProvisionerBundle\RabbitMQ; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Helper to gather informations about queues |
|
10
|
|
|
* |
|
11
|
|
|
* Layer on top of the rabbitmqadmin cli |
|
12
|
|
|
*/ |
|
13
|
|
|
class Admin |
|
14
|
|
|
{ |
|
15
|
|
|
protected $consumers = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Set a consumer definition |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $name |
|
21
|
|
|
* @param string $queue |
|
22
|
|
|
* @param string $host |
|
23
|
|
|
* @param int $port |
|
24
|
|
|
* @param string $user |
|
25
|
|
|
* @param string $pwd |
|
26
|
|
|
* @param string $vhost |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setConsumerDefinition($name, $queue, $host, $port, $user, $pwd, $vhost) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->consumers[(string) $name] = [ |
|
31
|
|
|
'queue' => (string) $queue, |
|
32
|
|
|
'host' => (string) $host, |
|
33
|
|
|
'port' => (int) $port + 10000, |
|
34
|
|
|
'user' => (string) $user, |
|
35
|
|
|
'pwd' => (string) $pwd, |
|
36
|
|
|
'vhost' => (string) $vhost, |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return the number of messages for the given queue |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $queue |
|
44
|
|
|
* |
|
45
|
|
|
* @return int |
|
46
|
|
|
*/ |
|
47
|
|
|
public function listQueueMessages($queue) |
|
48
|
|
|
{ |
|
49
|
|
|
$conf = $this->consumers[(string) $queue]; |
|
50
|
|
|
|
|
51
|
|
|
$count = 0; |
|
52
|
|
|
$process = new Process(sprintf( |
|
53
|
|
|
'rabbitmqadmin -H %s -P %s -V %s -u %s -p %s list queues name messages --format=kvp', |
|
54
|
|
|
$conf['host'], |
|
55
|
|
|
$conf['port'], |
|
56
|
|
|
$conf['vhost'], |
|
57
|
|
|
$conf['user'], |
|
58
|
|
|
$conf['pwd'] |
|
59
|
|
|
)); |
|
60
|
|
|
$process->run(function ($type, $buffer) use (&$count, $conf) { |
|
61
|
|
|
if (strpos($buffer, 'command not found')) { |
|
62
|
|
|
throw new RuntimeException('Command rabbitmqadmin not found in the system'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
preg_match( |
|
66
|
|
|
'/^name="(?<name>.*)" messages="(?<count>\d+)"$/', |
|
67
|
|
|
trim($buffer), |
|
68
|
|
|
$matches |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
if ( |
|
72
|
|
|
isset($matches['name']) && |
|
73
|
|
|
$matches['name'] === $conf['queue'] |
|
74
|
|
|
) { |
|
75
|
|
|
$count = (int) $matches['count']; |
|
76
|
|
|
} |
|
77
|
|
|
}); |
|
78
|
|
|
|
|
79
|
|
|
return $count; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|