Passed
Pull Request — release-11.2.x (#3528)
by Rafael
09:36
created

EventQueueWorkerTask::getSolrLogManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Task;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2021 Markus Friedrich <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Psr\EventDispatcher\EventDispatcherInterface;
29
use TYPO3\CMS\Scheduler\Task\AbstractTask;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
32
use ApacheSolrForTypo3\Solr\System\Records\Queue\EventQueueItemRepository;
33
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\UpdateHandler\Events\DataUpdateEventInterface;
34
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
35
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\UpdateHandler\EventListener\Events\DelayedProcessingFinishedEvent;
36
37
/**
38
 * A worker processing the queued data update events
39
 *
40
 * @author Markus Friedrich <[email protected]>
41
 */
42
final class EventQueueWorkerTask extends AbstractTask
43
{
44
    const DEFAULT_PROCESSING_LIMIT = 100;
45
46
    /**
47
     * Processing limit, the number of events to process
48
     *
49
     * @var int
50
     */
51
    protected $limit = self::DEFAULT_PROCESSING_LIMIT;
52
53
    /**
54
     * Works through the indexing queue and indexes the queued items into Solr.
55
     *
56
     * @return bool Returns TRUE on success, FALSE if no items were indexed or none were found.
57
     */
58 33
    public function execute(): bool
59
    {
60 33
       $this->processEvents();
61 33
       return true;
62
    }
63
64
    /**
65
     * Process queued data update events
66
     */
67 33
    protected function processEvents(): void
68
    {
69 33
        $itemRepository = $this->getEventQueueItemRepository();
70 33
        $dispatcher = $this->getEventDispatcher();
71
72 33
        $queueItems = $itemRepository->getEventQueueItems($this->limit);
73 33
        $processedItems = [];
74 33
        foreach ($queueItems as $queueItem) {
75
            try {
76 33
                $event = unserialize($queueItem['event']);
77 33
                if (!$event instanceof DataUpdateEventInterface) {
78 1
                    throw new \InvalidArgumentException(
79
                        'Unsupported event found: '
80 1
                            . (is_object($event) ? get_class($event) : (string)$event),
81 1
                        1639747163
82
                    );
83
                }
84
85 32
                $event->setForceImmediateProcessing(true);
86 32
                $dispatcher->dispatch($event);
87 31
                $processedItems[] = $queueItem['uid'];
88
89
                // dispatch event processing finished event
90 31
                $dispatcher->dispatch(
91 31
                    new DelayedProcessingFinishedEvent($event)
92
                );
93 2
            } catch (\Throwable $e) {
94 2
                $this->getSolrLogManager()->log(
95 2
                    SolrLogManager::ERROR,
96 2
                    'Couldn\'t process queued event',
97
                    [
98 2
                        'eventQueueItemUid' => $queueItem['uid'],
99 2
                        'error' => $e->getMessage(),
100 2
                        'errorCode' => $e->getCode(),
101 2
                        'errorFile' => $e->getFile() . ':' . $e->getLine()
102
                    ]
103
                );
104 2
                $itemRepository->updateEventQueueItem(
105 2
                    $queueItem['uid'],
106
                    [
107 2
                        'error' => 1,
108 2
                        'error_message' => $e->getMessage() . '[' . $e->getCode() . ']'
109
                    ]
110
                );
111
            }
112
        }
113
114
        // update event queue
115 33
        $itemRepository->deleteEventQueueItems($processedItems);
116 33
    }
117
118
    /**
119
     * Returns some additional information about indexing progress, shown in
120
     * the scheduler's task overview list.
121
     *
122
     * @return string Information to display
123
     */
124
    public function getAdditionalInformation(): string
125
    {
126
        $message = LocalizationUtility::translate(
127
            'LLL:EXT:solr/Resources/Private/Language/locallang_be.xlf:task.eventQueueWorkerTask.statusMsg'
128
        );
129
130
        $fullItemCount = $this->getEventQueueItemRepository()->count(false);
131
        $pendingItemsCount = $this->getEventQueueItemRepository()->count();
132
        return sprintf(
133
            $message,
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type null; however, parameter $format of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            /** @scrutinizer ignore-type */ $message,
Loading history...
134
            $pendingItemsCount,
135
            ($fullItemCount - $pendingItemsCount),
136
            $this->limit
137
        );
138
    }
139
140
    /**
141
     * Sets the limit
142
     *
143
     * @param int $limit
144
     */
145 2
    public function setLimit(int $limit): void
146
    {
147 2
        $this->limit = $limit;
148 2
    }
149
150
    /**
151
     * Returns the limit
152
     *
153
     * @param int $limit
154
     */
155
    public function getLimit(): int
156
    {
157
        return $this->limit;
158
    }
159
160
    /**
161
     * Return the SolrLogManager
162
     *
163
     * @return SolrLogManager
164
     */
165 2
    protected function getSolrLogManager(): SolrLogManager
166
    {
167 2
        return  GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
168
    }
169
170
    /**
171
     * Return the EventQueueItemRepository
172
     *
173
     * @return EventQueueItemRepository
174
     */
175 33
    protected function getEventQueueItemRepository(): EventQueueItemRepository
176
    {
177 33
        return GeneralUtility::makeInstance(EventQueueItemRepository::class);
178
    }
179
180
    /**
181
     * Returns the EventDispatcher
182
     *
183
     * @return EventDispatcherInterface
184
     */
185 33
    protected function getEventDispatcher(): EventDispatcherInterface
186
    {
187 33
        return GeneralUtility::makeInstance(EventDispatcherInterface::class);
188
    }
189
}
190