1
|
|
|
<?php |
2
|
|
|
/*************************************************************** |
3
|
|
|
* Copyright notice |
4
|
|
|
* |
5
|
|
|
* (c) 2009 AOE media ([email protected]) |
6
|
|
|
* All rights reserved |
7
|
|
|
* |
8
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
9
|
|
|
* free software; you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* The GNU General Public License can be found at |
15
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
16
|
|
|
* |
17
|
|
|
* This script is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
23
|
|
|
***************************************************************/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Manages cralwer processes and can be used to start a new process or multiple processes |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
class tx_crawler_domain_process_manager |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var $timeToLive integer |
33
|
|
|
*/ |
34
|
|
|
private $timeToLive; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var integer |
38
|
|
|
*/ |
39
|
|
|
private $countInARun; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var integer |
43
|
|
|
*/ |
44
|
|
|
private $processLimit; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var $crawlerObj tx_crawler_lib |
48
|
|
|
*/ |
49
|
|
|
private $crawlerObj; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var $queueRepository tx_crawler_domain_queue_repository |
53
|
|
|
*/ |
54
|
|
|
private $queueRepository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var tx_crawler_domain_process_repository |
58
|
|
|
*/ |
59
|
|
|
private $processRepository; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var $verbose boolean |
63
|
|
|
*/ |
64
|
|
|
private $verbose; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* the constructor |
68
|
|
|
*/ |
69
|
|
|
public function __construct() |
70
|
|
|
{ |
71
|
|
|
$this->processRepository = new tx_crawler_domain_process_repository(); |
72
|
|
|
$this->queueRepository = new tx_crawler_domain_queue_repository(); |
73
|
|
|
$this->crawlerObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_crawler_lib'); |
|
|
|
|
74
|
|
|
$this->timeToLive = intval($this->crawlerObj->extensionSettings['processMaxRunTime']); |
75
|
|
|
$this->countInARun = intval($this->crawlerObj->extensionSettings['countInARun']); |
76
|
|
|
$this->processLimit = intval($this->crawlerObj->extensionSettings['processLimit']); |
77
|
|
|
$this->verbose = intval($this->crawlerObj->extensionSettings['processVerbose']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* starts multiple processes |
82
|
|
|
* |
83
|
|
|
* @param integer $timeout |
84
|
|
|
* |
85
|
|
|
* @throws RuntimeException |
86
|
|
|
*/ |
87
|
|
|
public function multiProcess($timeout) |
88
|
|
|
{ |
89
|
|
|
if ($this->processLimit <= 1) { |
90
|
|
|
throw new RuntimeException('To run crawler in multi process mode you have to configure the processLimit > 1.' . PHP_EOL); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$pendingItemsStart = $this->queueRepository->countAllPendingItems(); |
94
|
|
|
$itemReportLimit = 20; |
95
|
|
|
$reportItemCount = $pendingItemsStart - $itemReportLimit; |
96
|
|
|
if ($this->verbose) { |
97
|
|
|
$this->reportItemStatus(); |
98
|
|
|
} |
99
|
|
|
$this->startRequiredProcesses(); |
100
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
101
|
|
|
for ($i = 0; $i < $timeout; $i++) { |
102
|
|
|
$currentPendingItems = $this->queueRepository->countAllPendingItems(); |
103
|
|
|
if ($this->startRequiredProcesses($this->verbose)) { |
|
|
|
|
104
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
105
|
|
|
} |
106
|
|
|
if ($currentPendingItems == 0) { |
107
|
|
|
if ($this->verbose) { |
108
|
|
|
echo 'Finished...' . chr(10); |
109
|
|
|
} |
110
|
|
|
break; |
111
|
|
|
} |
112
|
|
|
if ($currentPendingItems < $reportItemCount) { |
113
|
|
|
if ($this->verbose) { |
114
|
|
|
$this->reportItemStatus(); |
115
|
|
|
} |
116
|
|
|
$reportItemCount = $currentPendingItems - $itemReportLimit; |
117
|
|
|
} |
118
|
|
|
sleep(1); |
119
|
|
|
if ($nextTimeOut < time()) { |
120
|
|
|
$timedOutProcesses = $this->processRepository->findAll('', 'DESC', null, 0, 'ttl >' . $nextTimeOut); |
121
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
122
|
|
|
if ($this->verbose) { |
123
|
|
|
echo 'Cleanup' . implode(',', $timedOutProcesses->getProcessIds()) . chr(10); |
124
|
|
|
} |
125
|
|
|
$this->crawlerObj->CLI_releaseProcesses($timedOutProcesses->getProcessIds(), true); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
if ($currentPendingItems > 0 && $this->verbose) { |
|
|
|
|
129
|
|
|
echo 'Stop with timeout' . chr(10); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Reports curent Status of queue |
135
|
|
|
*/ |
136
|
|
|
protected function reportItemStatus() |
137
|
|
|
{ |
138
|
|
|
echo 'Pending:' . $this->queueRepository->countAllPendingItems() . ' / Assigned:' . $this->queueRepository->countAllAssignedPendingItems() . chr(10); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* according to the given count of pending items and the countInARun Setting this method |
143
|
|
|
* starts more crawling processes |
144
|
|
|
* @return boolean if processes are started |
145
|
|
|
*/ |
146
|
|
|
private function startRequiredProcesses() |
147
|
|
|
{ |
148
|
|
|
$ret = false; |
149
|
|
|
$currentProcesses = $this->processRepository->countActive(); |
150
|
|
|
$availableProcessesCount = $this->processLimit - $currentProcesses; |
151
|
|
|
$requiredProcessesCount = ceil($this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun); |
152
|
|
|
$startProcessCount = min([$availableProcessesCount,$requiredProcessesCount]); |
153
|
|
|
if ($startProcessCount <= 0) { |
154
|
|
|
return $ret; |
155
|
|
|
} |
156
|
|
|
if ($startProcessCount && $this->verbose) { |
157
|
|
|
echo 'Start ' . $startProcessCount . ' new processes (Running:' . $currentProcesses . ')'; |
158
|
|
|
} |
159
|
|
|
for ($i = 0;$i < $startProcessCount;$i++) { |
160
|
|
|
usleep(100); |
161
|
|
|
if ($this->startProcess()) { |
162
|
|
|
if ($this->verbose) { |
163
|
|
|
echo '.'; |
164
|
|
|
$ret = true; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
if ($this->verbose) { |
169
|
|
|
echo chr(10); |
170
|
|
|
} |
171
|
|
|
return $ret; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* starts new process |
176
|
|
|
* @throws Exception if no crawler process was started |
177
|
|
|
*/ |
178
|
|
|
public function startProcess() |
179
|
|
|
{ |
180
|
|
|
$ttl = (time() + $this->timeToLive - 1); |
181
|
|
|
$current = $this->processRepository->countNotTimeouted($ttl); |
182
|
|
|
|
183
|
|
|
// Check whether OS is Windows |
184
|
|
|
if (TYPO3_OS === 'WIN') { |
|
|
|
|
185
|
|
|
$completePath = escapeshellcmd('start ' . $this->getCrawlerCliPath()); |
186
|
|
|
} else { |
187
|
|
|
$completePath = '(' . escapeshellcmd($this->getCrawlerCliPath()) . ' &) > /dev/null'; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$fileHandler = system($completePath); |
191
|
|
|
if ($fileHandler === false) { |
192
|
|
|
throw new Exception('could not start process!'); |
193
|
|
|
} else { |
194
|
|
|
for ($i = 0;$i < 10;$i++) { |
195
|
|
|
if ($this->processRepository->countNotTimeouted($ttl) > $current) { |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
sleep(1); |
199
|
|
|
} |
200
|
|
|
throw new Exception('Something went wrong: process did not appear within 10 seconds.'); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns the path to start the crawler from the command line |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getCrawlerCliPath() |
210
|
|
|
{ |
211
|
|
|
$phpPath = $this->crawlerObj->extensionSettings['phpPath'] . ' '; |
212
|
|
|
$pathToTypo3 = rtrim(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'), '/'); |
213
|
|
|
$pathToTypo3 .= rtrim(\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_PATH'), '/'); |
214
|
|
|
$cliPart = '/typo3/cli_dispatch.phpsh crawler'; |
215
|
|
|
$scriptPath = $phpPath . $pathToTypo3 . $cliPart; |
216
|
|
|
|
217
|
|
|
if (TYPO3_OS === 'WIN') { |
|
|
|
|
218
|
|
|
$scriptPath = str_replace('/', '\\', $scriptPath); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $scriptPath; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths