| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Shopware\Elasticsearch\Framework\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\DBAL\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Elasticsearch\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Shopware\Core\Framework\Increment\Exception\IncrementGatewayNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Shopware\Core\Framework\Increment\IncrementGatewayRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Shopware\Core\Framework\Log\Package; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Shopware\Elasticsearch\Admin\AdminElasticsearchHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Shopware\Elasticsearch\Admin\AdminSearchIndexingMessage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | #[Package('system-settings')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class ElasticsearchAdminResetCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected static $defaultName = 'es:admin:reset'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected static $defaultDescription = 'Reset Admin Elasticsearch indexing'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private Client $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private Connection $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private IncrementGatewayRegistry $gatewayRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private AdminElasticsearchHelper $adminEsHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @internal | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         Client $client, | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         Connection $connection, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         IncrementGatewayRegistry $gatewayRegistry, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         AdminElasticsearchHelper $adminEsHelper | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->client = $client; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->connection = $connection; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->gatewayRegistry = $gatewayRegistry; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $this->adminEsHelper = $adminEsHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected function execute(InputInterface $input, OutputInterface $output): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $io = new SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($this->adminEsHelper->getEnabled() !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $io->error('Admin elasticsearch is not enabled'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             return self::FAILURE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $answer = $io->ask('Are you sure you want to reset the Admin Elasticsearch indexing?', 'yes'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if ($answer !== 'yes') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $io->error('Canceled clearing indexing process'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return self::SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $allIndices = $this->client->indices()->get(['index' => $this->adminEsHelper->getPrefix() . '*']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         foreach ($allIndices as $index) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->client->indices()->delete(['index' => $index['settings']['index']['provided_name']]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->connection->executeStatement('TRUNCATE admin_elasticsearch_index_task'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $gateway = $this->gatewayRegistry->get(IncrementGatewayRegistry::MESSAGE_QUEUE_POOL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $gateway->reset('message_queue_stats', AdminSearchIndexingMessage::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } catch (IncrementGatewayNotFoundException $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             // In case message_queue pool is disabled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $io->success('Admin Elasticsearch indices deleted and queue cleared'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return self::SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  |