@@ -42,9 +42,6 @@ public function __construct(
4242 */
4343 public function handle ()
4444 {
45- $ this ->internalStorageManager ->getVhostQueues ('/ ' );
46-
47-
4845 $ connectionName = (string ) ($ this ->argument ('connection ' ) ?: $ this ->laravel ['config ' ]['queue.default ' ]);
4946
5047 $ connectionConfig = $ this ->laravel ['config ' ]['queue ' ]['connections ' ][$ connectionName ] ?? [];
@@ -57,14 +54,26 @@ public function handle()
5754
5855 $ this ->loadVhosts ();
5956
57+ $ oldVhosts = $ this ->internalStorageManager ->getVhosts ();
58+
6059 if ($ this ->vhosts ->isNotEmpty ()) {
6160 foreach ($ this ->vhosts as $ vhost ) {
62- $ this ->processVhost ($ vhost );
61+ $ vhostDto = $ this ->processVhost ($ vhost );
62+ if (null === $ vhostDto ) {
63+ continue ;
64+ }
65+
66+ $ oldVhostIndex = array_search ($ vhostDto ->getName (), $ oldVhosts , true );
67+ if (false !== $ oldVhostIndex ) {
68+ unset($ oldVhosts [$ oldVhostIndex ]);
69+ }
6370 }
6471 } else {
6572 $ this ->warn (sprintf ('Vhosts for connection "%s" not found. ' , $ connectionName ));
6673 }
6774
75+ $ this ->removeOldsVhosts ($ oldVhosts );
76+
6877 return Command::SUCCESS ;
6978 }
7079
@@ -104,45 +113,84 @@ private function loadVhosts(int $page = 1, int $pageSize = 100): void
104113
105114 /**
106115 * @param array $vhostApiData
107- * @return void
116+ * @return VhostApiDto|null
108117 */
109- private function processVhost (array $ vhostApiData ): void
118+ private function processVhost (array $ vhostApiData ): ? VhostApiDto
110119 {
111120 $ vhostDto = new VhostApiDto ($ vhostApiData );
112121 if ('' === $ vhostDto ->getName ()) {
113- return ;
122+ return null ;
114123 }
115124
116125 $ indexedSuccessfully = $ this ->internalStorageManager ->indexVhost ($ vhostDto );
117126 if (!$ indexedSuccessfully ) {
118127 $ this ->warn (sprintf (
119- 'Skip processing vhost: "%s". Messages ready: %d. ' ,
128+ 'Skip indexation vhost: "%s". Messages ready: %d. ' ,
120129 $ vhostDto ->getName (),
121130 $ vhostDto ->getMessagesReady ()
122131 ));
123132
124- return ;
133+ return null ;
125134 }
126135
127136 $ this ->info (sprintf (
128- 'Start processing vhost: "%s". Messages ready: %d. ' ,
137+ 'Successfully indexed vhost: "%s". Messages ready: %d. ' ,
129138 $ vhostDto ->getName (),
130139 $ vhostDto ->getMessagesReady ()
131140 ));
132141
133142 $ this ->vhostQueues = new Collection ();
143+
134144 $ this ->loadVhostQueues ($ vhostDto );
135145
146+ $ oldVhostQueues = $ this ->internalStorageManager ->getVhostQueues ($ vhostDto ->getName ());
147+
136148 if ($ this ->vhostQueues ->isNotEmpty ()) {
137149 foreach ($ this ->vhostQueues as $ queueApiData ) {
138- $ this ->processVhostQueue ($ queueApiData );
150+ $ processQueueDto = $ this ->processVhostQueue ($ queueApiData );
151+ if (null === $ processQueueDto ) {
152+ continue ;
153+ }
154+
155+ $ oldVhostQueueIndex = array_search ($ processQueueDto ->getName (), $ oldVhostQueues , true );
156+ if (false !== $ oldVhostQueueIndex ) {
157+ unset($ oldVhostQueues [$ oldVhostQueueIndex ]);
158+ }
139159 }
140160 } else {
141161 $ this ->warn (sprintf (
142162 'Queues for vhost "%s" not found. ' ,
143163 $ vhostDto ->getName ()
144164 ));
145165 }
166+
167+ $ this ->removeOldVhostQueues ($ vhostDto , $ oldVhostQueues );
168+
169+ return $ vhostDto ;
170+ }
171+
172+ /**
173+ * @param array $oldVhosts
174+ * @return void
175+ */
176+ private function removeOldsVhosts (array $ oldVhosts ): void
177+ {
178+ if (empty ($ oldVhosts )) {
179+ return ;
180+ }
181+
182+ foreach ($ oldVhosts as $ oldVhostName ) {
183+ $ vhostDto = new VhostApiDto ([
184+ 'name ' => $ oldVhostName ,
185+ ]);
186+
187+ $ this ->internalStorageManager ->removeVhost ($ vhostDto );
188+
189+ $ this ->warn (sprintf (
190+ 'Removed from index vhost: "%s". ' ,
191+ $ vhostDto ->getName ()
192+ ));
193+ }
146194 }
147195
148196 /**
@@ -190,31 +238,60 @@ private function loadVhostQueues(VhostApiDto $vhostDto, int $page = 1, int $page
190238 * @param array $queueApiData
191239 * @return void
192240 */
193- private function processVhostQueue (array $ queueApiData ): void
241+ private function processVhostQueue (array $ queueApiData ): ? QueueApiDto
194242 {
195243 $ queueApiDto = new QueueApiDto ($ queueApiData );
196244 if ('' === $ queueApiDto ->getName ()) {
197- return ;
245+ return null ;
198246 }
199247
200248 $ indexedSuccessfully = $ this ->internalStorageManager ->indexQueue ($ queueApiDto );
201249 if (!$ indexedSuccessfully ) {
202250 $ this ->warn (sprintf (
203- 'Skip processing queue: "%s". Vhost: %s. Messages ready: %d. ' ,
251+ 'Skip indexation queue: "%s". Vhost: %s. Messages ready: %d. ' ,
204252 $ queueApiDto ->getName (),
205253 $ queueApiDto ->getVhostName (),
206254 $ queueApiDto ->getMessagesReady ()
207255 ));
208256
209- return ;
257+ return null ;
210258 }
211259
212260 $ this ->info (sprintf (
213- 'Start processing queue: "%s". Vhost: %s. Messages ready: %d. ' ,
261+ 'Successfully indexed queue: "%s". Vhost: %s. Messages ready: %d. ' ,
214262 $ queueApiDto ->getName (),
215263 $ queueApiDto ->getVhostName (),
216264 $ queueApiDto ->getMessagesReady ()
217265 ));
266+
267+ return $ queueApiDto ;
268+ }
269+
270+ /**
271+ * @param VhostApiDto $vhostDto
272+ * @param array $oldVhostQueues
273+ * @return void
274+ */
275+ private function removeOldVhostQueues (VhostApiDto $ vhostDto , array $ oldVhostQueues ): void
276+ {
277+ if (empty ($ oldVhostQueues )) {
278+ return ;
279+ }
280+
281+ foreach ($ oldVhostQueues as $ oldQueueName ) {
282+ $ queueApiDto = new QueueApiDto ([
283+ 'name ' => $ oldQueueName ,
284+ 'vhost ' => $ vhostDto ->getName (),
285+ ]);
286+
287+ $ this ->internalStorageManager ->removeQueue ($ queueApiDto );
288+
289+ $ this ->warn (sprintf (
290+ 'Removed from index queue: "%s". Vhost: %s. ' ,
291+ $ queueApiDto ->getName (),
292+ $ queueApiDto ->getVhostName ()
293+ ));
294+ }
218295 }
219296}
220297
0 commit comments