1
|
|
|
<?php |
2
|
|
|
namespace NeedleProject\LaravelRabbitMq\Command; |
3
|
|
|
|
4
|
|
|
use Illuminate\Console\Command; |
5
|
|
|
use NeedleProject\LaravelRabbitMq\ConsumerInterface; |
6
|
|
|
use NeedleProject\LaravelRabbitMq\Container; |
7
|
|
|
use NeedleProject\LaravelRabbitMq\Entity\AMQPEntityInterface; |
8
|
|
|
use NeedleProject\LaravelRabbitMq\Entity\ExchangeEntity; |
9
|
|
|
use NeedleProject\LaravelRabbitMq\Entity\QueueEntity; |
10
|
|
|
use PhpAmqpLib\Exception\AMQPProtocolChannelException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class SetupCommand |
14
|
|
|
* |
15
|
|
|
* @package NeedleProject\LaravelRabbitMq\Commad |
16
|
|
|
* @author Adrian Tilita <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class SetupCommand extends Command |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'rabbitmq:setup {--force}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Create all queues, exchanges and binds that are defined in entities AND referenced to' . |
33
|
|
|
' either a publisher or a consumer'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Container |
37
|
|
|
*/ |
38
|
|
|
private $container; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* CreateEntitiesCommand constructor. |
42
|
|
|
* |
43
|
|
|
* @param Container $container |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Container $container) |
46
|
|
|
{ |
47
|
|
|
$this->container = $container; |
48
|
|
|
parent::__construct(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param AMQPEntityInterface $entity |
53
|
|
|
* @param string $type |
54
|
|
|
* @param string $resourceName |
55
|
|
|
* @param bool $forceRecreate |
56
|
|
|
*/ |
57
|
|
|
private function createEntity( |
58
|
|
|
AMQPEntityInterface $entity, |
59
|
|
|
string $type, |
60
|
|
|
string $resourceName, |
61
|
|
|
bool $forceRecreate = false |
62
|
|
|
) { |
63
|
|
|
if (true === $forceRecreate) { |
64
|
|
|
$this->output->writeln( |
65
|
|
|
sprintf( |
66
|
|
|
"Deleting <info>%s</info> <fg=yellow>%s</>", |
67
|
|
|
(string)($entity instanceof QueueEntity) ? 'QUEUE' : 'EXCHANGE', |
68
|
|
|
(string)$entity->getAliasName() |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
$entity->delete(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$entity->create(); |
75
|
|
|
$this->output->writeln( |
76
|
|
|
sprintf( |
77
|
|
|
"Created <info>%s</info> <fg=yellow>%s</> for %s [<fg=yellow>%s</>]", |
78
|
|
|
(string)($entity instanceof QueueEntity) ? 'QUEUE' : 'EXCHANGE', |
79
|
|
|
(string)$entity->getAliasName(), |
80
|
|
|
(string)$type, |
81
|
|
|
(string)$resourceName |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Execute the console command. |
88
|
|
|
*/ |
89
|
|
|
public function handle() |
90
|
|
|
{ |
91
|
|
|
$forceRecreate = $this->input->getOption('force'); |
92
|
|
|
|
93
|
|
|
$hasErrors = false; |
94
|
|
|
/** @var QueueEntity|ExchangeEntity $entity */ |
95
|
|
|
foreach ($this->container->getPublishers() as $publisherName => $entity) { |
96
|
|
|
try { |
97
|
|
|
$this->createEntity($entity, 'publisher', $publisherName, $forceRecreate); |
98
|
|
|
} catch (AMQPProtocolChannelException $e) { |
99
|
|
|
$hasErrors = true; |
100
|
|
|
$this->output->error( |
101
|
|
|
sprintf( |
102
|
|
|
"Could not create entity %s for publisher [%s], got:\n%s", |
103
|
|
|
(string)$entity->getAliasName(), |
104
|
|
|
(string)$publisherName, |
105
|
|
|
(string)$e->getMessage() |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
$entity->reconnect(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @var QueueEntity|ExchangeEntity $entity */ |
113
|
|
|
foreach ($this->container->getConsumers() as $publisherName => $entity) { |
114
|
|
|
try { |
115
|
|
|
$this->createEntity($entity, 'consumer', $publisherName, $forceRecreate); |
116
|
|
|
} catch (AMQPProtocolChannelException $e) { |
117
|
|
|
$hasErrors = true; |
118
|
|
|
$this->output->error( |
119
|
|
|
sprintf( |
120
|
|
|
"Could not create entity %s for consumer [%s], got:\n%s", |
121
|
|
|
(string)$entity->getAliasName(), |
122
|
|
|
(string)$publisherName, |
123
|
|
|
(string)$e->getMessage() |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
$entity->reconnect(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->output->block("Create binds"); |
131
|
|
|
/** @var PublisherInterface $entity */ |
132
|
|
|
foreach ($this->container->getPublishers() as $publisherName => $entity) { |
133
|
|
|
try { |
134
|
|
|
$entity->bind(); |
135
|
|
|
$this->output->writeln( |
136
|
|
|
sprintf( |
137
|
|
|
"Created bind <info>%s</info> for publisher [<fg=yellow>%s</>]", |
138
|
|
|
(string)$entity->getAliasName(), |
139
|
|
|
(string)$publisherName |
140
|
|
|
) |
141
|
|
|
); |
142
|
|
|
} catch (\Exception $e) { |
143
|
|
|
$hasErrors = true; |
144
|
|
|
$this->output->error( |
145
|
|
|
sprintf( |
146
|
|
|
"Could not bind entity %s for publisher [%s], got:\n%s", |
147
|
|
|
(string)$entity->getAliasName(), |
148
|
|
|
(string)$publisherName, |
149
|
|
|
(string)$e->getMessage() |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** @var ConsumerInterface $entity */ |
156
|
|
|
foreach ($this->container->getConsumers() as $consumerAliasName => $entity) { |
157
|
|
|
try { |
158
|
|
|
$entity->bind(); |
159
|
|
|
$this->output->writeln( |
160
|
|
|
sprintf( |
161
|
|
|
"Bind entity <info>%s</info> for consumer [<fg=yellow>%s</>]", |
162
|
|
|
(string)$entity->getAliasName(), |
163
|
|
|
(string)$consumerAliasName |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
} catch (\Exception $e) { |
167
|
|
|
$hasErrors = true; |
168
|
|
|
$this->output->error( |
169
|
|
|
sprintf( |
170
|
|
|
"Could not create bind %s for consumer [%s], got:\n%s", |
171
|
|
|
(string)$entity->getAliasName(), |
172
|
|
|
(string)$consumerAliasName, |
173
|
|
|
(string)$e->getMessage() |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
return (int)$hasErrors; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|