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 = [ |
|
|
|
|
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 = [ |
|
|
|
|
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); |
|
|
|
|
122
|
|
|
$extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration(); |
123
|
|
|
$this->timeToLive = intval($extensionSettings['processMaxRunTime']); |
124
|
|
|
$this->countInARun = intval($extensionSettings['countInARun']); |
|
|
|
|
125
|
|
|
$this->processLimit = intval($extensionSettings['processLimit']); |
|
|
|
|
126
|
|
|
$this->verbose = boolval($extensionSettings['processVerbose']); |
|
|
|
|
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) { |
|
|
|
|
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(); |
|
|
|
|
144
|
|
|
$itemReportLimit = 20; |
145
|
|
|
$reportItemCount = $pendingItemsStart - $itemReportLimit; |
146
|
|
|
if ($this->verbose) { |
|
|
|
|
147
|
|
|
$this->reportItemStatus(); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
$this->startRequiredProcesses(); |
|
|
|
|
150
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
151
|
|
|
$currentPendingItems = ''; |
152
|
|
|
for ($i = 0; $i < $timeout; $i++) { |
153
|
|
|
$currentPendingItems = $this->queueRepository->countAllPendingItems(); |
|
|
|
|
154
|
|
|
if ($this->startRequiredProcesses()) { |
|
|
|
|
155
|
|
|
$nextTimeOut = time() + $this->timeToLive; |
156
|
|
|
} |
157
|
|
|
if ($currentPendingItems === 0) { |
158
|
|
|
if ($this->verbose) { |
|
|
|
|
159
|
|
|
echo 'Finished...' . chr(10); |
160
|
|
|
} |
161
|
|
|
break; |
162
|
|
|
} |
163
|
|
|
if ($currentPendingItems < $reportItemCount) { |
164
|
|
|
if ($this->verbose) { |
|
|
|
|
165
|
|
|
$this->reportItemStatus(); |
|
|
|
|
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) { |
|
|
|
|
174
|
|
|
echo 'Cleanup' . implode(',', $timedOutProcesses->getProcessIds()) . chr(10); |
175
|
|
|
} |
176
|
|
|
$this->crawlerController->CLI_releaseProcesses($timedOutProcesses->getProcessIds()); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
if ($currentPendingItems > 0 && $this->verbose) { |
|
|
|
|
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); |
|
|
|
|
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; |
|
|
|
|
253
|
|
|
$requiredProcessesCount = ceil($this->queueRepository->countAllUnassignedPendingItems() / $this->countInARun); |
|
|
|
|
254
|
|
|
$startProcessCount = min([$availableProcessesCount, $requiredProcessesCount]); |
255
|
|
|
if ($startProcessCount <= 0) { |
256
|
|
|
return $ret; |
257
|
|
|
} |
258
|
|
|
if ($startProcessCount && $this->verbose) { |
|
|
|
|
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) { |
|
|
|
|
265
|
|
|
echo '.'; |
266
|
|
|
$ret = true; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
if ($this->verbose) { |
|
|
|
|
271
|
|
|
echo chr(10); |
272
|
|
|
} |
273
|
|
|
return $ret; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|