Passed
Push — deprecate/getLogEntriesForPage... ( 209510...c5a454 )
by Tomas Norre
09:05 queued 05:28
created

ProcessService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Service;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2020 AOE GmbH <[email protected]>
11
 *
12
 *  All rights reserved
13
 *
14
 *  This script is part of the TYPO3 project. The TYPO3 project is
15
 *  free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 3 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  The GNU General Public License can be found at
21
 *  http://www.gnu.org/copyleft/gpl.html.
22
 *
23
 *  This script is distributed in the hope that it will be useful,
24
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 *  GNU General Public License for more details.
27
 *
28
 *  This copyright notice MUST APPEAR in all copies of the script!
29
 ***************************************************************/
30
31
use AOE\Crawler\Configuration\ExtensionConfigurationProvider;
32
use AOE\Crawler\Controller\CrawlerController;
33
use AOE\Crawler\Domain\Repository\ProcessRepository;
34
use AOE\Crawler\Domain\Repository\QueueRepository;
35
use AOE\Crawler\Exception\ProcessException;
36
use AOE\Crawler\Utility\PhpBinaryUtility;
37
use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
38
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
39
use TYPO3\CMS\Core\Core\Environment;
40
use TYPO3\CMS\Core\Utility\CommandUtility;
41
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
42
use TYPO3\CMS\Core\Utility\GeneralUtility;
43
use TYPO3\CMS\Extbase\Object\ObjectManager;
44
45
/**
46
 * Class ProcessService
47
 *
48
 * @package AOE\Crawler\Service
49
 * @ignoreAnnotation("noRector")
50
 */
51
class ProcessService
52
{
53
    use PublicMethodDeprecationTrait;
54
    use PublicPropertyDeprecationTrait;
55
56
    /**
57
     * @var string[]
58
     * @noRector
59
     * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector
60
     * @noRector \Rector\DeadCode\Rector\Property\RemoveSetterOnlyPropertyAndMethodCallRector
61
     */
62
    private $deprecatedPublicProperties = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicProperties is not used, and could be removed.
Loading history...
63
        'queueRepository' => 'Using queueRepository is deprecated since 9.0.1 and will be removed in v11.x',
64
        'crawlerController' => 'Using crawlerController is deprecated since 9.0.1 and will be removed in v11.x',
65
        'countInARun' => 'Using countInARun is deprecated since 9.0.1 and will be removed in v11.x',
66
        'processLimit' => 'Using processLimit is deprecated since 9.0.1 and will be removed in v11.x',
67
        'verbose' => 'Using verbose is deprecated since 9.0.1 and will be removed in v11.x',
68
    ];
69
70
    /**
71
     * @var string[]
72
     * @noRector
73
     * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector
74
     * @noRector \Rector\DeadCode\Rector\Property\RemoveSetterOnlyPropertyAndMethodCallRector
75
     */
76
    private $deprecatedPublicMethods = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicMethods is not used, and could be removed.
Loading history...
77
        'multiProcess' => 'Using ProcessService::multiProcess() is deprecated since 9.0.1 and will be removed in v11.x',
78
        'reportItemStatus' => 'Using ProcessService::reportItemStatus() is deprecated since 9.0.1 and will be removed in v11.x',
79
        'startRequiredProcesses' => 'Using ProcessService::startRequiredProcesses() is deprecated since 9.0.1 and will be removed in v11.x',
80
    ];
81
82
    /**
83
     * @var int
84
     */
85
    private $timeToLive;
86
87
    /**
88
     * @var int
89
     * @deprecated
90
     */
91
    private $countInARun;
92
93
    /**
94
     * @var int
95
     * @deprecated
96
     */
97
    private $processLimit;
98
99
    /**
100
     * @var CrawlerController
101
     * @deprecated
102
     */
103
    private $crawlerController;
104
105
    /**
106
     * @var \AOE\Crawler\Domain\Repository\QueueRepository
107
     * @deprecated
108
     */
109
    private $queueRepository;
110
111
    /**
112
     * @var \AOE\Crawler\Domain\Repository\ProcessRepository
113
     */
114
    private $processRepository;
115
116
    /**
117
     * @var array
118
     */
119
    private $extensionSettings;
120
121
    /**
122
     * @var bool
123
     * @deprecated
124
     */
125
    private $verbose;
126
127
    /**
128
     * the constructor
129
     */
130 26
    public function __construct()
