1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Elasticsearch\Framework\Indexing; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use OpenSearch\Client; |
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Shopware\Core\Framework\Adapter\Storage\AbstractKeyValueStorage; |
9
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
10
|
|
|
use Shopware\Core\Framework\Feature; |
11
|
|
|
use Shopware\Core\Framework\Log\Package; |
12
|
|
|
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler; |
13
|
|
|
use Shopware\Elasticsearch\Framework\ElasticsearchHelper; |
14
|
|
|
use Shopware\Elasticsearch\Framework\Indexing\Event\ElasticsearchIndexAliasSwitchedEvent; |
15
|
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @internal |
19
|
|
|
* |
20
|
|
|
* @final |
21
|
|
|
*/ |
22
|
|
|
#[AsMessageHandler(handles: CreateAliasTask::class)] |
23
|
|
|
#[Package('core')] |
24
|
|
|
class CreateAliasTaskHandler extends ScheduledTaskHandler |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @internal |
28
|
|
|
* |
29
|
|
|
* @param array<mixed> $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
EntityRepository $scheduledTaskRepository, |
33
|
|
|
private readonly Client $client, |
34
|
|
|
private readonly Connection $connection, |
35
|
|
|
private readonly ElasticsearchHelper $elasticsearchHelper, |
36
|
|
|
private readonly array $config, |
37
|
|
|
private readonly EventDispatcherInterface $eventDispatcher, |
38
|
|
|
private readonly AbstractKeyValueStorage $keyValueStorage |
39
|
|
|
) { |
40
|
|
|
parent::__construct($scheduledTaskRepository); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function run(): void |
44
|
|
|
{ |
45
|
|
|
try { |
46
|
|
|
$this->handleQueue(); |
47
|
|
|
} catch (\Throwable $e) { |
48
|
|
|
// catch exception - otherwise the task will never be called again |
49
|
|
|
$this->elasticsearchHelper->logAndThrowException($e); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function createAlias(string $index, string $alias): void |
54
|
|
|
{ |
55
|
|
|
$exist = $this->client->indices()->existsAlias(['name' => $alias]); |
56
|
|
|
|
57
|
|
|
if (!$exist) { |
58
|
|
|
$this->client->indices()->refresh([ |
59
|
|
|
'index' => $index, |
60
|
|
|
]); |
61
|
|
|
$this->client->indices()->putAlias(['index' => $index, 'name' => $alias]); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$actions = [ |
67
|
|
|
['add' => ['index' => $index, 'alias' => $alias]], |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$current = $this->client->indices()->getAlias(['name' => $alias]); |
71
|
|
|
$current = array_keys($current); |
72
|
|
|
|
73
|
|
|
foreach ($current as $value) { |
74
|
|
|
$actions[] = ['remove' => ['index' => $value, 'alias' => $alias]]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->client->indices()->updateAliases(['body' => ['actions' => $actions]]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function handleQueue(): void |
81
|
|
|
{ |
82
|
|
|
/** |
83
|
|
|
* @deprecated tag:v6.6.0 - Will be removed |
84
|
|
|
*/ |
85
|
|
|
if (Feature::isActive('ES_MULTILINGUAL_INDEX')) { |
86
|
|
|
$this->keyValueStorage->set(ElasticsearchHelper::ENABLE_MULTILINGUAL_INDEX_KEY, 1); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$indices = $this->connection->fetchAllAssociative('SELECT * FROM elasticsearch_index_task'); |
90
|
|
|
if (empty($indices)) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$changes = []; |
95
|
|
|
|
96
|
|
|
foreach ($indices as $row) { |
97
|
|
|
$index = $row['index']; |
98
|
|
|
$count = (int) $row['doc_count']; |
99
|
|
|
|
100
|
|
|
$this->client->indices()->refresh(['index' => $index]); |
101
|
|
|
|
102
|
|
|
if ($count > 0) { |
103
|
|
|
continue; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$alias = $row['alias']; |
107
|
|
|
|
108
|
|
|
$this->createAlias($index, $alias); |
109
|
|
|
|
110
|
|
|
$this->client->indices()->putSettings([ |
111
|
|
|
'index' => $index, |
112
|
|
|
'body' => [ |
113
|
|
|
'number_of_replicas' => $this->config['settings']['index']['number_of_replicas'], |
114
|
|
|
'refresh_interval' => null, |
115
|
|
|
], |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$this->connection->executeStatement( |
119
|
|
|
'DELETE FROM elasticsearch_index_task WHERE id = :id', |
120
|
|
|
['id' => $row['id']] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$changes[(string) $index] = $alias; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->eventDispatcher->dispatch(new ElasticsearchIndexAliasSwitchedEvent($changes)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|