1313 * source code.
1414 */
1515
16+ use Doctrine \Common \Collections \ArrayCollection ;
1617use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Driver \NodeTypeMappingBuilderInterface ;
1718use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Exception \ConfigurationException ;
1819use Flowpack \ElasticSearch \ContentRepositoryAdaptor \Indexer \NodeIndexer ;
@@ -98,26 +99,29 @@ class NodeIndexQueueCommandController extends CommandController
9899 */
99100 protected $ batchSize ;
100101
102+ /**
103+ * @Flow\Inject
104+ * @var \Neos\ContentRepository\Domain\Service\ContentDimensionCombinator
105+ */
106+ protected $ contentDimensionCombinator ;
107+
101108 /**
102109 * Index all nodes by creating a new index and when everything was completed, switch the index alias.
103110 *
104111 * @param string $workspace
105- * @throws ConfigurationException
106112 * @throws Exception
107113 * @throws StopCommandException
108114 * @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
109115 * @throws \Flowpack\ElasticSearch\Exception
110- * @throws \Neos\Flow\Http\Exception
111116 * @throws \Exception
112117 */
113118 public function buildCommand (string $ workspace = null ): void
114119 {
115- $ indexPostfix = (string ) time ();
116- $ indexName = $ this ->createNextIndex ($ indexPostfix );
120+ $ indexPostfix = (string )time ();
117121 $ this ->updateMapping ($ indexPostfix );
118122
119123 $ this ->outputLine ();
120- $ this ->outputLine ('<b>Indexing on %s ...</b> ' , [$ indexName ]);
124+ $ this ->outputLine ('<b>Indexing on %s ...</b> ' , [$ indexPostfix ]);
121125
122126 $ pendingJobs = $ this ->queueManager ->getQueue (self ::BATCH_QUEUE_NAME )->countReady ();
123127 if ($ pendingJobs !== 0 ) {
@@ -136,8 +140,11 @@ public function buildCommand(string $workspace = null): void
136140 $ this ->indexWorkspace ($ workspace , $ indexPostfix );
137141 }
138142
139- $ updateAliasJob = new UpdateAliasJob ($ indexPostfix );
140- $ this ->jobManager ->queue (self ::BATCH_QUEUE_NAME , $ updateAliasJob );
143+ $ combinations = new ArrayCollection ($ this ->contentDimensionCombinator ->getAllAllowedCombinations ());
144+ $ combinations ->map (function (array $ dimensionValues ) use ($ indexPostfix ) {
145+ $ updateAliasJob = new UpdateAliasJob ($ indexPostfix , $ dimensionValues );
146+ $ this ->jobManager ->queue (self ::BATCH_QUEUE_NAME , $ updateAliasJob );
147+ });
141148
142149 $ this ->outputLine ('Indexing jobs created for queue %s with success ... ' , [self ::BATCH_QUEUE_NAME ]);
143150 $ this ->outputSystemReport ();
@@ -159,9 +166,11 @@ public function workCommand(string $queue = 'batch', int $exitAfter = null, int
159166 'batch ' => self ::BATCH_QUEUE_NAME ,
160167 'live ' => self ::LIVE_QUEUE_NAME
161168 ];
169+
162170 if (!isset ($ allowedQueues [$ queue ])) {
163171 $ this ->output ('Invalid queue, should be "live" or "batch" ' );
164172 }
173+
165174 $ queueName = $ allowedQueues [$ queue ];
166175
167176 if ($ verbose ) {
@@ -181,37 +190,45 @@ public function workCommand(string $queue = 'batch', int $exitAfter = null, int
181190 if ($ exitAfter !== null ) {
182191 $ timeout = max (1 , $ exitAfter - (time () - $ startTime ));
183192 }
193+
194+
184195 try {
185196 $ message = $ this ->jobManager ->waitAndExecute ($ queueName , $ timeout );
186- } catch (Exception $ exception ) {
197+ } catch (\ Exception $ exception ) {
187198 $ numberOfJobExecutions ++;
188- $ this ->outputLine ('<error>%s</error> ' , [$ exception ->getMessage ()]);
189- if ($ verbose && $ exception ->getPrevious () instanceof \Exception) {
190- $ this ->outputLine (' Reason: %s ' , [$ exception ->getPrevious ()->getMessage ()]);
199+
200+ $ verbose && $ this ->outputLine ('<error>%s</error> ' , [$ exception ->getMessage ()]);
201+
202+ if ($ exception ->getPrevious () instanceof \Exception) {
203+ $ verbose && $ this ->outputLine (' Reason: %s ' , [$ exception ->getPrevious ()->getMessage ()]);
204+ $ this ->logger ->error (sprintf ('Indexing job failed: %s. Detailed reason %s ' , $ exception ->getMessage (), $ exception ->getPrevious ()->getMessage ()), LogEnvironment::fromMethodName (__METHOD__ ));
205+ } else {
206+ $ this ->logger ->error ('Indexing job failed: ' . $ exception ->getMessage (), LogEnvironment::fromMethodName (__METHOD__ ));
191207 }
192- } catch (\Exception $ exception ) {
193- $ this ->outputLine ('<error>Unexpected exception during job execution: %s, aborting...</error> ' , [$ exception ->getMessage ()]);
194- $ this ->quit (1 );
195208 }
209+
196210 if ($ message !== null ) {
197211 $ numberOfJobExecutions ++;
198212 if ($ verbose ) {
199213 $ messagePayload = strlen ($ message ->getPayload ()) <= 50 ? $ message ->getPayload () : substr ($ message ->getPayload (), 0 , 50 ) . '... ' ;
200214 $ this ->outputLine ('<success>Successfully executed job "%s" (%s)</success> ' , [$ message ->getIdentifier (), $ messagePayload ]);
201215 }
202216 }
217+
203218 if ($ exitAfter !== null && (time () - $ startTime ) >= $ exitAfter ) {
204219 if ($ verbose ) {
205220 $ this ->outputLine ('Quitting after %d seconds due to <i>--exit-after</i> flag ' , [time () - $ startTime ]);
206221 }
207222 $ this ->quit ();
208223 }
224+
209225 if ($ limit !== null && $ numberOfJobExecutions >= $ limit ) {
210226 if ($ verbose ) {
211227 $ this ->outputLine ('Quitting after %d executed job%s due to <i>--limit</i> flag ' , [$ numberOfJobExecutions , $ numberOfJobExecutions > 1 ? 's ' : '' ]);
212228 }
213229 $ this ->quit ();
214230 }
231+
215232 } while (true );
216233 }
217234
@@ -232,7 +249,7 @@ public function flushCommand(): void
232249 /**
233250 * Output system report for CLI commands
234251 */
235- protected function outputSystemReport ()
252+ protected function outputSystemReport (): void
236253 {
237254 $ this ->outputLine ();
238255 $ this ->outputLine ('Memory Usage : %s ' , [Files::bytesToSizeString (memory_get_peak_usage (true ))]);
@@ -291,40 +308,30 @@ protected function indexWorkspace(string $workspaceName, string $indexPostfix):
291308 $ this ->outputLine ();
292309 }
293310
294- /**
295- * @param string $indexPostfix
296- * @return string
297- * @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
298- * @throws ConfigurationException
299- * @throws \Flowpack\ElasticSearch\Exception
300- * @throws \Neos\Flow\Http\Exception
301- */
302- protected function createNextIndex (string $ indexPostfix ): string
303- {
304- $ this ->nodeIndexer ->setIndexNamePostfix ($ indexPostfix );
305- $ this ->nodeIndexer ->getIndex ()->create ();
306- $ this ->logger ->info (sprintf ('Index %s created ' , $ this ->nodeIndexer ->getIndexName ()), LogEnvironment::fromMethodName (__METHOD__ ));
307-
308- return $ this ->nodeIndexer ->getIndexName ();
309- }
310-
311311 /**
312312 * Update Index Mapping
313313 *
314314 * @param string $indexPostfix
315315 * @return void
316- * @throws ConfigurationException
317316 * @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
318317 * @throws \Flowpack\ElasticSearch\Exception
319318 */
320319 protected function updateMapping (string $ indexPostfix ): void
321320 {
322- $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
323- foreach ($ nodeTypeMappingCollection as $ mapping ) {
321+ $ combinations = new ArrayCollection ($ this ->contentDimensionCombinator ->getAllAllowedCombinations ());
322+ $ combinations ->map (function (array $ dimensionValues ) use ($ indexPostfix ) {
323+ $ this ->nodeIndexer ->setDimensions ($ dimensionValues );
324324 $ this ->nodeIndexer ->setIndexNamePostfix ($ indexPostfix );
325- /** @var Mapping $mapping */
326- $ mapping ->apply ();
327- }
328- $ this ->logger ->info (sprintf ('Mapping updated for index %s ' , $ this ->nodeIndexer ->getIndexName ()), LogEnvironment::fromMethodName (__METHOD__ ));
325+
326+ if (!$ this ->nodeIndexer ->getIndex ()->exists ()) {
327+ $ this ->nodeIndexer ->getIndex ()->create ();
328+ }
329+ $ nodeTypeMappingCollection = $ this ->nodeTypeMappingBuilder ->buildMappingInformation ($ this ->nodeIndexer ->getIndex ());
330+ foreach ($ nodeTypeMappingCollection as $ mapping ) {
331+ /** @var Mapping $mapping */
332+ $ mapping ->apply ();
333+ }
334+ $ this ->logger ->info (sprintf ('Mapping updated for index %s ' , $ this ->nodeIndexer ->getIndexName ()), LogEnvironment::fromMethodName (__METHOD__ ));
335+ });
329336 }
330337}
0 commit comments