88use Illuminate \Support \Facades \Redis ;
99use Salesmessage \LibRabbitMQ \Dto \QueueApiDto ;
1010use Salesmessage \LibRabbitMQ \Dto \VhostApiDto ;
11- use Salesmessage \LibRabbitMQ \Services \Api \RabbitApiClient ;
11+ use Salesmessage \LibRabbitMQ \Services \QueueService ;
12+ use Salesmessage \LibRabbitMQ \Services \VhostsService ;
1213use Throwable ;
1314use Salesmessage \LibRabbitMQ \Services \InternalStorageManager ;
1415
1516class ScanVhostsCommand extends Command
1617{
17- protected $ signature = 'lib-rabbitmq:scan-vhosts
18- {connection? : The name of the queue connection to work} ' ;
18+ protected $ signature = 'lib-rabbitmq:scan-vhosts ' ;
1919
2020 protected $ description = 'Scan and index vhosts ' ;
2121
@@ -24,11 +24,13 @@ class ScanVhostsCommand extends Command
2424 private Collection $ vhostQueues ;
2525
2626 /**
27- * @param RabbitApiClient $apiClient
27+ * @param VhostsService $vhostsService
28+ * @param QueueService $queueService
2829 * @param InternalStorageManager $internalStorageManager
2930 */
3031 public function __construct (
31- private RabbitApiClient $ apiClient ,
32+ private VhostsService $ vhostsService ,
33+ private QueueService $ queueService ,
3234 private InternalStorageManager $ internalStorageManager
3335 ) {
3436 $ this ->vhosts = new Collection ();
@@ -42,17 +44,7 @@ public function __construct(
4244 */
4345 public function handle ()
4446 {
45- $ connectionName = (string ) ($ this ->argument ('connection ' ) ?: $ this ->laravel ['config ' ]['queue.default ' ]);
46-
47- $ connectionConfig = $ this ->laravel ['config ' ]['queue ' ]['connections ' ][$ connectionName ] ?? [];
48- if (empty ($ connectionConfig )) {
49- $ this ->error (sprintf ('Config for connection "%s" not found. ' , $ connectionName ));
50- return Command::INVALID ;
51- }
52-
53- $ this ->apiClient ->setConnectionConfig ($ connectionConfig );
54-
55- $ this ->loadVhosts ();
47+ $ this ->vhosts = $ this ->vhostsService ->getAllVhosts ();
5648
5749 $ oldVhosts = $ this ->internalStorageManager ->getVhosts ();
5850
@@ -69,48 +61,14 @@ public function handle()
6961 }
7062 }
7163 } else {
72- $ this ->warn (sprintf ( 'Vhosts for connection "%s" not found. ' , $ connectionName ) );
64+ $ this ->warn ('Vhosts not found. ' );
7365 }
7466
7567 $ this ->removeOldsVhosts ($ oldVhosts );
7668
7769 return Command::SUCCESS ;
7870 }
7971
80- /**
81- * @param int $page
82- * @param int $pageSize
83- * @return void
84- */
85- private function loadVhosts (int $ page = 1 , int $ pageSize = 100 ): void
86- {
87- try {
88- $ data = $ this ->apiClient ->request (
89- 'GET ' ,
90- '/api/vhosts ' , [
91- 'page ' => $ page ,
92- 'page_size ' => $ pageSize ,
93- 'columns ' => 'name,messages,messages_ready,messages_unacknowledged ' ,
94- ]);
95- } catch (Throwable $ exception ) {
96- $ data = [];
97-
98- $ this ->error (sprintf ('Load vhosts error: %s. ' , (string ) $ exception ->getMessage ()));
99- }
100-
101- $ items = (array ) ($ data ['items ' ] ?? []);
102- if (!empty ($ items )) {
103- $ this ->vhosts ->push (...$ items );
104- }
105-
106- $ nextPage = $ page + 1 ;
107- $ lastPage = (int ) ($ data ['page_count ' ] ?? 1 );
108- if ($ lastPage >= $ nextPage ) {
109- $ this ->loadVhosts ($ nextPage , $ pageSize );
110- return ;
111- }
112- }
113-
11472 /**
11573 * @param array $vhostApiData
11674 * @return VhostApiDto|null
@@ -139,9 +97,7 @@ private function processVhost(array $vhostApiData): ?VhostApiDto
13997 $ vhostDto ->getMessagesReady ()
14098 ));
14199
142- $ this ->vhostQueues = new Collection ();
143-
144- $ this ->loadVhostQueues ($ vhostDto );
100+ $ this ->vhostQueues = $ this ->queueService ->getAllVhostQueues ($ vhostDto );
145101
146102 $ oldVhostQueues = $ this ->internalStorageManager ->getVhostQueues ($ vhostDto ->getName ());
147103
@@ -193,47 +149,6 @@ private function removeOldsVhosts(array $oldVhosts): void
193149 }
194150 }
195151
196- /**
197- * @param VhostApiDto $vhostDto
198- * @param int $page
199- * @param int $pageSize
200- * @return void
201- */
202- private function loadVhostQueues (VhostApiDto $ vhostDto , int $ page = 1 , int $ pageSize = 2 ): void
203- {
204- try {
205- $ data = $ this ->apiClient ->request (
206- 'GET ' ,
207- '/api/queues/ ' . $ vhostDto ->getApiName (), [
208- 'page ' => $ page ,
209- 'page_size ' => $ pageSize ,
210- 'columns ' => 'name,vhost,messages,messages_ready,messages_unacknowledged ' ,
211- 'disable_stats ' => 'true ' ,
212- 'enable_queue_totals ' => 'true ' ,
213- ]);
214- } catch (Throwable $ exception ) {
215- $ data = [];
216-
217- $ this ->error (sprintf (
218- 'Load vhost "%s" queues error: %s. ' ,
219- $ vhostDto ->getName (),
220- (string ) $ exception ->getMessage ()
221- ));
222- }
223-
224- $ items = (array ) ($ data ['items ' ] ?? []);
225- if (!empty ($ items )) {
226- $ this ->vhostQueues ->push (...$ items );
227- }
228-
229- $ nextPage = $ page + 1 ;
230- $ lastPage = (int ) ($ data ['page_count ' ] ?? 1 );
231- if ($ lastPage >= $ nextPage ) {
232- $ this ->loadVhostQueues ($ vhostDto , $ nextPage , $ pageSize );
233- return ;
234- }
235- }
236-
237152 /**
238153 * @param array $queueApiData
239154 * @return void
0 commit comments