Skip to content

Commit fae03d3

Browse files
committed
!!! TASK: Refactor error handling
Bulkindexing errors are now written to files and references are written to the systemlog
1 parent 157790a commit fae03d3

7 files changed

Lines changed: 115 additions & 155 deletions

File tree

Classes/Command/NodeIndexCommandController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
18+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling\ErrorHandlingService;
1819
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
1920
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
20-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error\ErrorInterface;
2121
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
2222
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\WorkspaceIndexer;
23-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service\ErrorHandlingService;
2423
use Flowpack\ElasticSearch\Domain\Model\Mapping;
2524
use Flowpack\ElasticSearch\Transfer\Exception\ApiException;
2625
use Neos\ContentRepository\Domain\Model\Workspace;
@@ -141,6 +140,9 @@ class NodeIndexCommandController extends CommandController
141140
* @param string $workspace
142141
* @param string|null $postfix
143142
* @return void
143+
* @throws ConfigurationException
144+
* @throws FilesException
145+
* @throws StopCommandException
144146
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
145147
* @throws \Flowpack\ElasticSearch\Exception
146148
*/
@@ -199,6 +201,7 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
199201
}
200202

201203
$this->nodeIndexer->flush();
204+
$this->outputErrorHandling();
202205
}
203206

204207
/**
@@ -485,13 +488,7 @@ private function outputErrorHandling(): void
485488
}
486489

487490
$this->outputLine();
488-
/** @var ErrorInterface $error */
489-
foreach ($this->errorHandlingService as $error) {
490-
$this->outputLine('<error>Error</error> ' . $error->message());
491-
}
492-
493-
$this->outputLine();
494-
$this->outputLine('<error>Check your logs for more information</error>');
491+
$this->outputLine('<error>%s Errors where returned while indexing. Check your logs for more information.</error>', [$this->errorHandlingService->getErrorCount()]);
495492
}
496493

497494
/**

Classes/Driver/Version6/RequestDriver.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public function bulk(Index $index, $request): array
3838

3939
// Bulk request MUST end with line return
4040
$request = trim($request) . "\n";
41-
$response = $index->request('POST', '/_bulk', [], $request)->getOriginalResponse()->getBody()->getContents();
42-
43-
return array_map('json_decode', explode("\n", $response));
41+
return $index->request('POST', '/_bulk', [], $request)->getTreatedContent();
4442
}
4543
}
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Service;
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling;
55

66
/*
77
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
@@ -13,44 +13,50 @@
1313
* source code.
1414
*/
1515

16-
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error\ErrorInterface;
1716
use Neos\Flow\Annotations as Flow;
17+
use Psr\Log\LoggerInterface;
1818

1919
/**
2020
* Error Handling Service
2121
*
2222
* @Flow\Scope("singleton")
2323
*/
24-
class ErrorHandlingService implements \IteratorAggregate
24+
class ErrorHandlingService
2525
{
2626
/**
27-
* @var array
27+
* @Flow\Inject
28+
* @var LoggerInterface
2829
*/
29-
protected $errors = [];
30+
protected $logger;
3031

3132
/**
32-
* @param ErrorInterface $error
33-
* @return void
33+
* @var int
3434
*/
35-
public function log(ErrorInterface $error): void
35+
protected $errorCount = 0;
36+
37+
/**
38+
* @param string $message
39+
* @param $context
40+
*/
41+
public function log(string $message, $context): void
3642
{
37-
$this->errors[] = $error;
38-
$error->log();
43+
$this->errorCount++;
44+
$this->logger->error($message, $context);
3945
}
4046

4147
/**
42-
* @return bool
48+
* @return int
4349
*/
44-
public function hasError(): bool
50+
public function getErrorCount(): int
4551
{
46-
return count($this->errors) > 0;
52+
return $this->errorCount;
4753
}
4854

4955
/**
50-
* @return \ArrayIterator
56+
* @return bool
5157
*/
52-
public function getIterator()
58+
public function hasError(): bool
5359
{
54-
return new \ArrayIterator($this->errors);
60+
return $this->errorCount > 0;
5561
}
5662
}
Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
2-
32
declare(strict_types=1);
43

5-
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error;
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling;
65

76
/*
87
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
@@ -14,22 +13,13 @@
1413
* source code.
1514
*/
1615

17-
/**
18-
* Error Interface
19-
*/
20-
interface ErrorInterface
16+
interface ErrorStorageInterface
2117
{
2218
/**
23-
* Log the error message
24-
*
25-
* @return void
26-
*/
27-
public function log(): void;
28-
29-
/**
30-
* Get a short log message for reporting
19+
* Write the error message and return a short info for the log
3120
*
32-
* @return string
21+
* @param array $errorResult
22+
* @return string Information about the logged Elasticsearch Error
3323
*/
34-
public function message(): string;
24+
public function logErrorResult(array $errorResult): string;
3525
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling;
5+
6+
/*
7+
* This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package.
8+
*
9+
* (c) Contributors of the Neos Project - www.neos.io
10+
*
11+
* This package is Open Source Software. For the full copyright and license
12+
* information, please view the LICENSE file which was distributed with this
13+
* source code.
14+
*/
15+
16+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
17+
18+
/**
19+
* Handle error result and build human readable output for analysis
20+
*/
21+
class FileStorage implements ErrorStorageInterface
22+
{
23+
24+
public function __construct()
25+
{
26+
if (!file_exists(FLOW_PATH_DATA . 'Logs/Elasticsearch')) {
27+
mkdir(FLOW_PATH_DATA . 'Logs/Elasticsearch');
28+
}
29+
}
30+
31+
/**
32+
* Log the error message
33+
*
34+
* @param array $errorResult
35+
* @return string
36+
* @throws RuntimeException
37+
*/
38+
public function logErrorResult(array $errorResult): string
39+
{
40+
41+
$referenceCode = date('YmdHis', $_SERVER['REQUEST_TIME']) . substr(md5((string)rand()), 0, 6);
42+
43+
$filename = FLOW_PATH_DATA . 'Logs/Elasticsearch/' . $referenceCode . '.txt';
44+
$message = sprintf('Elasticsearch API Error detected - See also: Data/Logs/Elasticsearch/%s on host: %s', basename($filename), gethostname());
45+
46+
if (file_exists(FLOW_PATH_DATA . 'Logs/Elasticsearch') && is_dir(FLOW_PATH_DATA . 'Logs/Elasticsearch') && is_writable(FLOW_PATH_DATA . 'Logs/Elasticsearch')) {
47+
file_put_contents($filename, $this->renderErrorResult($errorResult));
48+
} else {
49+
throw new RuntimeException('Elasticsearch error response could not be written to ' . $filename, 1588835331);
50+
}
51+
52+
return $message;
53+
}
54+
55+
/**
56+
* @param array $errorResult
57+
* @return string
58+
*/
59+
protected function renderErrorResult(array $errorResult): string
60+
{
61+
$error = json_encode($errorResult, JSON_PRETTY_PRINT);
62+
return sprintf("Error:\n=======\n\n%s\n\n", $error);
63+
}
64+
65+
}

Classes/Indexer/Error/BulkIndexingError.php

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

0 commit comments

Comments
 (0)