Completed
Push — typo3v9 ( 818151...63af3e )
by Tomas Norre
12:14 queued 02:12
created

ProcessService   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 22.92%

Importance

Changes 0
Metric Value
dl 0
loc 214
ccs 22
cts 96
cp 0.2292
rs 9.76
c 0
b 0
f 0
wmc 33
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A reportItemStatus() 0 4 1
A __construct() 0 10 1
C multiProcess() 0 46 13
B startRequiredProcesses() 0 27 8
A startProcess() 0 25 5
A getCrawlerCliPath() 0 29 5
1
<?php
2
namespace AOE\Crawler\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH <[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 AOE\Crawler\Controller\CrawlerController;
29
use AOE\Crawler\Domain\Repository\ProcessRepository;
30
use AOE\Crawler\Domain\Repository\QueueRepository;
31
use TYPO3\CMS\Core\Utility\CommandUtility;
32
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Class ProcessService
37
 *
38
 * @package AOE\Crawler\Service
39
 */
40
class ProcessService
41
{
42
    /**
43
     * @var $timeToLive integer
44
     */
45
    private $timeToLive;
46
47
    /**
48
     * @var integer
49
     */
50
    private $countInARun;
51
52
    /**
53
     * @var integer
54
     */
55
    private $processLimit;
56
57
    /**
58
     * @var CrawlerController
59
     */
60
    private $crawlerController;
61
62
    /**
63
     * @var \AOE\Crawler\Domain\Repository\QueueRepository
64
     */
65
    private $queueRepository;
66
67
    /**
68
     * @var \AOE\Crawler\Domain\Repository\ProcessRepository
69
     */
70
    private $processRepository;
71
72
    /**
73
     * @var $verbose boolean
74
     */
75
    private $verbose;
76
77
    /**
78
     * the constructor
79
     */
80 1
    public function __construct()
81
    {
82 1
        $this->processRepository = new ProcessRepository();
83 1
        $this->queueRepository = new QueueRepository();
84 1
        $this->crawlerController = GeneralUtility::makeInstance(CrawlerController::class);
85 1
        $this->timeToLive = intval($this->crawlerController->extensionSettings['processMaxRunTime']);
86 1
        $this->countInARun = intval($this->crawlerController->extensionSettings['countInARun']);
87 1
        $this->processLimit = intval($this->crawlerController->extensionSettings['processLimit']);
88 1
        $this->verbose = intval($this->crawlerController->extensionSettings['processVerbose']);
89 1
    }
90
91
    /**
92
     * starts multiple processes
93
     *
94
     * @param integer $timeout
95
     *
96
     * @throws \RuntimeException
97
     */
98 1
    public function multiProcess($timeout)
99
    {
100 1
        if ($this->processLimit <= 1) {
101 1
            throw new \RuntimeException('To run crawler in multi process mode you have to configure the processLimit > 1.' . PHP_EOL);
102
        }
103
104
        $pendingItemsStart = $this->queueRepository->countAllPendingItems();
105
        $itemReportLimit = 20;
106
        $reportItemCount = $pendingItemsStart - $itemReportLimit;
107
        if ($this->verbose) {
108
            $this->reportItemStatus();
109
        }
110
        $this->startRequiredProcesses();
111
        $nextTimeOut = time() + $this->timeToLive;
112
        $currentPendingItems = '';
113
        for ($i = 0; $i < $timeout; $i++) {
114
            $currentPendingItems = $this->queueRepository->countAllPendingItems();
115
            if ($this->startRequiredProcesses()) {
116
                $nextTimeOut = time() + $this->timeToLive;
117
            }
118
            if ($currentPendingItems == 0) {
119
                if ($this->verbose) {
120
                    echo 'Finished...' . chr(10);
121
                }
122
                break;
123
            }
124
            if ($currentPendingItems < $reportItemCount) {
125
                if ($this->verbose) {
126
                    $this->reportItemStatus();
127
                }
128
                $reportItemCount = $currentPendingItems - $itemReportLimit;
129
            }
130
            sleep(1);
131
            if ($nextTimeOut < time()) {
132
                $timedOutProcesses = $this->processRepository->findAll('', 'DESC', null, 0, 'ttl >' . $nextTimeOut);
0 ignored issues
show
Unused Code introduced by
The call to ProcessRepository::findAll() has too many arguments starting with ''.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
                $nextTimeOut = time() + $this->timeToLive;
134
                if ($this->verbose) {
135
                    echo 'Cleanup' . implode(',', $timedOutProcesses->getProcessIds()) . chr(10);
136
                }
137
                $this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds(), true);
138
            }
139
        }
140
        if ($currentPendingItems > 0 && $this->verbose) {
141
            echo 'Stop with timeout' . chr(10);
142
        }
143
    }
144
145
    /**
146
     * Reports curent Status of queue
147
     */
148
    protected function reportItemStatus()
149
    {
150
        echo 'Pending:' . $this->queueRepository->countAllPendingItems() . ' / Assigned:' . $this->queueRepository->countAllAssignedPendingItems() . chr(10);
151
    }
152
153
    /**
154
     * according to the given count of pending items and the countInARun Setting this method
155
     * starts more crawling processes
156
     *
157
     * @return boolean if processes are started
158
     * @throws \Exception
159
     *
160
     */
161
    private function startRequiredProcesses()
162
    {
163
        $ret = false;
164
        $currentProcesses = $this->processRepository->countActive();
165
        $availableProcessesCount = $this->processLimit - $currentProcesses;
166
        $requiredProcessesCount = ceil($this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun);
167
        $startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]);
