-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathProcessService.php
114 lines (99 loc) · 3.75 KB
/
ProcessService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace AOE\Crawler\Service;
/*
* (c) 2022 Tomas Norre Mikkelsen <tomasnorre@gmail.com>
*
* This file is part of the TYPO3 Crawler Extension.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use AOE\Crawler\Domain\Repository\ProcessRepository;
use AOE\Crawler\Exception\ProcessException;
use AOE\Crawler\Helper\Sleeper\SleeperInterface;
use AOE\Crawler\Utility\PhpBinaryUtility;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Utility\CommandUtility;
/**
* @package AOE\Crawler\Service
* @ignoreAnnotation("noRector")
*
* @internal since v9.2.5
*/
class ProcessService
{
public function __construct(
private readonly ProcessRepository $processRepository,
private readonly SleeperInterface $sleeper
) {
}
/**
* starts new process
* @throws ProcessException if no crawler process was started
*/
public function startProcess(int $timeToLive = 300): bool
{
$ttl = (time() + $timeToLive - 1);
$current = $this->processRepository->countNotTimeouted($ttl);
// Check whether OS is Windows
if (Environment::isWindows()) {
$completePath = 'start ' . $this->getCrawlerCliPath();
} else {
$completePath = '(' . $this->getCrawlerCliPath() . ' &) > /dev/null';
}
$output = null;
$returnValue = 0;
CommandUtility::exec($completePath, $output, $returnValue);
if ($returnValue !== 0) {
throw new ProcessException('could not start process!');
}
for ($i = 0; $i < 10; $i++) {
if ($this->processRepository->countNotTimeouted($ttl) > $current) {
return true;
}
$this->sleeper->sleep(1);
}
throw new ProcessException('Something went wrong: process did not appear within 10 seconds.');
}
/**
* Returns the path to start the crawler from the command line
*/
public function getCrawlerCliPath(): string
{
$phpPath = PhpBinaryUtility::getPhpBinary();
$typo3BinaryPath = $this->getComposerBinPath();
$cliPart = 'typo3 crawler:processQueue';
// Don't like the spacing, but don't have an better idea for now
$scriptPath = $phpPath . ' ' . $typo3BinaryPath . $cliPart;
if (Environment::isWindows()) {
$scriptPath = str_replace('/', '\\', $scriptPath);
}
return ltrim($scriptPath);
}
private function getComposerBinPath(): ?string
{
if (!getenv('TYPO3_PATH_COMPOSER_ROOT')) {
return sprintf('%s/%s/', getenv('TYPO3_PATH_APP'), 'vendor/typo3/cms-cli');
}
// copied and modified from @see
// https://github.com/TYPO3/typo3/blob/8a9c80b9d85ef986f5f369f1744fc26a6b607dda/typo3/sysext/scheduler/Classes/Controller/SchedulerModuleController.php#L402
$composerJsonFile = getenv('TYPO3_PATH_COMPOSER_ROOT') . '/composer.json';
if (!file_exists($composerJsonFile) || !($jsonContent = file_get_contents($composerJsonFile))) {
return null;
}
$jsonConfig = @json_decode($jsonContent, true);
if (empty($jsonConfig) || !is_array($jsonConfig)) {
return null;
}
$vendorDir = trim($jsonConfig['config']['vendor-dir'] ?? 'vendor', '/');
$binDir = trim($jsonConfig['config']['bin-dir'] ?? $vendorDir . '/bin', '/');
return sprintf('%s/%s/', getenv('TYPO3_PATH_COMPOSER_ROOT'), $binDir);
}
}