|
| 1 | +<?php |
| 2 | +namespace Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\Error; |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the Flowpack.ElasticSearch.ContentRepositoryAdaptor package. |
| 6 | + * |
| 7 | + * (c) Contributors of the Neos Project - www.neos.io |
| 8 | + * |
| 9 | + * This package is Open Source Software. For the full copyright and license |
| 10 | + * information, please view the LICENSE file which was distributed with this |
| 11 | + * source code. |
| 12 | + */ |
| 13 | + |
| 14 | +use Flowpack\ElasticSearch\ContentRepositoryAdaptor\LoggerInterface; |
| 15 | +use Neos\Flow\Annotations as Flow; |
| 16 | + |
| 17 | +/** |
| 18 | + * Handle Bulk Indexing Error and build human readable output for analysis |
| 19 | + */ |
| 20 | +class BulkIndexingError implements ErrorInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @Flow\Inject |
| 24 | + * @var LoggerInterface |
| 25 | + */ |
| 26 | + protected $logger; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $message; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $filename; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var array |
| 40 | + */ |
| 41 | + protected $currentBulkRequest; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var array |
| 45 | + */ |
| 46 | + protected $errors; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param array $currentBulkRequest |
| 50 | + * @param array $errors |
| 51 | + */ |
| 52 | + public function __construct(array $currentBulkRequest, array $errors) |
| 53 | + { |
| 54 | + $this->currentBulkRequest = $currentBulkRequest; |
| 55 | + $this->errors = json_decode($errors, true); |
| 56 | + |
| 57 | + if (!file_exists(FLOW_PATH_DATA . 'Logs/Elasticsearch')) { |
| 58 | + mkdir(FLOW_PATH_DATA . 'Logs/Elasticsearch'); |
| 59 | + } |
| 60 | + |
| 61 | + $referenceCode = date('YmdHis', $_SERVER['REQUEST_TIME']) . substr(md5(rand()), 0, 6); |
| 62 | + |
| 63 | + $this->filename = FLOW_PATH_DATA . 'Logs/Elasticsearch/' . $referenceCode . '.txt'; |
| 64 | + $this->message = sprintf('Bulk indexing errors detected - See also: Data/Logs/Elasticsearch/%s on host: %s', basename($this->filename), gethostname()); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Log the error message |
| 69 | + * |
| 70 | + * @return void |
| 71 | + */ |
| 72 | + public function log() |
| 73 | + { |
| 74 | + if (file_exists(FLOW_PATH_DATA . 'Logs/Elasticsearch') && is_dir(FLOW_PATH_DATA . 'Logs/Elasticsearch') && is_writable(FLOW_PATH_DATA . 'Logs/Elasticsearch')) { |
| 75 | + file_put_contents($this->filename, $this->renderErrors()); |
| 76 | + $this->logger->log($this->message, LOG_ERR, [], 'Flowpack.ElasticSearch.ContentRepositoryAdaptor', __CLASS__, __FUNCTION__); |
| 77 | + } else { |
| 78 | + $this->logger->log(sprintf('Could not write indexing errors backtrace into %s because the directory could not be created or is not writable.', FLOW_PATH_DATA . 'Logs/Elasticsearch/'), LOG_WARNING, [], 'Flowpack.ElasticSearch.ContentRepositoryAdaptor', __CLASS__, __FUNCTION__); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + public function message() |
| 86 | + { |
| 87 | + return $this->message; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * @return string |
| 93 | + */ |
| 94 | + protected function renderErrors() |
| 95 | + { |
| 96 | + $bulkRequest = json_encode($this->currentBulkRequest, JSON_PRETTY_PRINT); |
| 97 | + $errors = json_encode($this->errors, JSON_PRETTY_PRINT); |
| 98 | + return sprintf("Payload:\n========\n\n%s\n\nErrors:\n=======\n\n%s\n\n", $bulkRequest, $errors); |
| 99 | + } |
| 100 | +} |
0 commit comments