-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationFactory.php
More file actions
129 lines (109 loc) · 4.31 KB
/
ConfigurationFactory.php
File metadata and controls
129 lines (109 loc) · 4.31 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace Netlogix\JobQueue\FastRabbit\Job;
use Flowpack\JobQueue\Common\Queue\QueueManager;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Configuration\ConfigurationManager;
use Neos\Flow\Core\Booting\Scripts;
use Neos\Utility\ObjectAccess;
use Netlogix\JobQueue\Pool\ProcessFactory;
use function defined;
use function preg_replace;
use function rtrim;
use function sprintf;
use function strtolower;
use function trim;
class ConfigurationFactory
{
/**
* @var string
* @Flow\InjectConfiguration(package="Netlogix.JobQueue.FastRabbit", path="supervisor.applicationName")
*/
protected $applicationName;
/**
* @var QueueManager
*/
protected $queueManager;
/**
* @var ConfigurationManager
*/
private $configurationManager;
public function injectQueueManager(QueueManager $queueManager): void
{
$this->queueManager = $queueManager;
}
public function injectConfigurationManager(ConfigurationManager $configurationManager): void
{
$this->configurationManager = $configurationManager;
}
/**
* @param string $queueName
* @return array<string, mixed>
* @throws \Flowpack\JobQueue\Common\Exception
* @throws \Neos\Flow\Configuration\Exception\InvalidConfigurationTypeException
* @throws \Neos\Flow\Exception
*/
public function buildJobConfiguration(string $queueName): array
{
assert(defined('FLOW_PATH_FLOW'));
assert(defined('FLOW_PATH_TEMPORARY_BASE'));
$flowSettings = (array)$this->configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
'Neos.Flow'
);
$command = (new ProcessFactory)->buildSubprocessCommand();
$workerPool = (array)$this->configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
'Netlogix.JobQueue.FastRabbit.supervisor.workerPool'
);
$jobConfig = [
'command' => $command,
'queueSettings' => $this->queueManager->getQueueSettings($queueName),
'cacheConfiguration' => $this->configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_CACHES,
'FlowPackJobQueueCommon_MessageCache'
),
'queueName' => $queueName,
'temporaryDirectoryBase' => \FLOW_PATH_TEMPORARY_BASE,
'applicationIdentifier' => ObjectAccess::getPropertyPath($flowSettings, 'cache.applicationIdentifier'),
'contextString' => ObjectAccess::getPropertyPath($flowSettings, 'core.context'),
'workerPool' => $workerPool,
];
return $jobConfig;
}
public function getJobName(string $queueName): string
{
return self::removeInvalidCharactersFromSupervisorIdentifier($this->applicationName . '-' . $queueName);
}
public function getJobConfigurationFilePath(string $queueName): string
{
return $this->getJobFilePath($queueName, 'json');
}
public function getJobSupervisorFilePath(string $queueName): string
{
return $this->getJobFilePath($queueName, 'conf');
}
public function getNumberOfProcesses(string $queueName): int
{
$queueSettings = $this->queueManager->getQueueSettings($queueName);
return (int)(ObjectAccess::getPropertyPath($queueSettings, 'fastRabbit.numProcs') ?? 1);
}
protected function getJobFilePath(string $queueName, string $suffix): string
{
assert(defined('FLOW_PATH_CONFIGURATION'));
$jobName = $this->getJobName($queueName);
$pathPrefix = rtrim(FLOW_PATH_CONFIGURATION, '/') . '/Supervisor/';
return sprintf($pathPrefix . 'program-%s.%s', $jobName, $suffix);
}
private static function removeInvalidCharactersFromSupervisorIdentifier(string $subject): string
{
/**
* \p{xx} A character with the xx unicode property. https://php.net/manual/en/regexp.reference.escape.php
* \pL Every Letter https://php.net/manual/en/regexp.reference.unicode.php
*/
$cleanupPattern = '%[^\\pL\\d]+%iUum';
$subject = (string)preg_replace($cleanupPattern, '-', $subject);
$subject = trim($subject, '-');
return strtolower($subject);
}
}