131
    {
132 26
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
133 26
        $this->processRepository = $objectManager->get(ProcessRepository::class);
134 26
        $this->queueRepository = $objectManager->get(QueueRepository::class);
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\Proc...rvice::$queueRepository has been deprecated. ( Ignorable by Annotation )

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

134
        /** @scrutinizer ignore-deprecated */ $this->queueRepository = $objectManager->get(QueueRepository::class);
Loading history...
135 26
        $this->extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
136 26
        $this->timeToLive = (int) $this->extensionSettings['processMaxRunTime'];
137 26
        $this->countInARun = (int) $this->extensionSettings['countInARun'];
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$countInARun has been deprecated. ( Ignorable by Annotation )

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

137
        /** @scrutinizer ignore-deprecated */ $this->countInARun = (int) $this->extensionSettings['countInARun'];
Loading history...
138 26
        $this->processLimit = (int) $this->extensionSettings['processLimit'];
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$processLimit has been deprecated. ( Ignorable by Annotation )

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

138
        /** @scrutinizer ignore-deprecated */ $this->processLimit = (int) $this->extensionSettings['processLimit'];
Loading history...
139 26
        $this->verbose = (bool) $this->extensionSettings['processVerbose'];
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

139
        /** @scrutinizer ignore-deprecated */ $this->verbose = (bool) $this->extensionSettings['processVerbose'];
Loading history...
140 26
    }
141
142
    /**
143
     * starts multiple processes
144
     *
145
     * @param integer $timeout
146
     *
147
     * @throws \RuntimeException
148
     * @deprecated
149
     * @codeCoverageIgnore
150
     */
151
    public function multiProcess($timeout): void
152
    {
153
        if ($this->processLimit <= 1) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$processLimit has been deprecated. ( Ignorable by Annotation )

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

153
        if (/** @scrutinizer ignore-deprecated */ $this->processLimit <= 1) {
Loading history...
154
            throw new \RuntimeException('To run crawler in multi process mode you have to configure the processLimit > 1.' . PHP_EOL);
155
        }
156
157
        $pendingItemsStart = $this->queueRepository->countAllPendingItems();
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\Proc...rvice::$queueRepository has been deprecated. ( Ignorable by Annotation )

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

157
        $pendingItemsStart = /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllPendingItems();
Loading history...
158
        $itemReportLimit = 20;
159
        $reportItemCount = $pendingItemsStart - $itemReportLimit;
160
        if ($this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

160
        if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
161
            $this->reportItemStatus();
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Service\Proc...ice::reportItemStatus() has been deprecated. ( Ignorable by Annotation )

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

161
            /** @scrutinizer ignore-deprecated */ $this->reportItemStatus();
Loading history...
162
        }
163
        $this->startRequiredProcesses();
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Service\Proc...tartRequiredProcesses() has been deprecated. ( Ignorable by Annotation )

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

163
        /** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses();
Loading history...
164
        $nextTimeOut = time() + $this->timeToLive;
165
        $currentPendingItems = '';
166
        for ($i = 0; $i < $timeout; $i++) {
167
            $currentPendingItems = $this->queueRepository->countAllPendingItems();
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\Proc...rvice::$queueRepository has been deprecated. ( Ignorable by Annotation )

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

167
            $currentPendingItems = /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllPendingItems();
Loading history...
168
            if ($this->startRequiredProcesses()) {
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Service\Proc...tartRequiredProcesses() has been deprecated. ( Ignorable by Annotation )

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

168
            if (/** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses()) {
Loading history...
169
                $nextTimeOut = time() + $this->timeToLive;
170
            }
171
            if ($currentPendingItems === 0) {
172
                if ($this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

172
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
173
                    echo 'Finished...' . chr(10);
174
                }
175
                break;
176
            }
177
            if ($currentPendingItems < $reportItemCount) {
178
                if ($this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

178
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
179
                    $this->reportItemStatus();
0 ignored issues
show
Deprecated Code introduced by
The function AOE\Crawler\Service\Proc...ice::reportItemStatus() has been deprecated. ( Ignorable by Annotation )

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

179
                    /** @scrutinizer ignore-deprecated */ $this->reportItemStatus();
Loading history...
180
                }
181
                $reportItemCount = $currentPendingItems - $itemReportLimit;
182
            }
183
            sleep(1);
184
            if ($nextTimeOut < time()) {
185
                $timedOutProcesses = $this->processRepository->findAll();
186
                $nextTimeOut = time() + $this->timeToLive;
187
                if ($this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

187
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
188
                    echo 'Cleanup' . implode(',', $timedOutProcesses->getProcessIds()) . chr(10);
189
                }
190
                $this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds());
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\Proc...ice::$crawlerController has been deprecated. ( Ignorable by Annotation )

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

190
                /** @scrutinizer ignore-deprecated */ $this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds());
Loading history...
191
            }
192
        }
