Passed
Push — codeception ( 70a2c0...f5176b )
by Tomas Norre
06:16
created

ProcessService   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Test Coverage

Coverage 8.47%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 30
eloc 99
c 5
b 0
f 0
dl 0
loc 231
ccs 10
cts 118
cp 0.0847
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A startProcess() 0 23 5
A __construct() 0 10 1
B startRequiredProcesses() 0 26 8
A reportItemStatus() 0 3 1
A getCrawlerCliPath() 0 13 2
C multiProcess() 0 44 13
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Service;
6
7
/***************************************************************
8
 *  Copyright notice
9
 *
10
 *  (c) 2017 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
 */
50
class ProcessService
51
{
52
    use PublicMethodDeprecationTrait;
53
    use PublicPropertyDeprecationTrait;
54
55
    /**
56
     * @var string[]
57
     * @noRector
58
     * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector
59
     * @noRector \Rector\DeadCode\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector
60
     */
61
    private $deprecatedPublicProperties = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicProperties is not used, and could be removed.
Loading history...
62
        'queueRepository' => 'Using queueRepository is deprecated since 9.0.1 and will be removed in v10.x',
63
        'crawlerController' => 'Using crawlerController is deprecated since 9.0.1 and will be removed in v10.x',
64
        'countInARun' => 'Using countInARun is deprecated since 9.0.1 and will be removed in v10.x',
65
        'processLimit' => 'Using processLimit is deprecated since 9.0.1 and will be removed in v10.x',
66
        'verbose' => 'Using verbose is deprecated since 9.0.1 and will be removed in v10.x',
67
    ];
68
69
    /**
70
     * @var string[]
71
     * @noRector
72
     * @noRector \Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector
73
     * @noRector \Rector\DeadCode\Rector\Class_\RemoveSetterOnlyPropertyAndMethodCallRector
74
     */
75
    private $deprecatedPublicMethods = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicMethods is not used, and could be removed.
Loading history...
76
        'multiProcess' => 'Using ProcessService::multiProcess() is deprecated since 9.0.1 and will be removed in v10.x',
77
        'reportItemStatus' => 'Using ProcessService::reportItemStatus() is deprecated since 9.0.1 and will be removed in v10.x',
78
        'startRequiredProcesses' => 'Using ProcessService::startRequiredProcesses() is deprecated since 9.0.1 and will be removed in v10.x',
79
    ];
80
81
    /**
82
     * @var int
83
     */
84
    private $timeToLive;
85
86
    /**
87
     * @var int
88
     * @deprecated
89
     */
90
    private $countInARun;
91
92
    /**
93
     * @var int
94
     * @deprecated
95
     */
96
    private $processLimit;
97
98
    /**
99
     * @var CrawlerController
100
     * @deprecated
101
     */
102
    private $crawlerController;
103
104
    /**
105
     * @var \AOE\Crawler\Domain\Repository\QueueRepository
106
     * @deprecated
107
     */
108
    private $queueRepository;
109
110
    /**
111
     * @var \AOE\Crawler\Domain\Repository\ProcessRepository
112
     */
113
    private $processRepository;
114
115
    /**
116
     * @var bool
117
     * @deprecated
118
     */
119
    private $verbose;
120
121
    /**
122
     * the constructor
123
     */
124
    public function __construct()
125
    {
126
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
127
        $this->processRepository = $objectManager->get(ProcessRepository::class);
128
        $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

128
        /** @scrutinizer ignore-deprecated */ $this->queueRepository = $objectManager->get(QueueRepository::class);
Loading history...
129
        $extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
130
        $this->timeToLive = intval($extensionSettings['processMaxRunTime']);
131
        $this->countInARun = intval($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

131
        /** @scrutinizer ignore-deprecated */ $this->countInARun = intval($extensionSettings['countInARun']);
Loading history...
132
        $this->processLimit = intval($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

132
        /** @scrutinizer ignore-deprecated */ $this->processLimit = intval($extensionSettings['processLimit']);
Loading history...
133
        $this->verbose = boolval($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

133
        /** @scrutinizer ignore-deprecated */ $this->verbose = boolval($extensionSettings['processVerbose']);
Loading history...
134
    }
135
136
    /**
137
     * starts multiple processes
138
     *
139
     * @param integer $timeout
140
     *
141
     * @throws \RuntimeException
142
     * @deprecated
143
     */
144 1
    public function multiProcess($timeout): void
145
    {
146 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

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

150
        $pendingItemsStart = /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllPendingItems();
Loading history...
151
        $itemReportLimit = 20;
152
        $reportItemCount = $pendingItemsStart - $itemReportLimit;
153
        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

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

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

156
        /** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses();
Loading history...
157
        $nextTimeOut = time() + $this->timeToLive;
158
        $currentPendingItems = '';
159
        for ($i = 0; $i < $timeout; $i++) {
160
            $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

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

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

165
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
166
                    echo 'Finished...' . chr(10);
167
                }
168
                break;
169
            }
170
            if ($currentPendingItems < $reportItemCount) {
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
                    $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

172
                    /** @scrutinizer ignore-deprecated */ $this->reportItemStatus();
Loading history...
173
                }
174
                $reportItemCount = $currentPendingItems - $itemReportLimit;
175
            }
176
            sleep(1);
177
            if ($nextTimeOut < time()) {
178
                $timedOutProcesses = $this->processRepository->findAll();
179
                $nextTimeOut = time() + $this->timeToLive;
180
                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

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

183
                /** @scrutinizer ignore-deprecated */ $this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds());
Loading history...
184
            }
185
        }
186
        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

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

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

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

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

260
        $requiredProcessesCount = ceil(/** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun);
Loading history...
261
        $startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]);
262
        if ($startProcessCount <= 0) {
263
            return $ret;
264
        }
265
        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

265
        if ($startProcessCount && /** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
266
            echo 'Start ' . $startProcessCount . ' new processes (Running:' . $currentProcesses . ')';
267
        }
268
        for ($i = 0; $i < $startProcessCount; $i++) {
269
            usleep(100);
270
            if ($this->startProcess()) {
271
                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

271
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
272
                    echo '.';
273
                    $ret = true;
274
                }
275
            }
276
        }
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 chr(10);
279
        }
280
        return $ret;
281
    }
282
}
283