1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AOE\Crawler\Service; |
6
|
|
|
|
7
|
|
|
/*************************************************************** |
8
|
|
|
* Copyright notice |
9
|
|
|
* |
10
|
|
|
* (c) 2020 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
|
|
|
* @ignoreAnnotation("noRector") |
50
|
|
|
*/ |
51
|
|
|
class ProcessService |
52
|
|
|
{ |
53
|
|
|
use PublicMethodDeprecationTrait; |
|
|
|
|
54
|
|
|
use PublicPropertyDeprecationTrait; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var int |
58
|
|
|
*/ |
59
|
|
|
private $timeToLive; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \AOE\Crawler\Domain\Repository\ProcessRepository |
63
|
|
|
*/ |
64
|
|
|
private $processRepository; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private $extensionSettings; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* the constructor |
73
|
|
|
*/ |
74
|
8 |
|
public function __construct() |
75
|
|
|
{ |
76
|
8 |
|
$objectManager = GeneralUtility::makeInstance(ObjectManager::class); |
77
|
8 |
|
$this->processRepository = $objectManager->get(ProcessRepository::class); |
78
|
8 |
|
$this->queueRepository = $objectManager->get(QueueRepository::class); |
|
|
|
|
79
|
8 |
|
$this->extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration(); |
80
|
8 |
|
$this->timeToLive = (int) $this->extensionSettings['processMaxRunTime']; |
81
|
8 |
|
$this->countInARun = (int) $this->extensionSettings['countInARun']; |
|
|
|
|
82
|
8 |
|
$this->processLimit = (int) $this->extensionSettings['processLimit']; |
|
|
|
|
83
|
8 |
|
$this->verbose = (bool) $this->extensionSettings['processVerbose']; |
|
|
|
|
84
|
8 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* starts new process |
88
|
|
|
* @throws ProcessException if no crawler process was started |
89
|
|
|
*/ |
90
|
1 |
|
public function startProcess(): bool |
91
|
|
|
{ |
92
|
1 |
|
$ttl = (time() + $this->timeToLive - 1); |
93
|
1 |
|
$current = $this->processRepository->countNotTimeouted($ttl); |
94
|
|
|
|
95
|
|
|
// Check whether OS is Windows |
96
|
1 |
|
if (Environment::isWindows()) { |
97
|
|
|
$completePath = 'start ' . $this->getCrawlerCliPath(); |
98
|
|
|
} else { |
99
|
1 |
|
$completePath = '(' . $this->getCrawlerCliPath() . ' &) > /dev/null'; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$fileHandler = CommandUtility::exec($completePath); |
103
|
1 |
|
if ($fileHandler === false) { |
104
|
|
|
throw new ProcessException('could not start process!'); |
105
|
|
|
} |
106
|
1 |
|
for ($i = 0; $i < 10; $i++) { |
107
|
1 |
|
if ($this->processRepository->countNotTimeouted($ttl) > $current) { |
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
sleep(1); |
111
|
|
|
} |
112
|
|
|
throw new ProcessException('Something went wrong: process did not appear within 10 seconds.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the path to start the crawler from the command line |
117
|
|
|
*/ |
118
|
7 |
|
public function getCrawlerCliPath(): string |
119
|
|
|
{ |
120
|
7 |
|
$phpPath = PhpBinaryUtility::getPhpBinary(); |
121
|
7 |
|
$typo3BinaryPath = ExtensionManagementUtility::extPath('core') . 'bin/'; |
122
|
7 |
|
$cliPart = 'typo3 crawler:processQueue'; |
123
|
|
|
// Don't like the spacing, but don't have an better idea for now |
124
|
7 |
|
$scriptPath = $phpPath . ' ' . $typo3BinaryPath . $cliPart; |
125
|
|
|
|
126
|
7 |
|
if (Environment::isWindows()) { |
127
|
|
|
$scriptPath = str_replace('/', '\\', $scriptPath); |
128
|
|
|
} |
129
|
|
|
|
130
|
7 |
|
return ltrim($scriptPath); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|