193
        if ($currentPendingItems > 0 && $this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

193
        if ($currentPendingItems > 0 && /** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
194
            echo 'Stop with timeout' . chr(10);
195
        }
196
    }
197
198
    /**
199
     * starts new process
200
     * @throws ProcessException if no crawler process was started
201
     */
202 2
    public function startProcess(): bool
203
    {
204 2
        $ttl = (time() + $this->timeToLive - 1);
205 2
        $current = $this->processRepository->countNotTimeouted($ttl);
206
207
        // Check whether OS is Windows
208 2
        if (Environment::isWindows()) {
209
            $completePath = 'start ' . $this->getCrawlerCliPath();
210
        } else {
211 2
            $completePath = '(' . $this->getCrawlerCliPath() . ' &) > /dev/null';
212
        }
213
214 2
        $fileHandler = CommandUtility::exec($completePath);
215 2
        if ($fileHandler === false) {
216
            throw new ProcessException('could not start process!');
217
        }
218 2
        for ($i = 0; $i < 10; $i++) {
219 2
            if ($this->processRepository->countNotTimeouted($ttl) > $current) {
220 2
                return true;
221
            }
222
            sleep(1);
223
        }
224
        throw new ProcessException('Something went wrong: process did not appear within 10 seconds.');
225
    }
226
227
    /**
228
     * Returns the path to start the crawler from the command line
229
     */
230 24
    public function getCrawlerCliPath(): string
231
    {
232 24
        $phpPath = PhpBinaryUtility::getPhpBinary();
233 24
        $typo3BinaryPath = ExtensionManagementUtility::extPath('core') . 'bin/';
234 24
        $cliPart = 'typo3 crawler:processQueue';
235
        // Don't like the spacing, but don't have an better idea for now
236 24
        $scriptPath = $phpPath . ' ' . $typo3BinaryPath . $cliPart;
237
238 24
        if (Environment::isWindows()) {
239
            $scriptPath = str_replace('/', '\\', $scriptPath);
240
        }
241
242 24
        return ltrim($scriptPath);
243
    }
244
245
    /**
246
     * Reports curent Status of queue
247
     * @deprecated
248
     * @codeCoverageIgnore
249
     */
250
    protected function reportItemStatus(): void
251
    {
252
        echo 'Pending:' . $this->queueRepository->countAllPendingItems() . ' / Assigned:' . $this->queueRepository->countAllAssignedPendingItems() . chr(10);
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\Proc...rvice::$queueRepository has been deprecated. ( Ignorable by Annotation )

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

252
        echo 'Pending:' . $this->queueRepository->countAllPendingItems() . ' / Assigned:' . /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllAssignedPendingItems() . chr(10);
Loading history...
253
    }
254
255
    /**
256
     * according to the given count of pending items and the countInARun Setting this method
257
     * starts more crawling processes
258
     *
259
     * @return boolean if processes are started
260
     * @throws ProcessException
261
     * @deprecated
262
     * @codeCoverageIgnore
263
     */
264
    private function startRequiredProcesses()
265
    {
266
        $ret = false;
267
        $currentProcesses = $this->processRepository->findAllActive()->count();
268
        $availableProcessesCount = $this->processLimit - $currentProcesses;
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$processLimit has been deprecated. ( Ignorable by Annotation )

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

268
        $availableProcessesCount = /** @scrutinizer ignore-deprecated */ $this->processLimit - $currentProcesses;
Loading history...
269
        $requiredProcessesCount = ceil($this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun);
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$countInARun has been deprecated. ( Ignorable by Annotation )

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

269
        $requiredProcessesCount = ceil($this->queueRepository->countAllUnassignedPendingItems() / /** @scrutinizer ignore-deprecated */ $this->countInARun);
Loading history...
Deprecated Code introduced by
The property AOE\Crawler\Service\Proc...rvice::$queueRepository has been deprecated. ( Ignorable by Annotation )

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

269
        $requiredProcessesCount = ceil(/** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun);
Loading history...
270
        $startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]);
271
        if ($startProcessCount <= 0) {
272
            return $ret;
273
        }
274
        if ($startProcessCount && $this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

274
        if ($startProcessCount && /** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
275
            echo 'Start ' . $startProcessCount . ' new processes (Running:' . $currentProcesses . ')';
276
        }
277
        for ($i = 0; $i < $startProcessCount; $i++) {
278
            usleep(100);
279
            if ($this->startProcess()) {
280
                if ($this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

280
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
281
                    echo '.';
282
                    $ret = true;
283
                }
284
            }
285
        }
286
        if ($this->verbose) {
0 ignored issues
show
Deprecated Code introduced by
The property AOE\Crawler\Service\ProcessService::$verbose has been deprecated. ( Ignorable by Annotation )

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

286
        if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
287
            echo chr(10);
288
        }
289
        return $ret;
290
    }
291
}
292