1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Locastic\Loggastic\Command; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Common\Exceptions\BadRequest400Exception; |
6
|
|
|
use Locastic\Loggastic\Bridge\Elasticsearch\Index\ElasticsearchIndexFactoryInterface; |
7
|
|
|
use Locastic\Loggastic\Metadata\LoggableContext\Factory\LoggableContextCollectionFactoryInterface; |
8
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
|
14
|
|
|
#[AsCommand('locastic:activity-logs:create-loggable-indexes')] |
15
|
|
|
final class CreateLoggableIndexesCommand extends Command |
16
|
|
|
{ |
17
|
|
|
public function __construct(private readonly LoggableContextCollectionFactoryInterface $loggableContextCollectionFactory, private readonly ElasticsearchIndexFactoryInterface $elasticsearchIndexFactory) |
18
|
|
|
{ |
19
|
|
|
parent::__construct(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
23
|
|
|
{ |
24
|
|
|
$io = new SymfonyStyle($input, $output); |
25
|
|
|
|
26
|
|
|
$io->title('Creating activity log indexes ...'); |
27
|
|
|
$loggableContextCollection = $this->loggableContextCollectionFactory->create(); |
28
|
|
|
|
29
|
|
|
foreach ($loggableContextCollection->getIterator() as $loggableClass => $config) { |
30
|
|
|
$io->writeln('Creating '.$loggableClass.' activity_log index'); |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$this->elasticsearchIndexFactory->createActivityLogIndex($loggableClass); |
|
|
|
|
34
|
|
|
} catch (BadRequest400Exception $e) { |
35
|
|
|
if (strpos($e->getMessage(), 'resource_already_exists_exception')) { |
36
|
|
|
$output->writeln('Index already exists, skipping.'); |
37
|
|
|
} else { |
38
|
|
|
throw $e; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$io->writeln('Creating '.$loggableClass.' current_data_tracker index'); |
43
|
|
|
|
44
|
|
|
try { |
45
|
|
|
$this->elasticsearchIndexFactory->createCurrentDataTrackerLogIndex($loggableClass); |
|
|
|
|
46
|
|
|
} catch (BadRequest400Exception $e) { |
47
|
|
|
if (strpos($e->getMessage(), 'resource_already_exists_exception')) { |
48
|
|
|
$output->writeln('Index already exists, skipping.'); |
49
|
|
|
} else { |
50
|
|
|
throw $e; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$io->success('Done!'); |
56
|
|
|
|
57
|
|
|
return Command::SUCCESS; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|