Completed
Push — bugfix/domain-model-repository ( 4b87f9...1dbd77 )
by Tomas Norre
08:57
created

ProcessService   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 9.84%

Importance

Changes 0
Metric Value
dl 0
loc 202
ccs 12
cts 122
cp 0.0984
rs 9.92
c 0
b 0
f 0
wmc 31
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
C multiProcess() 0 46 13
A reportItemStatus() 0 4 1
B startRequiredProcesses() 0 27 8
A startProcess() 0 25 5
A getCrawlerCliPath() 0 16 3
1
<?php
2
namespace AOE\Crawler\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2019 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\GeneralUtility;
32
use TYPO3\CMS\Extbase\Object\ObjectManager;
33
34
/**
35
 * Class ProcessService
36
 *
37
 * @package AOE\Crawler\Service
38
 */
39
class ProcessService
40
{
41
    /**
42
     * @var $timeToLive integer
43
     */
44
    private $timeToLive;
45
46
    /**
47
     * @var integer
48
     */
49
    private $countInARun;
50
51
    /**
52
     * @var integer
53
     */
54
    private $processLimit;
55
56
    /**
57
     * @var CrawlerController
58
     */
59
    private $crawlerController;
60
61
    /**
62
     * @var \AOE\Crawler\Domain\Repository\QueueRepository
63
     */
64
    private $queueRepository;
65
66
    /**
67
     * @var \AOE\Crawler\Domain\Repository\ProcessRepository
68
     */
69
    private $processRepository;
70
71
    /**
72
     * @var $verbose boolean
73
     */
74
    private $verbose;
75
76
    /**
77
     * the constructor
78
     */
79
    public function __construct()
80
    {
81
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
82
        $this->processRepository = $objectManager->get(ProcessRepository::class);
83
        $this->queueRepository = $objectManager->get(QueueRepository::class);
84
        $this->crawlerController = $objectManager->get(CrawlerController::class);
85
        $this->timeToLive = intval($this->crawlerController->extensionSettings['processMaxRunTime']);
86
        $this->countInARun = intval($this->crawlerController->extensionSettings['countInARun']);
87
        $this->processLimit = intval($this->crawlerController->extensionSettings['processLimit']);
88
        $this->verbose = intval($this->crawlerController->extensionSettings['processVerbose']);
89
    }
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);
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);
0 ignored issues
show
Deprecated Code introduced by
The method AOE\Crawler\Controller\C...:CLI_releaseProcesses() has been deprecated with message: since crawler v6.5.1, will be removed in crawler v9.0.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
     * @throws \Exception
158
     *
159
     * @return boolean if processes are started
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 = system($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
        $jsonDecoded = json_decode(file_get_contents($composerRootDir . 'composer.json'), true);
228 1
        $binDir = $jsonDecoded['config']['bin-dir'] ?: 'vendor/bin';
229
230 1
        $phpPath = $this->crawlerController->extensionSettings['phpPath'] . ' ';
231 1
        $cliPart = '/typo3cms crawler:crawlqueue';
232 1
        $scriptPath = $phpPath . $composerRootDir . $binDir . $cliPart;
233
234 1
        if (TYPO3_OS === 'WIN') {
235
            $scriptPath = str_replace('/', '\\', $scriptPath);
236
        }
237
238 1
        return ltrim($scriptPath);
239
    }
240
}
241