|
27 | 27 | * |
28 | 28 | * @Flow\Scope("singleton") |
29 | 29 | */ |
30 | | -class NodeIndexQueueCommandController extends CommandController { |
31 | | - |
32 | | - /** |
33 | | - * @Flow\Inject |
34 | | - * @var JobManager |
35 | | - */ |
36 | | - protected $jobManager; |
37 | | - |
38 | | - /** |
39 | | - * @var PersistenceManagerInterface |
40 | | - * @Flow\Inject |
41 | | - */ |
42 | | - protected $persistenceManager; |
43 | | - |
44 | | - /** |
45 | | - * @Flow\Inject |
46 | | - * @var NodeTypeMappingBuilder |
47 | | - */ |
48 | | - protected $nodeTypeMappingBuilder; |
49 | | - |
50 | | - /** |
51 | | - * @Flow\Inject |
52 | | - * @var NodeDataRepository |
53 | | - */ |
54 | | - protected $nodeDataRepository; |
55 | | - |
56 | | - /** |
57 | | - * @Flow\Inject |
58 | | - * @var WorkspaceRepository |
59 | | - */ |
60 | | - protected $workspaceRepository; |
61 | | - |
62 | | - /** |
63 | | - * @Flow\Inject |
64 | | - * @var NodeIndexer |
65 | | - */ |
66 | | - protected $nodeIndexer; |
67 | | - |
68 | | - /** |
69 | | - * @Flow\Inject |
70 | | - * @var LoggerInterface |
71 | | - */ |
72 | | - protected $logger; |
73 | | - |
74 | | - /** |
75 | | - * Index all nodes by creating a new index and when everything was completed, switch the index alias. |
76 | | - * |
77 | | - * @param string $workspace |
78 | | - */ |
79 | | - public function buildCommand($workspace = NULL) { |
80 | | - $indexPostfix = time(); |
81 | | - $this->createNextIndex($indexPostfix); |
82 | | - $this->updateMapping(); |
83 | | - |
84 | | - |
85 | | - $this->outputLine(sprintf('Indexing on %s ... ', $indexPostfix)); |
86 | | - |
87 | | - if ($workspace === NULL) { |
88 | | - foreach ($this->workspaceRepository->findAll() as $workspace) { |
89 | | - $this->indexWorkspace($workspace->getName(), $indexPostfix); |
90 | | - } |
91 | | - } else { |
92 | | - $this->indexWorkspace($workspace, $indexPostfix); |
93 | | - } |
94 | | - $updateAliasJob = new UpdateAliasJob($indexPostfix); |
95 | | - $queueName = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer'; |
96 | | - $this->jobManager->queue($queueName, $updateAliasJob); |
97 | | - |
98 | | - $this->outputLine(); |
99 | | - $this->outputLine(sprintf('Indexing jobs created for queue %s with success ...', $queueName)); |
100 | | - } |
101 | | - |
102 | | - /** |
103 | | - * @param string $workspaceName |
104 | | - * @param string $indexPostfix |
105 | | - */ |
106 | | - protected function indexWorkspace($workspaceName, $indexPostfix) { |
107 | | - $offset = 0; |
108 | | - $batchSize = 250; |
109 | | - while (TRUE) { |
110 | | - $iterator = $this->nodeDataRepository->findAllBySiteAndWorkspace($workspaceName, $offset, $batchSize); |
111 | | - |
112 | | - $jobData = []; |
113 | | - |
114 | | - foreach ($this->nodeDataRepository->iterate($iterator) as $data) { |
115 | | - $jobData[] = [ |
116 | | - 'nodeIdentifier' => $data['nodeIdentifier'], |
117 | | - 'dimensions' => $data['dimensions'] |
118 | | - ]; |
119 | | - } |
120 | | - |
121 | | - if ($jobData === []) { |
122 | | - break; |
123 | | - } |
124 | | - |
125 | | - $indexingJob = new IndexingJob($indexPostfix, $workspaceName, $jobData); |
126 | | - $this->jobManager->queue('Flowpack.ElasticSearch.ContentRepositoryQueueIndexer', $indexingJob); |
127 | | - $this->output('.'); |
128 | | - $offset += $batchSize; |
129 | | - $this->persistenceManager->clearState(); |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - /** |
134 | | - * Create next index |
135 | | - * @param string $indexPostfix |
136 | | - */ |
137 | | - protected function createNextIndex($indexPostfix) { |
138 | | - $this->nodeIndexer->setIndexNamePostfix($indexPostfix); |
139 | | - $this->nodeIndexer->getIndex()->create(); |
140 | | - $this->logger->log(sprintf('action=indexing step=index-created index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO); |
141 | | - } |
142 | | - |
143 | | - /** |
144 | | - * Update Index Mapping |
145 | | - */ |
146 | | - protected function updateMapping() { |
147 | | - $nodeTypeMappingCollection = $this->nodeTypeMappingBuilder->buildMappingInformation($this->nodeIndexer->getIndex()); |
148 | | - foreach ($nodeTypeMappingCollection as $mapping) { |
149 | | - /** @var \Flowpack\ElasticSearch\Domain\Model\Mapping $mapping */ |
150 | | - $mapping->apply(); |
151 | | - } |
152 | | - $this->logger->log(sprintf('action=indexing step=mapping-updated index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO); |
153 | | - } |
154 | | - |
155 | | - |
| 30 | +class NodeIndexQueueCommandController extends CommandController |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @Flow\Inject |
| 34 | + * @var JobManager |
| 35 | + */ |
| 36 | + protected $jobManager; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var PersistenceManagerInterface |
| 40 | + * @Flow\Inject |
| 41 | + */ |
| 42 | + protected $persistenceManager; |
| 43 | + |
| 44 | + /** |
| 45 | + * @Flow\Inject |
| 46 | + * @var NodeTypeMappingBuilder |
| 47 | + */ |
| 48 | + protected $nodeTypeMappingBuilder; |
| 49 | + |
| 50 | + /** |
| 51 | + * @Flow\Inject |
| 52 | + * @var NodeDataRepository |
| 53 | + */ |
| 54 | + protected $nodeDataRepository; |
| 55 | + |
| 56 | + /** |
| 57 | + * @Flow\Inject |
| 58 | + * @var WorkspaceRepository |
| 59 | + */ |
| 60 | + protected $workspaceRepository; |
| 61 | + |
| 62 | + /** |
| 63 | + * @Flow\Inject |
| 64 | + * @var NodeIndexer |
| 65 | + */ |
| 66 | + protected $nodeIndexer; |
| 67 | + |
| 68 | + /** |
| 69 | + * @Flow\Inject |
| 70 | + * @var LoggerInterface |
| 71 | + */ |
| 72 | + protected $logger; |
| 73 | + |
| 74 | + /** |
| 75 | + * Index all nodes by creating a new index and when everything was completed, switch the index alias. |
| 76 | + * |
| 77 | + * @param string $workspace |
| 78 | + */ |
| 79 | + public function buildCommand($workspace = null) |
| 80 | + { |
| 81 | + $indexPostfix = time(); |
| 82 | + $this->createNextIndex($indexPostfix); |
| 83 | + $this->updateMapping(); |
| 84 | + |
| 85 | + |
| 86 | + $this->outputLine(sprintf('Indexing on %s ... ', $indexPostfix)); |
| 87 | + |
| 88 | + if ($workspace === null) { |
| 89 | + foreach ($this->workspaceRepository->findAll() as $workspace) { |
| 90 | + $this->indexWorkspace($workspace->getName(), $indexPostfix); |
| 91 | + } |
| 92 | + } else { |
| 93 | + $this->indexWorkspace($workspace, $indexPostfix); |
| 94 | + } |
| 95 | + $updateAliasJob = new UpdateAliasJob($indexPostfix); |
| 96 | + $queueName = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer'; |
| 97 | + $this->jobManager->queue($queueName, $updateAliasJob); |
| 98 | + |
| 99 | + $this->outputLine(); |
| 100 | + $this->outputLine(sprintf('Indexing jobs created for queue %s with success ...', $queueName)); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param string $workspaceName |
| 105 | + * @param string $indexPostfix |
| 106 | + */ |
| 107 | + protected function indexWorkspace($workspaceName, $indexPostfix) |
| 108 | + { |
| 109 | + $offset = 0; |
| 110 | + $batchSize = 250; |
| 111 | + while (true) { |
| 112 | + $iterator = $this->nodeDataRepository->findAllBySiteAndWorkspace($workspaceName, $offset, $batchSize); |
| 113 | + |
| 114 | + $jobData = []; |
| 115 | + |
| 116 | + foreach ($this->nodeDataRepository->iterate($iterator) as $data) { |
| 117 | + $jobData[] = [ |
| 118 | + 'nodeIdentifier' => $data['nodeIdentifier'], |
| 119 | + 'dimensions' => $data['dimensions'] |
| 120 | + ]; |
| 121 | + } |
| 122 | + |
| 123 | + if ($jobData === []) { |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + $indexingJob = new IndexingJob($indexPostfix, $workspaceName, $jobData); |
| 128 | + $this->jobManager->queue('Flowpack.ElasticSearch.ContentRepositoryQueueIndexer', $indexingJob); |
| 129 | + $this->output('.'); |
| 130 | + $offset += $batchSize; |
| 131 | + $this->persistenceManager->clearState(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Create next index |
| 137 | + * @param string $indexPostfix |
| 138 | + */ |
| 139 | + protected function createNextIndex($indexPostfix) |
| 140 | + { |
| 141 | + $this->nodeIndexer->setIndexNamePostfix($indexPostfix); |
| 142 | + $this->nodeIndexer->getIndex()->create(); |
| 143 | + $this->logger->log(sprintf('action=indexing step=index-created index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Update Index Mapping |
| 148 | + */ |
| 149 | + protected function updateMapping() |
| 150 | + { |
| 151 | + $nodeTypeMappingCollection = $this->nodeTypeMappingBuilder->buildMappingInformation($this->nodeIndexer->getIndex()); |
| 152 | + foreach ($nodeTypeMappingCollection as $mapping) { |
| 153 | + /** @var \Flowpack\ElasticSearch\Domain\Model\Mapping $mapping */ |
| 154 | + $mapping->apply(); |
| 155 | + } |
| 156 | + $this->logger->log(sprintf('action=indexing step=mapping-updated index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO); |
| 157 | + } |
156 | 158 | } |
0 commit comments