Passed
Push — refactor/crawlerController ( 30d084...b87fbb )
by Tomas Norre
07:12
created

ProcessService::createProcessId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
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 14
    public function __construct()
131
    {
132 14
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
133 14
        $this->processRepository = $objectManager->get(ProcessRepository::class);
134 14
        $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 14
        $this->extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
136 14
        $this->timeToLive = (int) $this->extensionSettings['processMaxRunTime'];
137 14
        $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 14
        $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 14
        $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 14
    }
141
142
    /**
143
     * starts multiple processes
144
     *
145
     * @param integer $timeout
146
     *
147
     * @throws \RuntimeException
148
     * @deprecated
149
     */
150 1
    public function multiProcess($timeout): void
151
    {
152 1
        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

152
        if (/** @scrutinizer ignore-deprecated */ $this->processLimit <= 1) {
Loading history...
153 1
            throw new \RuntimeException('To run crawler in multi process mode you have to configure the processLimit > 1.' . PHP_EOL);
154
        }
155
156
        $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

156
        $pendingItemsStart = /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllPendingItems();
Loading history...
157
        $itemReportLimit = 20;
158
        $reportItemCount = $pendingItemsStart - $itemReportLimit;
159
        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

159
        if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
160
            $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

160
            /** @scrutinizer ignore-deprecated */ $this->reportItemStatus();
Loading history...
161
        }
162
        $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

162
        /** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses();
Loading history...
163
        $nextTimeOut = time() + $this->timeToLive;
164
        $currentPendingItems = '';
165
        for ($i = 0; $i < $timeout; $i++) {
166
            $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

166
            $currentPendingItems = /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllPendingItems();
Loading history...
167
            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

167
            if (/** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses()) {
Loading history...
168
                $nextTimeOut = time() + $this->timeToLive;
169
            }
170
            if ($currentPendingItems === 0) {
171
                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

171
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
172
                    echo 'Finished...' . chr(10);
173
                }
174
                break;
175
            }
176
            if ($currentPendingItems < $reportItemCount) {
177
                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

177
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
178
                    $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

178
                    /** @scrutinizer ignore-deprecated */ $this->reportItemStatus();
Loading history...
179
                }
180
                $reportItemCount = $currentPendingItems - $itemReportLimit;
181
            }
182
            sleep(1);
183
            if ($nextTimeOut < time()) {
184
                $timedOutProcesses = $this->processRepository->findAll();
185
                $nextTimeOut = time() + $this->timeToLive;
186
                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

186
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
187
                    echo 'Cleanup' . implode(',', $timedOutProcesses->getProcessIds()) . chr(10);
188
                }
189
                $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

189
                /** @scrutinizer ignore-deprecated */ $this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds());
Loading history...
190
            }
191
        }
192
        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

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

250
        echo 'Pending:' . $this->queueRepository->countAllPendingItems() . ' / Assigned:' . /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllAssignedPendingItems() . chr(10);
Loading history...
251
    }
252
253
    /**
254
     * according to the given count of pending items and the countInARun Setting this method
255
     * starts more crawling processes
256
     *
257
     * @return boolean if processes are started
258
     * @throws ProcessException
259
     * @deprecated
260
     */
261
    private function startRequiredProcesses()
262
    {
263
        $ret = false;
264
        $currentProcesses = $this->processRepository->findAllActive()->count();
265
        $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

265
        $availableProcessesCount = /** @scrutinizer ignore-deprecated */ $this->processLimit - $currentProcesses;
Loading history...
266
        $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

266
        $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

266
        $requiredProcessesCount = ceil(/** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun);
Loading history...
267
        $startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]);
268
        if ($startProcessCount <= 0) {
269
            return $ret;
270
        }
271
        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

271
        if ($startProcessCount && /** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
272
            echo 'Start ' . $startProcessCount . ' new processes (Running:' . $currentProcesses . ')';
273
        }
274
        for ($i = 0; $i < $startProcessCount; $i++) {
275
            usleep(100);
276
            if ($this->startProcess()) {
277
                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

277
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
278
                    echo '.';
279
                    $ret = true;
280
                }
281
            }
282
        }
283
        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

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