Skip to content

Commit e033102

Browse files
Merge pull request #39 from daniellienert/task/cradapter-v6-compat
WIP: TASK: Refactor package / make compatible to ES CRAdapter 6.x
2 parents 6442ddf + 27c97eb commit e033102

File tree

10 files changed

+113
-86
lines changed

10 files changed

+113
-86
lines changed

Classes/AbstractIndexingJob.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
2020
use Neos\Flow\Annotations as Flow;
2121
use Neos\Flow\Utility\Algorithms;
22+
use Psr\Log\LoggerInterface;
2223

2324
abstract class AbstractIndexingJob implements JobInterface
2425
{
25-
use LoggerTrait;
26+
27+
/**
28+
* @FLow\Inject
29+
* @var LoggerInterface
30+
*/
31+
protected $logger;
2632

2733
/**
2834
* @var NodeIndexer
@@ -78,6 +84,7 @@ abstract class AbstractIndexingJob implements JobInterface
7884
* @param string $indexPostfix
7985
* @param string $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
8086
* @param array $nodes
87+
* @throws \Exception
8188
*/
8289
public function __construct($indexPostfix, $targetWorkspaceName, array $nodes)
8390
{
@@ -92,7 +99,7 @@ public function __construct($indexPostfix, $targetWorkspaceName, array $nodes)
9299
*
93100
* @return string A job identifier
94101
*/
95-
public function getIdentifier()
102+
public function getIdentifier(): string
96103
{
97104
return $this->identifier;
98105
}

Classes/Command/NodeIndexQueueCommandController.php

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Command;
35

46
/*
@@ -12,6 +14,7 @@
1214
*/
1315

1416
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
17+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
1518
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
1619
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Repository\NodeDataRepository;
1720
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\IndexingJob;
@@ -24,8 +27,12 @@
2427
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
2528
use Neos\Flow\Annotations as Flow;
2629
use Neos\Flow\Cli\CommandController;
30+
use Neos\Flow\Cli\Exception\StopCommandException;
31+
use Neos\Flow\Log\Utility\LogEnvironment;
32+
use Neos\Flow\Mvc\Exception\StopActionException;
2733
use Neos\Flow\Persistence\PersistenceManagerInterface;
2834
use Neos\Utility\Files;
35+
use Psr\Log\LoggerInterface;
2936

3037
/**
3138
* Provides CLI features for index handling
@@ -34,10 +41,14 @@
3441
*/
3542
class NodeIndexQueueCommandController extends CommandController
3643
{
37-
use LoggerTrait;
44+
protected const BATCH_QUEUE_NAME = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer';
45+
public const LIVE_QUEUE_NAME = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer.Live';
3846

39-
const BATCH_QUEUE_NAME = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer';
40-
const LIVE_QUEUE_NAME = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer.Live';
47+
/**
48+
* @Flow\Inject
49+
* @var LoggerInterface
50+
*/
51+
protected $logger;
4152

4253
/**
4354
* @var JobManager
@@ -91,13 +102,17 @@ class NodeIndexQueueCommandController extends CommandController
91102
* Index all nodes by creating a new index and when everything was completed, switch the index alias.
92103
*
93104
* @param string $workspace
94-
* @throws \Flowpack\JobQueue\Common\Exception
95-
* @throws \Neos\Flow\Mvc\Exception\StopActionException
105+
* @throws ConfigurationException
106+
* @throws Exception
107+
* @throws StopCommandException
96108
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
109+
* @throws \Flowpack\ElasticSearch\Exception
110+
* @throws \Neos\Flow\Http\Exception
111+
* @throws \Exception
97112
*/
98-
public function buildCommand($workspace = null)
113+
public function buildCommand(string $workspace = null): void
99114
{
100-
$indexPostfix = time();
115+
$indexPostfix = (string) time();
101116
$indexName = $this->createNextIndex($indexPostfix);
102117
$this->updateMapping($indexPostfix);
103118

@@ -120,10 +135,11 @@ public function buildCommand($workspace = null)
120135
$this->outputLine();
121136
$this->indexWorkspace($workspace, $indexPostfix);
122137
}
138+
123139
$updateAliasJob = new UpdateAliasJob($indexPostfix);
124140
$this->jobManager->queue(self::BATCH_QUEUE_NAME, $updateAliasJob);
125141

126-
$this->outputLine("Indexing jobs created for queue %s with success ...", [self::BATCH_QUEUE_NAME]);
142+
$this->outputLine('Indexing jobs created for queue %s with success ...', [self::BATCH_QUEUE_NAME]);
127143
$this->outputSystemReport();
128144
$this->outputLine();
129145
}
@@ -134,9 +150,10 @@ public function buildCommand($workspace = null)
134150
* @param int $limit If set, only the given amount of jobs are processed (successful or not) before the script exits
135151
* @param bool $verbose Output debugging information
136152
* @return void
137-
* @throws \Neos\Flow\Mvc\Exception\StopActionException
153+
* @throws StopActionException
154+
* @throws StopCommandException
138155
*/
139-
public function workCommand($queue = 'batch', $exitAfter = null, $limit = null, $verbose = false)
156+
public function workCommand(string $queue = 'batch', int $exitAfter = null, int $limit = null, $verbose = false): void
140157
{
141158
$allowedQueues = [
142159
'batch' => self::BATCH_QUEUE_NAME,
@@ -201,7 +218,7 @@ public function workCommand($queue = 'batch', $exitAfter = null, $limit = null,
201218
/**
202219
* Flush the index queue
203220
*/
204-
public function flushCommand()
221+
public function flushCommand(): void
205222
{
206223
try {
207224
$this->queueManager->getQueue(self::BATCH_QUEUE_NAME)->flush();
@@ -235,8 +252,9 @@ protected function outputSystemReport()
235252
/**
236253
* @param string $workspaceName
237254
* @param string $indexPostfix
255+
* @throws \Exception
238256
*/
239-
protected function indexWorkspace($workspaceName, $indexPostfix)
257+
protected function indexWorkspace(string $workspaceName, string $indexPostfix): void
240258
{
241259
$this->outputLine('<info>++</info> Indexing %s workspace', [$workspaceName]);
242260
$nodeCounter = 0;
@@ -277,30 +295,36 @@ protected function indexWorkspace($workspaceName, $indexPostfix)
277295
* @param string $indexPostfix
278296
* @return string
279297
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
298+
* @throws ConfigurationException
299+
* @throws \Flowpack\ElasticSearch\Exception
300+
* @throws \Neos\Flow\Http\Exception
280301
*/
281-
protected function createNextIndex($indexPostfix)
302+
protected function createNextIndex(string $indexPostfix): string
282303
{
283304
$this->nodeIndexer->setIndexNamePostfix($indexPostfix);
284305
$this->nodeIndexer->getIndex()->create();
285-
$this->log(sprintf('action=indexing step=index-created index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO);
306+
$this->logger->info(sprintf('Index %s created', $this->nodeIndexer->getIndexName()), LogEnvironment::fromMethodName(__METHOD__));
286307

287308
return $this->nodeIndexer->getIndexName();
288309
}
289310

290311
/**
291312
* Update Index Mapping
292313
*
314+
* @param string $indexPostfix
293315
* @return void
316+
* @throws ConfigurationException
294317
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
318+
* @throws \Flowpack\ElasticSearch\Exception
295319
*/
296-
protected function updateMapping($indexPostfix)
320+
protected function updateMapping(string $indexPostfix): void
297321
{
298322
$nodeTypeMappingCollection = $this->nodeTypeMappingBuilder->buildMappingInformation($this->nodeIndexer->getIndex());
299323
foreach ($nodeTypeMappingCollection as $mapping) {
300324
$this->nodeIndexer->setIndexNamePostfix($indexPostfix);
301325
/** @var Mapping $mapping */
302326
$mapping->apply();
303327
}
304-
$this->log(sprintf('action=indexing step=mapping-updated index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO);
328+
$this->logger->info(sprintf('Mapping updated for index %s', $this->nodeIndexer->getIndexName()), LogEnvironment::fromMethodName(__METHOD__));
305329
}
306330
}

Classes/Domain/Repository/NodeDataRepository.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Repository;
35

46
/*
@@ -12,6 +14,8 @@
1214
*/
1315

1416
use Doctrine\Common\Persistence\ObjectManager;
17+
use Doctrine\ORM\EntityManager;
18+
use Doctrine\ORM\EntityManagerInterface;
1519
use Doctrine\ORM\Internal\Hydration\IterableResult;
1620
use Doctrine\ORM\QueryBuilder;
1721
use Neos\ContentRepository\Domain\Model\NodeData;
@@ -23,11 +27,11 @@
2327
*/
2428
class NodeDataRepository extends Repository
2529
{
26-
const ENTITY_CLASSNAME = NodeData::class;
30+
public const ENTITY_CLASSNAME = NodeData::class;
2731

2832
/**
2933
* @Flow\Inject
30-
* @var ObjectManager
34+
* @var EntityManagerInterface
3135
*/
3236
protected $entityManager;
3337

@@ -37,7 +41,7 @@ class NodeDataRepository extends Repository
3741
* @param integer $maxResults
3842
* @return IterableResult
3943
*/
40-
public function findAllBySiteAndWorkspace($workspaceName, $firstResult = 0, $maxResults = 1000)
44+
public function findAllBySiteAndWorkspace($workspaceName, $firstResult = 0, $maxResults = 1000): IterableResult
4145
{
4246
/** @var QueryBuilder $queryBuilder */
4347
$queryBuilder = $this->entityManager->createQueryBuilder();

Classes/Domain/Service/FakeNodeDataFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Service;
35

46
/*

Classes/Indexer/NodeIndexer.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Indexer;
35

46
/*
@@ -21,7 +23,7 @@
2123
use Neos\Flow\Persistence\PersistenceManagerInterface;
2224

2325
/**
24-
* Nodeindexer for use in batch jobs
26+
* NodeIndexer for use in batch jobs
2527
*/
2628
class NodeIndexer extends ContentRepositoryAdaptor\Indexer\NodeIndexer
2729
{
@@ -46,9 +48,9 @@ class NodeIndexer extends ContentRepositoryAdaptor\Indexer\NodeIndexer
4648
/**
4749
* @param NodeInterface $node
4850
* @param string|null $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
49-
* @throws \Neos\ContentRepository\Search\Exception\IndexingException
51+
* @throws ContentRepositoryAdaptor\Exception
5052
*/
51-
public function indexNode(NodeInterface $node, $targetWorkspaceName = null)
53+
public function indexNode(NodeInterface $node, $targetWorkspaceName = null): void
5254
{
5355
if( $node->isRemoved() ){
5456
$this->removeNode($node, $targetWorkspaceName);
@@ -77,8 +79,12 @@ public function indexNode(NodeInterface $node, $targetWorkspaceName = null)
7779
/**
7880
* @param NodeInterface $node
7981
* @param string|null $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
82+
* @throws ContentRepositoryAdaptor\Exception
83+
* @throws \Flowpack\ElasticSearch\Exception
84+
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
85+
* @throws \Neos\Utility\Exception\FilesException
8086
*/
81-
public function removeNode(NodeInterface $node, $targetWorkspaceName = null)
87+
public function removeNode(NodeInterface $node, string $targetWorkspaceName = null): void
8288
{
8389
if ($this->enableLiveAsyncIndexing !== true) {
8490
parent::removeNode($node, $targetWorkspaceName);
@@ -106,7 +112,7 @@ public function removeNode(NodeInterface $node, $targetWorkspaceName = null)
106112
* @param NodeInterface $node
107113
* @return array
108114
*/
109-
protected function nodeAsArray(NodeInterface $node)
115+
protected function nodeAsArray(NodeInterface $node): array
110116
{
111117
return [
112118
[

Classes/IndexingJob.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer;
35

46
/*
@@ -15,6 +17,7 @@
1517
use Flowpack\JobQueue\Common\Queue\QueueInterface;
1618
use Neos\ContentRepository\Domain\Model\NodeData;
1719
use Neos\ContentRepository\Domain\Model\NodeInterface;
20+
use Neos\Flow\Log\Utility\LogEnvironment;
1821

1922
/**
2023
* Elasticsearch Node Indexing Job
@@ -34,13 +37,14 @@ public function execute(QueueInterface $queue, Message $message): bool
3437
$this->nodeIndexer->withBulkProcessing(function () {
3538
$numberOfNodes = count($this->nodes);
3639
$startTime = microtime(true);
40+
3741
foreach ($this->nodes as $node) {
3842
/** @var NodeData $nodeData */
3943
$nodeData = $this->nodeDataRepository->findByIdentifier($node['persistenceObjectIdentifier']);
4044

4145
// Skip this iteration if the nodedata can not be fetched (deleted node)
4246
if (!$nodeData instanceof NodeData) {
43-
$this->log(sprintf('action=indexing step=skipped node=%s message="Node data could not be loaded"', $node['identifier']), \LOG_NOTICE);
47+
$this->logger->notice(sprintf('Node data of node %s could not be loaded. Node might be deleted."', $node['identifier']), LogEnvironment::fromMethodName(__METHOD__));
4448
continue;
4549
}
4650

@@ -54,7 +58,7 @@ public function execute(QueueInterface $queue, Message $message): bool
5458

5559
// Skip this iteration if the node can not be fetched from the current context
5660
if (!$currentNode instanceof NodeInterface) {
57-
$this->log(sprintf('action=indexing step=failed node=%s message="Node could not be processed"', $node['identifier']), \LOG_WARNING);
61+
$this->logger->warning(sprintf('Node %s could not be processed"', $node['identifier']), LogEnvironment::fromMethodName(__METHOD__));
5862
continue;
5963
}
6064

@@ -65,7 +69,7 @@ public function execute(QueueInterface $queue, Message $message): bool
6569
$this->nodeIndexer->flush();
6670
$duration = microtime(true) - $startTime;
6771
$rate = $numberOfNodes / $duration;
68-
$this->log(sprintf('action=indexing step=finished number_of_nodes=%d duration=%f nodes_per_second=%f', $numberOfNodes, $duration, $rate), \LOG_INFO);
72+
$this->logger->info(sprintf('Indexed %s nodes in %s seconds (%s nodes per second)', $numberOfNodes, $duration, $rate), LogEnvironment::fromMethodName(__METHOD__));
6973
});
7074

7175
return true;

Classes/LoggerTrait.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)