Passed
Push — deprecations ( 3cb403...87297a )
by Tomas Norre
10:49 queued 06:55
created

ProcessService   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Test Coverage

Coverage 8.47%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
C multiProcess() 0 44 13
A __construct() 0 10 1
A startProcess() 0 23 5
A getCrawlerCliPath() 0 13 2
B startRequiredProcesses() 0 26 8
A reportItemStatus() 0 3 1
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\Utility\PhpBinaryUtility;
36
use TYPO3\CMS\Core\Compatibility\PublicMethodDeprecationTrait;
37
use TYPO3\CMS\Core\Compatibility\PublicPropertyDeprecationTrait;
38
use TYPO3\CMS\Core\Core\Environment;
39
use TYPO3\CMS\Core\Utility\CommandUtility;
40
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
41
use TYPO3\CMS\Core\Utility\GeneralUtility;
42
use TYPO3\CMS\Extbase\Object\ObjectManager;
43
44
/**
45
 * Class ProcessService
46
 *
47
 * @package AOE\Crawler\Service
48
 */
49
class ProcessService
50
{
51
    use PublicMethodDeprecationTrait;
52
    use PublicPropertyDeprecationTrait;
53
54
    /**
55
     * @var string[]
56
     */
57
    private $deprecatedPublicProperties = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicProperties is not used, and could be removed.
Loading history...
58
        'queueRepository' => 'Using queueRepository is deprecated since 9.0.1 and will be removed in v10.x',
59
        'crawlerController' => 'Using crawlerController is deprecated since 9.0.1 and will be removed in v10.x',
60
        'countInARun' => 'Using countInARun is deprecated since 9.0.1 and will be removed in v10.x',
61
        'processLimit' => 'Using processLimit is deprecated since 9.0.1 and will be removed in v10.x',
62
        'verbose' => 'Using verbose is deprecated since 9.0.1 and will be removed in v10.x',
63
    ];
64
65
    /**
66
     * @var string[]
67
     */
68
    private $deprecatedPublicMethods = [
0 ignored issues
show
introduced by
The private property $deprecatedPublicMethods is not used, and could be removed.
Loading history...
69
        'multiProcess' => 'Using ProcessService::multiProcess() is deprecated since 9.0.1 and will be removed in v10.x',
70
        'reportItemStatus' => 'Using ProcessService::reportItemStatus() is deprecated since 9.0.1 and will be removed in v10.x',
71
        'startRequiredProcesses' => 'Using ProcessService::startRequiredProcesses() is deprecated since 9.0.1 and will be removed in v10.x',
72
    ];
73
74
    /**
75
     * @var int
76
     */
77
    private $timeToLive;
78
79
    /**
80
     * @var int
81
     * @deprecated
82
     */
83
    private $countInARun;
84
85
    /**
86
     * @var int
87
     * @deprecated
88
     */
89
    private $processLimit;
90
91
    /**
92
     * @var CrawlerController
93
     * @deprecated
94
     */
95
    private $crawlerController;
96
97
    /**
98
     * @var \AOE\Crawler\Domain\Repository\QueueRepository
99
     * @deprecated
100
     */
101
    private $queueRepository;
102
103
    /**
104
     * @var \AOE\Crawler\Domain\Repository\ProcessRepository
105
     */
106
    private $processRepository;
107
108
    /**
109
     * @var bool
110
     * @deprecated
111
     */
112
    private $verbose;
113
114
    /**
115
     * the constructor
116
     */
117
    public function __construct()
118
    {
119
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
120
        $this->processRepository = $objectManager->get(ProcessRepository::class);
121
        $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

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

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

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

126
        /** @scrutinizer ignore-deprecated */ $this->verbose = boolval($extensionSettings['processVerbose']);
Loading history...
127
    }
128
129
    /**
130
     * starts multiple processes
131
     *
132
     * @param integer $timeout
133
     *
134
     * @throws \RuntimeException
135
     * @deprecated
136
     */
137 1
    public function multiProcess($timeout): void
138
    {
139 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

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

143
        $pendingItemsStart = /** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllPendingItems();
Loading history...
144
        $itemReportLimit = 20;
145
        $reportItemCount = $pendingItemsStart - $itemReportLimit;
146
        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

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

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

149
        /** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses();
Loading history...
150
        $nextTimeOut = time() + $this->timeToLive;
151
        $currentPendingItems = '';
152
        for ($i = 0; $i < $timeout; $i++) {
153
            $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

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

154
            if (/** @scrutinizer ignore-deprecated */ $this->startRequiredProcesses()) {
Loading history...
155
                $nextTimeOut = time() + $this->timeToLive;
156
            }
157
            if ($currentPendingItems === 0) {
158
                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

158
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
159
                    echo 'Finished...' . chr(10);
160
                }
161
                break;
162
            }
163
            if ($currentPendingItems < $reportItemCount) {
164
                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

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

165
                    /** @scrutinizer ignore-deprecated */ $this->reportItemStatus();
Loading history...
166
                }
167
                $reportItemCount = $currentPendingItems - $itemReportLimit;
168
            }
169
            sleep(1);
170
            if ($nextTimeOut < time()) {
171
                $timedOutProcesses = $this->processRepository->findAll();
172
                $nextTimeOut = time() + $this->timeToLive;
173
                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

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

176
                /** @scrutinizer ignore-deprecated */ $this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds());
Loading history...
177
            }
178
        }
179
        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

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

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

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

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

253
        $requiredProcessesCount = ceil(/** @scrutinizer ignore-deprecated */ $this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun);
Loading history...
254
        $startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]);
255
        if ($startProcessCount <= 0) {
256
            return $ret;
257
        }
258
        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

258
        if ($startProcessCount && /** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
259
            echo 'Start ' . $startProcessCount . ' new processes (Running:' . $currentProcesses . ')';
260
        }
261
        for ($i = 0; $i < $startProcessCount; $i++) {
262
            usleep(100);
263
            if ($this->startProcess()) {
264
                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

264
                if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
265
                    echo '.';
266
                    $ret = true;
267
                }
268
            }
269
        }
270
        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

270
        if (/** @scrutinizer ignore-deprecated */ $this->verbose) {
Loading history...
271
            echo chr(10);
272
        }
273
        return $ret;
274
    }
275
}
276