|
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 = [ |
|
|
|
|
|
|
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 = [ |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
129
|
|
|
$extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration(); |
|
130
|
|
|
$this->timeToLive = intval($extensionSettings['processMaxRunTime']); |
|
131
|
|
|
$this->countInARun = intval($extensionSettings['countInARun']); |
|
|
|
|
|
|
132
|
|
|
$this->processLimit = intval($extensionSettings['processLimit']); |
|
|
|
|
|
|
133
|
|
|
$this->verbose = boolval($extensionSettings['processVerbose']); |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
151
|
|
|
$itemReportLimit = 20; |
|
152
|
|
|
$reportItemCount = $pendingItemsStart - $itemReportLimit; |
|
153
|
|
|
if ($this->verbose) { |
|
|
|
|
|
|
154
|
|
|
$this->reportItemStatus(); |
|
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
$this->startRequiredProcesses(); |
|
|
|
|
|
|
157
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
|
158
|
|
|
$currentPendingItems = ''; |
|
159
|
|
|
for ($i = 0; $i < $timeout; $i++) { |
|
160
|
|
|
$currentPendingItems = $this->queueRepository->countAllPendingItems(); |
|
|
|
|
|
|
161
|
|
|
if ($this->startRequiredProcesses()) { |
|
|
|
|
|
|
162
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
|
163
|
|
|
} |
|
164
|
|
|
if ($currentPendingItems === 0) { |
|
165
|
|
|
if ($this->verbose) { |
|
|
|
|
|
|
166
|
|
|
echo 'Finished...' . chr(10); |
|
167
|
|
|
} |
|
168
|
|
|
break; |
|
169
|
|
|
} |
|
170
|
|
|
if ($currentPendingItems < $reportItemCount) { |
|
171
|
|
|
if ($this->verbose) { |
|
|
|
|
|
|
172
|
|
|
$this->reportItemStatus(); |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
181
|
|
|
echo 'Cleanup' . implode(',', $timedOutProcesses->getProcessIds()) . chr(10); |
|
182
|
|
|
} |
|
183
|
|
|
$this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds()); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
if ($currentPendingItems > 0 && $this->verbose) { |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
260
|
|
|
$requiredProcessesCount = ceil($this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun); |
|
|
|
|
|
|
261
|
|
|
$startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]); |
|
262
|
|
|
if ($startProcessCount <= 0) { |
|
263
|
|
|
return $ret; |
|
264
|
|
|
} |
|
265
|
|
|
if ($startProcessCount && $this->verbose) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
272
|
|
|
echo '.'; |
|
273
|
|
|
$ret = true; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
if ($this->verbose) { |
|
|
|
|
|
|
278
|
|
|
echo chr(10); |
|
279
|
|
|
} |
|
280
|
|
|
return $ret; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|