168
        if ($startProcessCount <= 0) {
169
            return $ret;
170
        }
171
        if ($startProcessCount && $this->verbose) {
172
            echo 'Start ' . $startProcessCount . ' new processes (Running:' . $currentProcesses . ')';
173
        }
174
        for ($i = 0; $i < $startProcessCount; $i++) {
175
            usleep(100);
176
            if ($this->startProcess()) {
177
                if ($this->verbose) {
178
                    echo '.';
179
                    $ret = true;
180
                }
181
            }
182
        }
183
        if ($this->verbose) {
184
            echo chr(10);
185
        }
186
        return $ret;
187
    }
188
189
    /**
190
     * starts new process
191
     * @throws \Exception if no crawler process was started
192
     */
193
    public function startProcess()
194
    {
195
        $ttl = (time() + $this->timeToLive - 1);
196
        $current = $this->processRepository->countNotTimeouted($ttl);
197
198
        // Check whether OS is Windows
199
        if (TYPO3_OS === 'WIN') {
200
            $completePath = escapeshellcmd('start ' . $this->getCrawlerCliPath());
201
        } else {
202
            $completePath = '(' . escapeshellcmd($this->getCrawlerCliPath()) . ' &) > /dev/null';
203
        }
204
205
        $fileHandler = CommandUtility::exec($completePath);
206
        if ($fileHandler === false) {
207
            throw new \Exception('could not start process!');
208
        } else {
209
            for ($i = 0; $i < 10; $i++) {
210
                if ($this->processRepository->countNotTimeouted($ttl) > $current) {
211
                    return true;
212
                }
213
                sleep(1);
214
            }
215
            throw new \Exception('Something went wrong: process did not appear within 10 seconds.');
216
        }
217
    }
218
219
    /**
220
     * Returns the path to start the crawler from the command line
221
     *
222
     * @return string
223
     */
224 1
    public function getCrawlerCliPath()
225
    {
226 1
        $composerRootDir = getenv('TYPO3_PATH_COMPOSER_ROOT') . '/';
227 1
        $composerFile = $composerRootDir . 'composer.json';
228 1
        $phpPath = $this->crawlerController->extensionSettings['phpPath'] . ' ';
229 1
        $cliPart = 'typo3cms crawler:crawlqueue';
230
231 1
        if (file_exists($composerFile)) {
232
            $jsonDecoded = json_decode(file_get_contents($composerFile), true);
233
234
            if (isset($jsonDecoded['config']['bin-dir'])) {
235
                $binDir = $jsonDecoded['config']['bin-dir'];
236
            } elseif (isset($jsonDecoded['config']['vendor-dir'])) {
237
                $binDir = $jsonDecoded['config']['vendor-dir'] . '/bin';
238
            } else {
239
                $binDir = 'vendor/bin';
240
            }
241
            $scriptPath = $phpPath . $composerRootDir . $binDir . '/' . $cliPart;
242
        } else {
243 1
            $typo3ConsolePath = ExtensionManagementUtility::extPath('typo3_console');
244 1
            $scriptPath = $phpPath . $typo3ConsolePath . $cliPart;
245
        }
246
247 1
        if (TYPO3_OS === 'WIN') {
248
            $scriptPath = str_replace('/', '\\', $scriptPath);
249
        }
250
251 1
        return ltrim($scriptPath);
252
    }
253
}
254