-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDeploymentReindexCommand.php
More file actions
116 lines (100 loc) · 3.92 KB
/
DeploymentReindexCommand.php
File metadata and controls
116 lines (100 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
declare(strict_types=1);
/**
* This source file is available under the terms of the
* Pimcore Open Core License (POCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
* @license Pimcore Open Core License (POCL)
*/
namespace Pimcore\Bundle\GenericDataIndexBundle\Command;
use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Exception\CommandAlreadyRunningException;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\ClassDefinition\ClassDefinitionReindexServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexQueue\EnqueueServiceInterface;
use Pimcore\Console\AbstractCommand;
use Pimcore\Model\DataObject\ClassDefinition;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class DeploymentReindexCommand extends AbstractCommand
{
use LockableTrait;
private const OPTION_ALL_CLASSES = 'all-classes';
public function __construct(
private readonly EnqueueServiceInterface $enqueueService,
private readonly ClassDefinitionReindexServiceInterface $classDefinitionReindexService,
?string $name = null
) {
parent::__construct($name);
}
protected function configure(): void
{
$this
->setName('generic-data-index:deployment:reindex')
->addOption(
self::OPTION_ALL_CLASSES,
null,
InputOption::VALUE_NONE,
'Reindex all class definitions, even if the mapping checksum has not changed.'
)
->setDescription(
'Updates index/mapping for all classDefinitions which changed without' .
'deleting them. Afterwards are affected items added into the index queue.'
);
}
/**
* @throws CommandAlreadyRunningException
*
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->lock()) {
throw new CommandAlreadyRunningException(
'The command is already running in another process.'
);
}
try {
$updatedIds = [];
$forceAllClasses = (bool) $input->getOption(self::OPTION_ALL_CLASSES);
$classesList = new ClassDefinition\Listing();
$classes = $classesList->load();
foreach ($classes as $classDefinition) {
$updated = $this->classDefinitionReindexService
->reindexClassDefinition($classDefinition, !$forceAllClasses, true)
;
if ($updated) {
$updatedIds[] = $classDefinition->getId();
}
}
if (!empty($updatedIds)) {
$output->writeln(
sprintf(
'<info>Updated following ClassDefinitions: [%s]</info>',
implode(', ', $updatedIds)
),
OutputInterface::VERBOSITY_NORMAL
);
$output->writeln(
'<info>Dispatch queue messages</info>',
OutputInterface::VERBOSITY_NORMAL
);
$this->enqueueService->dispatchQueueMessages(true);
} else {
$output->writeln('<info>No updates needed - everything is up to date</info>', OutputInterface::VERBOSITY_NORMAL);
}
$output->writeln('<info>Finished</info>', OutputInterface::VERBOSITY_NORMAL);
} catch (Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
} finally {
$this->release();
}
return self::SUCCESS;
}
}