Skip to content

Commit fb96ecf

Browse files
committed
TASK: Refactor codebase / replace logger
- Replaced the logger trait with logger injections - Exchange parseable log messages with readable - Adjust interfaces - Add strict typing - Add additional type constraints
1 parent f31b0ee commit fb96ecf

9 files changed

Lines changed: 102 additions & 81 deletions

File tree

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: 34 additions & 13 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,11 +41,15 @@
3441
*/
3542
class NodeIndexQueueCommandController extends CommandController
3643
{
37-
use LoggerTrait;
38-
3944
const BATCH_QUEUE_NAME = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer';
4045
const LIVE_QUEUE_NAME = 'Flowpack.ElasticSearch.ContentRepositoryQueueIndexer.Live';
4146

47+
/**
48+
* @Flow\Inject
49+
* @var LoggerInterface
50+
*/
51+
protected $logger;
52+
4253
/**
4354
* @var JobManager
4455
* @Flow\Inject
@@ -91,11 +102,12 @@ 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 Exception
106+
* @throws StopActionException
96107
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
108+
* @throws StopCommandException
97109
*/
98-
public function buildCommand($workspace = null)
110+
public function buildCommand(string $workspace = null): void
99111
{
100112
$indexPostfix = time();
101113
$indexName = $this->createNextIndex($indexPostfix);
@@ -120,6 +132,7 @@ public function buildCommand($workspace = null)
120132
$this->outputLine();
121133
$this->indexWorkspace($workspace, $indexPostfix);
122134
}
135+
123136
$updateAliasJob = new UpdateAliasJob($indexPostfix);
124137
$this->jobManager->queue(self::BATCH_QUEUE_NAME, $updateAliasJob);
125138

@@ -134,9 +147,10 @@ public function buildCommand($workspace = null)
134147
* @param int $limit If set, only the given amount of jobs are processed (successful or not) before the script exits
135148
* @param bool $verbose Output debugging information
136149
* @return void
137-
* @throws \Neos\Flow\Mvc\Exception\StopActionException
150+
* @throws StopActionException
151+
* @throws StopCommandException
138152
*/
139-
public function workCommand($queue = 'batch', $exitAfter = null, $limit = null, $verbose = false)
153+
public function workCommand(string $queue = 'batch', int $exitAfter = null, int $limit = null, $verbose = false): void
140154
{
141155
$allowedQueues = [
142156
'batch' => self::BATCH_QUEUE_NAME,
@@ -201,7 +215,7 @@ public function workCommand($queue = 'batch', $exitAfter = null, $limit = null,
201215
/**
202216
* Flush the index queue
203217
*/
204-
public function flushCommand()
218+
public function flushCommand(): void
205219
{
206220
try {
207221
$this->queueManager->getQueue(self::BATCH_QUEUE_NAME)->flush();
@@ -235,8 +249,9 @@ protected function outputSystemReport()
235249
/**
236250
* @param string $workspaceName
237251
* @param string $indexPostfix
252+
* @throws \Exception
238253
*/
239-
protected function indexWorkspace($workspaceName, $indexPostfix)
254+
protected function indexWorkspace(string $workspaceName, string $indexPostfix): void
240255
{
241256
$this->outputLine('<info>++</info> Indexing %s workspace', [$workspaceName]);
242257
$nodeCounter = 0;
@@ -277,30 +292,36 @@ protected function indexWorkspace($workspaceName, $indexPostfix)
277292
* @param string $indexPostfix
278293
* @return string
279294
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
295+
* @throws ConfigurationException
296+
* @throws \Flowpack\ElasticSearch\Exception
297+
* @throws \Neos\Flow\Http\Exception
280298
*/
281-
protected function createNextIndex($indexPostfix)
299+
protected function createNextIndex(string $indexPostfix): string
282300
{
283301
$this->nodeIndexer->setIndexNamePostfix($indexPostfix);
284302
$this->nodeIndexer->getIndex()->create();
285-
$this->log(sprintf('action=indexing step=index-created index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO);
303+
$this->logger->info(sprintf('Index %s created', $this->nodeIndexer->getIndexName()), LogEnvironment::fromMethodName(__METHOD__));
286304

287305
return $this->nodeIndexer->getIndexName();
288306
}
289307

290308
/**
291309
* Update Index Mapping
292310
*
311+
* @param string $indexPostfix
293312
* @return void
313+
* @throws ConfigurationException
294314
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
315+
* @throws \Flowpack\ElasticSearch\Exception
295316
*/
296-
protected function updateMapping($indexPostfix)
317+
protected function updateMapping(string $indexPostfix): void
297318
{
298319
$nodeTypeMappingCollection = $this->nodeTypeMappingBuilder->buildMappingInformation($this->nodeIndexer->getIndex());
299320
foreach ($nodeTypeMappingCollection as $mapping) {
300321
$this->nodeIndexer->setIndexNamePostfix($indexPostfix);
301322
/** @var Mapping $mapping */
302323
$mapping->apply();
303324
}
304-
$this->log(sprintf('action=indexing step=mapping-updated index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO);
325+
$this->logger->info(sprintf('Mapping updated for index %s', $this->nodeIndexer->getIndexName()), LogEnvironment::fromMethodName(__METHOD__));
305326
}
306327
}

Classes/Domain/Repository/NodeDataRepository.php

Lines changed: 4 additions & 2 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
/*
@@ -23,7 +25,7 @@
2325
*/
2426
class NodeDataRepository extends Repository
2527
{
26-
const ENTITY_CLASSNAME = NodeData::class;
28+
public const ENTITY_CLASSNAME = NodeData::class;
2729

2830
/**
2931
* @Flow\Inject
@@ -37,7 +39,7 @@ class NodeDataRepository extends Repository
3739
* @param integer $maxResults
3840
* @return IterableResult
3941
*/
40-
public function findAllBySiteAndWorkspace($workspaceName, $firstResult = 0, $maxResults = 1000)
42+
public function findAllBySiteAndWorkspace($workspaceName, $firstResult = 0, $maxResults = 1000): IterableResult
4143
{
4244
/** @var QueryBuilder $queryBuilder */
4345
$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)