|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Netlogix\JobQueue\FastRabbit\Command; |
| 5 | + |
| 6 | +use Flowpack\JobQueue\Common\Queue\QueueManager; |
| 7 | +use Neos\Flow\Annotations as Flow; |
| 8 | +use Neos\Flow\Configuration\ConfigurationManager; |
| 9 | +use Neos\Flow\Configuration\Exception\InvalidConfigurationTypeException; |
| 10 | +use Neos\Flow\Configuration\Exception\SchemaValidationException; |
| 11 | +use Neos\Flow\Core\Booting\Scripts; |
| 12 | +use Neos\Flow\ObjectManagement\ObjectManagerInterface; |
| 13 | +use Neos\Flow\Reflection\ReflectionService; |
| 14 | +use Neos\Utility\Files; |
| 15 | +use Netlogix\JobQueue\FastRabbit\Job\ConfigurationFactory; |
| 16 | +use Netlogix\JobQueue\FastRabbit\Queues\Locator; |
| 17 | + |
| 18 | +class SupervisorCommandController extends \Neos\Flow\Cli\CommandController |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var ObjectManagerInterface |
| 22 | + * @Flow\Inject |
| 23 | + */ |
| 24 | + protected $objectManager; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var QueueManager |
| 28 | + * @Flow\Inject |
| 29 | + */ |
| 30 | + protected $queueManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ConfigurationManager |
| 34 | + * @Flow\Inject |
| 35 | + */ |
| 36 | + protected $configurationManager; |
| 37 | + |
| 38 | + /** |
| 39 | + * @Flow\CompileStatic |
| 40 | + * @param ObjectManagerInterface $objectManager |
| 41 | + * @return array |
| 42 | + */ |
| 43 | + public static function collectLocatorNames( |
| 44 | + ObjectManagerInterface $objectManager |
| 45 | + ): array { |
| 46 | + $reflectionService = $objectManager->get(ReflectionService::class); |
| 47 | + $locatorNames = $reflectionService->getAllImplementationClassNamesForInterface(Locator::class); |
| 48 | + return array_values($locatorNames); |
| 49 | + } |
| 50 | + |
| 51 | + public function createCommand(): void |
| 52 | + { |
| 53 | + $this->createSupervisorGroupConfigCommand(); |
| 54 | + $queueNames = $this->collectQueueNames(); |
| 55 | + foreach ($queueNames as $queueName) { |
| 56 | + $this->createSupervisorProcessConfigCommand($queueName); |
| 57 | + $this->createWorkerConfigCommand($queueName); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function createSupervisorGroupConfigCommand(): void |
| 65 | + { |
| 66 | + $pathPrefix = FLOW_PATH_CONFIGURATION . 'Supervisor/'; |
| 67 | + Files::createDirectoryRecursively($pathPrefix); |
| 68 | + foreach (glob($pathPrefix . 'program-*.conf') as $configFile) { |
| 69 | + unlink($configFile); |
| 70 | + } |
| 71 | + |
| 72 | + $factory = new ConfigurationFactory(); |
| 73 | + |
| 74 | + $queueNames = self::collectQueueNames($this->objectManager); |
| 75 | + |
| 76 | + $groupConfiguration = $factory->buildGroupConfigurationForQueues(... $queueNames); |
| 77 | + $groupFilePath = $pathPrefix . 'group.conf'; |
| 78 | + file_put_contents($groupFilePath, $groupConfiguration); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return void |
| 83 | + */ |
| 84 | + public function createSupervisorProcessConfigCommand(string $queueName): void |
| 85 | + { |
| 86 | + $factory = new ConfigurationFactory(); |
| 87 | + $jobConfiguration = $factory->buildJobConfigurationForQueue($queueName); |
| 88 | + |
| 89 | + $jobFilePath = $factory->getJobSupervisorFile($queueName); |
| 90 | + file_put_contents($jobFilePath, $jobConfiguration); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param string $queueName |
| 95 | + * @throws \Flowpack\JobQueue\Common\Exception |
| 96 | + * @throws InvalidConfigurationTypeException |
| 97 | + * @throws \Neos\Flow\Exception |
| 98 | + */ |
| 99 | + public function createWorkerConfigCommand(string $queueName): void |
| 100 | + { |
| 101 | + $command = Scripts::buildPhpCommand( |
| 102 | + $this->configurationManager->getConfiguration('Settings', 'Neos.Flow') |
| 103 | + ); |
| 104 | + $command .= sprintf( |
| 105 | + ' %s %s --queue=%s', |
| 106 | + escapeshellarg(FLOW_PATH_FLOW . 'Scripts/flow.php'), |
| 107 | + escapeshellarg('flowpack.jobqueue.common:job:execute'), |
| 108 | + escapeshellarg($queueName) |
| 109 | + ); |
| 110 | + |
| 111 | + $jobConfig = [ |
| 112 | + 'command' => $command, |
| 113 | + 'queueSettings' => $this->queueManager->getQueueSettings($queueName), |
| 114 | + 'cacheConfiguration' => $this->configurationManager->getConfiguration( |
| 115 | + ConfigurationManager::CONFIGURATION_TYPE_CACHES, |
| 116 | + 'FlowPackJobQueueCommon_MessageCache' |
| 117 | + ), |
| 118 | + 'queueName' => $queueName, |
| 119 | + 'temporaryDirectoryBase' => FLOW_PATH_TEMPORARY_BASE, |
| 120 | + 'applicationIdentifier' => $this->configurationManager->getConfiguration( |
| 121 | + ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, |
| 122 | + 'Neos.Flow.cache.applicationIdentifier' |
| 123 | + ), |
| 124 | + 'contextString' => $this->configurationManager->getConfiguration( |
| 125 | + ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, |
| 126 | + 'Neos.Flow.core.context' |
| 127 | + ), |
| 128 | + 'workerPool' => $this->configurationManager->getConfiguration( |
| 129 | + ConfigurationManager::CONFIGURATION_TYPE_SETTINGS, |
| 130 | + 'Netlogix.JobQueue.FastRabbit.supervisor.workerPool' |
| 131 | + ), |
| 132 | + ]; |
| 133 | + |
| 134 | + $factory = new ConfigurationFactory(); |
| 135 | + $jobFilePath = $factory->getJobConfigurationFile($queueName); |
| 136 | + file_put_contents($jobFilePath, json_encode($jobConfig, JSON_PRETTY_PRINT)); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @return string[] |
| 141 | + * @throws SchemaValidationException |
| 142 | + */ |
| 143 | + protected function collectQueueNames(): array |
| 144 | + { |
| 145 | + $locatorNames = self::collectLocatorNames($this->objectManager); |
| 146 | + $queueNames = []; |
| 147 | + foreach ($locatorNames as $locatorName) { |
| 148 | + $locator = $this->objectManager->get($locatorName); |
| 149 | + assert($locator instanceof Locator); |
| 150 | + foreach ($locator as $queueName) { |
| 151 | + if (in_array($queueName, $queueNames, true)) { |
| 152 | + throw new SchemaValidationException( |
| 153 | + sprintf('Duplicate supervisor config found for queue "%s".', $queueName), |
| 154 | + 1594829585 |
| 155 | + ); |
| 156 | + } |
| 157 | + $queueNames[$queueName] = $queueName; |
| 158 | + } |
| 159 | + } |
| 160 | + return array_values($queueNames); |
| 161 | + } |
| 162 | +} |
0 commit comments