Skip to content

Commit 9dc8332

Browse files
committed
TASK: Some code cleanup
Remove some unused imports, adjust docblocks and tweak exception handling a bit.
1 parent e3195b0 commit 9dc8332

8 files changed

Lines changed: 56 additions & 39 deletions

File tree

Classes/AbstractIndexingJob.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Repository\NodeDataRepository;
66
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Service\FakeNodeDataFactory;
77
use Flowpack\JobQueue\Common\Job\JobInterface;
8-
use Flowpack\JobQueue\Common\Queue\Message;
9-
use Flowpack\JobQueue\Common\Queue\QueueInterface;
10-
use Neos\ContentRepository\Domain\Model\NodeData;
11-
use Neos\Flow\Annotations as Flow;
12-
use Neos\Flow\Utility\Algorithms;
138
use Neos\ContentRepository\Domain\Factory\NodeFactory;
14-
use Neos\ContentRepository\Domain\Model\NodeInterface;
159
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
10+
use Neos\Flow\Annotations as Flow;
11+
use Neos\Flow\Utility\Algorithms;
1612

1713
/**
1814
* Elasticsearch Node Abstract Job

Classes/Command/NodeIndexQueueCommandController.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\IndexingJob;
88
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\LoggerTrait;
99
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\UpdateAliasJob;
10+
use Flowpack\ElasticSearch\Domain\Model\Mapping;
11+
use Flowpack\JobQueue\Common\Exception;
1012
use Flowpack\JobQueue\Common\Job\JobManager;
1113
use Flowpack\JobQueue\Common\Queue\QueueManager;
14+
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
1215
use Neos\Flow\Annotations as Flow;
1316
use Neos\Flow\Cli\CommandController;
1417
use Neos\Flow\Persistence\PersistenceManagerInterface;
15-
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
1618
use Neos\Utility\Files;
1719

1820
/**
1921
* Provides CLI features for index handling
20-
*
2122
* @Flow\Scope("singleton")
2223
*/
2324
class NodeIndexQueueCommandController extends CommandController
@@ -73,14 +74,15 @@ class NodeIndexQueueCommandController extends CommandController
7374
* Index all nodes by creating a new index and when everything was completed, switch the index alias.
7475
*
7576
* @param string $workspace
77+
* @throws \Flowpack\JobQueue\Common\Exception
78+
* @throws \Neos\Flow\Mvc\Exception\StopActionException
7679
*/
7780
public function buildCommand($workspace = null)
7881
{
7982
$indexPostfix = time();
8083
$indexName = $this->createNextIndex($indexPostfix);
8184
$this->updateMapping();
8285

83-
8486
$this->outputLine();
8587
$this->outputLine('<b>Indexing on %s ...</b>', [$indexName]);
8688

@@ -114,6 +116,7 @@ public function buildCommand($workspace = null)
114116
* @param int $limit If set, only the given amount of jobs are processed (successful or not) before the script exits
115117
* @param bool $verbose Output debugging information
116118
* @return void
119+
* @throws \Neos\Flow\Mvc\Exception\StopActionException
117120
*/
118121
public function workCommand($queue = 'batch', $exitAfter = null, $limit = null, $verbose = false)
119122
{
@@ -145,8 +148,8 @@ public function workCommand($queue = 'batch', $exitAfter = null, $limit = null,
145148
}
146149
try {
147150
$message = $this->jobManager->waitAndExecute($queueName, $timeout);
148-
} catch (JobQueueException $exception) {
149-
$numberOfJobExecutions ++;
151+
} catch (Exception $exception) {
152+
$numberOfJobExecutions++;
150153
$this->outputLine('<error>%s</error>', [$exception->getMessage()]);
151154
if ($verbose && $exception->getPrevious() instanceof \Exception) {
152155
$this->outputLine(' Reason: %s', [$exception->getPrevious()->getMessage()]);
@@ -156,7 +159,7 @@ public function workCommand($queue = 'batch', $exitAfter = null, $limit = null,
156159
$this->quit(1);
157160
}
158161
if ($message !== null) {
159-
$numberOfJobExecutions ++;
162+
$numberOfJobExecutions++;
160163
if ($verbose) {
161164
$messagePayload = strlen($message->getPayload()) <= 50 ? $message->getPayload() : substr($message->getPayload(), 0, 50) . '...';
162165
$this->outputLine('<success>Successfully executed job "%s" (%s)</success>', [$message->getIdentifier(), $messagePayload]);
@@ -174,7 +177,6 @@ public function workCommand($queue = 'batch', $exitAfter = null, $limit = null,
174177
}
175178
$this->quit();
176179
}
177-
178180
} while (true);
179181
}
180182

@@ -183,8 +185,12 @@ public function workCommand($queue = 'batch', $exitAfter = null, $limit = null,
183185
*/
184186
public function flushCommand()
185187
{
186-
$this->queueManager->getQueue(self::BATCH_QUEUE_NAME)->flush();
187-
$this->outputSystemReport();
188+
try {
189+
$this->queueManager->getQueue(self::BATCH_QUEUE_NAME)->flush();
190+
$this->outputSystemReport();
191+
} catch (Exception $exception) {
192+
$this->outputLine('An error occurred: %s', [$exception->getMessage()]);
193+
}
188194
$this->outputLine();
189195
}
190196

@@ -198,7 +204,11 @@ protected function outputSystemReport()
198204
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
199205
$this->outputLine('Execution time : %s seconds', [$time]);
200206
$this->outputLine('Indexing Queue : %s', [self::BATCH_QUEUE_NAME]);
201-
$this->outputLine('Pending Jobs : %s', [$this->queueManager->getQueue(self::BATCH_QUEUE_NAME)->count()]);
207+
try {
208+
$this->outputLine('Pending Jobs : %s', [$this->queueManager->getQueue(self::BATCH_QUEUE_NAME)->count()]);
209+
} catch (Exception $exception) {
210+
$this->outputLine('Pending Jobs : Error, queue not found, %s', [$exception->getMessage()]);
211+
}
202212
}
203213

204214
/**
@@ -251,6 +261,7 @@ protected function createNextIndex($indexPostfix)
251261
$this->nodeIndexer->setIndexNamePostfix($indexPostfix);
252262
$this->nodeIndexer->getIndex()->create();
253263
$this->log(sprintf('action=indexing step=index-created index=%s', $this->nodeIndexer->getIndexName()), LOG_INFO);
264+
254265
return $this->nodeIndexer->getIndexName();
255266
}
256267

Classes/Domain/Repository/NodeDataRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Doctrine\Common\Persistence\ObjectManager;
55
use Doctrine\ORM\Internal\Hydration\IterableResult;
66
use Doctrine\ORM\QueryBuilder;
7+
use Neos\ContentRepository\Domain\Model\NodeData;
78
use Neos\Flow\Annotations as Flow;
89
use Neos\Flow\Persistence\Repository;
910

@@ -12,7 +13,7 @@
1213
*/
1314
class NodeDataRepository extends Repository
1415
{
15-
const ENTITY_CLASSNAME = 'Neos\ContentRepository\Domain\Model\NodeData';
16+
const ENTITY_CLASSNAME = NodeData::class;
1617

1718
/**
1819
* @Flow\Inject
@@ -28,12 +29,11 @@ class NodeDataRepository extends Repository
2829
*/
2930
public function findAllBySiteAndWorkspace($workspaceName, $firstResult = 0, $maxResults = 1000)
3031
{
31-
3232
/** @var QueryBuilder $queryBuilder */
3333
$queryBuilder = $this->entityManager->createQueryBuilder();
3434

3535
$queryBuilder->select('n.Persistence_Object_Identifier nodeIdentifier, n.dimensionValues dimensions, n.nodeType nodeType, n.path path')
36-
->from('Neos\ContentRepository\Domain\Model\NodeData', 'n')
36+
->from(NodeData::class, 'n')
3737
->where("n.workspace = :workspace AND n.removed = :removed AND n.movedTo IS NULL")
3838
->setFirstResult((integer)$firstResult)
3939
->setMaxResults((integer)$maxResults)
@@ -64,7 +64,7 @@ public function iterate(IterableResult $iterator, callable $callback = null)
6464
if ($callback !== null) {
6565
call_user_func($callback, $iteration, $object);
6666
}
67-
++$iteration;
67+
$iteration++;
6868
}
6969
}
7070
}

Classes/Domain/Service/FakeNodeDataFactory.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Neos\ContentRepository\Domain\Model\NodeData;
66
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
77
use Neos\ContentRepository\Domain\Service\NodeTypeManager;
8+
use Neos\ContentRepository\Exception\NodeTypeNotFoundException;
89
use Neos\Flow\Annotations as Flow;
910

1011
/**
@@ -24,28 +25,39 @@ class FakeNodeDataFactory
2425
*/
2526
protected $nodeTypeManager;
2627

27-
public function createFromPayload(array $data)
28+
/**
29+
* Thie creates a NodeData instance from the given payload
30+
*
31+
* @param array $payload
32+
* @return NodeData
33+
* @throws Exception
34+
*/
35+
public function createFromPayload(array $payload)
2836
{
29-
if (!isset($data['workspace']) || empty($data['workspace'])) {
37+
if (!isset($payload['workspace']) || empty($payload['workspace'])) {
3038
throw new Exception('Unable to create fake node data, missing workspace value', 1508448007);
3139
}
32-
if (!isset($data['path']) || empty($data['path'])) {
40+
if (!isset($payload['path']) || empty($payload['path'])) {
3341
throw new Exception('Unable to create fake node data, missing path value', 1508448008);
3442
}
35-
if (!isset($data['nodeIdentifier']) || empty($data['nodeIdentifier'])) {
43+
if (!isset($payload['nodeIdentifier']) || empty($payload['nodeIdentifier'])) {
3644
throw new Exception('Unable to create fake node data, missing identifier value', 1508448009);
3745
}
38-
if (!isset($data['nodeType']) || empty($data['nodeType'])) {
46+
if (!isset($payload['nodeType']) || empty($payload['nodeType'])) {
3947
throw new Exception('Unable to create fake node data, missing nodeType value', 1508448011);
4048
}
4149

42-
$workspace = $this->workspaceRepository->findOneByName($data['workspace']);
50+
$workspace = $this->workspaceRepository->findOneByName($payload['workspace']);
4351
if ($workspace === null) {
4452
throw new Exception('Unable to create fake node data, workspace not found', 1508448028);
4553
}
4654

47-
$nodeData = new NodeData($data['path'], $workspace, $data['nodeIdentifier'], isset($data['dimensions']) ? $data['dimensions'] : null);
48-
$nodeData->setNodeType($this->nodeTypeManager->getNodeType($data['nodeType']));
55+
$nodeData = new NodeData($payload['path'], $workspace, $payload['nodeIdentifier'], isset($payload['dimensions']) ? $payload['dimensions'] : null);
56+
try {
57+
$nodeData->setNodeType($this->nodeTypeManager->getNodeType($payload['nodeType']));
58+
} catch (NodeTypeNotFoundException $e) {
59+
throw new Exception('Unable to create fake node data, node type not found', 1509362172);
60+
}
4961

5062
$nodeData->setProperty('title', 'Fake node');
5163
$nodeData->setProperty('uriPathSegment', 'fake-node');

Classes/Indexer/NodeIndexer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\IndexingJob;
77
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\RemovalJob;
88
use Flowpack\JobQueue\Common\Job\JobManager;
9+
use Neos\ContentRepository\Domain\Model\NodeInterface;
910
use Neos\Flow\Annotations as Flow;
1011
use Neos\Flow\Persistence\PersistenceManagerInterface;
11-
use Neos\ContentRepository\Domain\Model\NodeInterface;
1212

1313
/**
1414
* ElasticSearch Indexing Job Interface
@@ -36,11 +36,13 @@ class NodeIndexer extends ContentRepositoryAdaptor\Indexer\NodeIndexer
3636
/**
3737
* @param NodeInterface $node
3838
* @param string|null $targetWorkspaceName In case indexing is triggered during publishing, a target workspace name will be passed in
39+
* @throws \Neos\ContentRepository\Search\Exception\IndexingException
3940
*/
4041
public function indexNode(NodeInterface $node, $targetWorkspaceName = null)
4142
{
4243
if ($this->enableLiveAsyncIndexing !== true) {
4344
parent::indexNode($node, $targetWorkspaceName);
45+
4446
return;
4547
}
4648
$indexingJob = new IndexingJob($this->indexNamePostfix, $targetWorkspaceName, [
@@ -63,6 +65,7 @@ public function removeNode(NodeInterface $node, $targetWorkspaceName = null)
6365
{
6466
if ($this->enableLiveAsyncIndexing !== true) {
6567
parent::removeNode($node, $targetWorkspaceName);
68+
6669
return;
6770
}
6871
$removalJob = new RemovalJob($this->indexNamePostfix, $targetWorkspaceName, [

Classes/IndexingJob.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<?php
22
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer;
33

4-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
54
use Flowpack\JobQueue\Common\Queue\Message;
65
use Flowpack\JobQueue\Common\Queue\QueueInterface;
76
use Neos\ContentRepository\Domain\Model\NodeData;
8-
use Neos\Flow\Annotations as Flow;
97
use Neos\ContentRepository\Domain\Model\NodeInterface;
108

119
/**
@@ -19,6 +17,7 @@ class IndexingJob extends AbstractIndexingJob
1917
* @param QueueInterface $queue
2018
* @param Message $message The original message
2119
* @return boolean TRUE if the job was executed successfully and the message should be finished
20+
* @throws \Exception
2221
*/
2322
public function execute(QueueInterface $queue, Message $message)
2423
{

Classes/RemovalJob.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@
22
namespace Flowpack\ElasticSearch\ContentRepositoryQueueIndexer;
33

44
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
5-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
6-
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Repository\NodeDataRepository;
7-
use Flowpack\ElasticSearch\ContentRepositoryQueueIndexer\Domain\Service\FakeNodeDataFactory;
8-
use Flowpack\JobQueue\Common\Job\JobInterface;
95
use Flowpack\JobQueue\Common\Queue\Message;
106
use Flowpack\JobQueue\Common\Queue\QueueInterface;
117
use Neos\ContentRepository\Domain\Model\NodeData;
12-
use Neos\Flow\Annotations as Flow;
13-
use Neos\Flow\Utility\Algorithms;
14-
use Neos\ContentRepository\Domain\Factory\NodeFactory;
158
use Neos\ContentRepository\Domain\Model\NodeInterface;
16-
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
179

1810
/**
1911
* Elasticsearch Node Removal Job
@@ -26,6 +18,7 @@ class RemovalJob extends AbstractIndexingJob
2618
* @param QueueInterface $queue
2719
* @param Message $message The original message
2820
* @return boolean TRUE if the job was executed successfully and the message should be finished
21+
* @throws \Exception
2922
*/
3023
public function execute(QueueInterface $queue, Message $message)
3124
{

Classes/UpdateAliasJob.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function __construct($indexPostfix)
4747
* @param QueueInterface $queue
4848
* @param Message $message The original message
4949
* @return boolean TRUE if the job was executed successfully and the message should be finished
50+
* @throws \Exception
51+
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
52+
* @throws \Flowpack\ElasticSearch\Transfer\Exception\ApiException
5053
*/
5154
public function execute(QueueInterface $queue, Message $message)
5255
{

0 commit comments

Comments
 (0)