|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPLRPM; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use RuntimeException; |
| 7 | +use TIPC\FileSystemUtils; |
| 8 | +use TIPC\SocketStreamClient; |
| 9 | +use TIPC\UnixDomainSocketAddress; |
| 10 | + |
| 11 | +class ConfigurationProcess |
| 12 | +{ |
| 13 | + public const DEFAULT_CONFIG_POLL_INTERVAL = 30; |
| 14 | + |
| 15 | + private const CONFIG_POLL_TIME_INIT = 0; |
| 16 | + |
| 17 | + private $configurationSource; |
| 18 | + private $config = []; |
| 19 | + private $configSocket; |
| 20 | + private $configPollIntervalSeconds; |
| 21 | + private $timeOfLastConfigPoll = self::CONFIG_POLL_TIME_INIT; |
| 22 | + private $client; |
| 23 | + |
| 24 | + public function findConfigSocket() |
| 25 | + { |
| 26 | + $this->configSocket = FileSystemUtils::findWritableFilePath( |
| 27 | + MessageService::CONFIG_SOCKET_FILE_NAME, |
| 28 | + IPCUtilities::getSocketDirs() |
| 29 | + ); |
| 30 | + if (is_null($this->configSocket)) { |
| 31 | + throw new RuntimeException('Config process could not find supervisor config Unix domain socket'); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public function __construct(string $configurationSourceClass, int $configPollIntervalSeconds) |
| 36 | + { |
| 37 | + $this->configurationSource = new $configurationSourceClass(); |
| 38 | + $this->configPollIntervalSeconds = $configPollIntervalSeconds; |
| 39 | + } |
| 40 | + |
| 41 | + private function installSignalHandlers(): void |
| 42 | + { |
| 43 | + fwrite(STDERR, '--> Config process installing signal handlers' . PHP_EOL); |
| 44 | + pcntl_signal(SIGHUP, function (int $signo, $_siginfo) { |
| 45 | + fwrite(STDERR, "--> Config process caught SIGHUP ($signo), will reload configuration" . PHP_EOL); |
| 46 | + $this->timeOfLastConfigPoll = self::CONFIG_POLL_TIME_INIT; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + public function runConfigurationProcessLoop($supervisorPid) |
| 51 | + { |
| 52 | + $this->installSignalHandlers(); |
| 53 | + $this->initClient(); |
| 54 | + fwrite(STDERR, "--> Signaling parent $supervisorPid that we are up and running" . PHP_EOL); |
| 55 | + posix_kill($supervisorPid, SIGUSR1); |
| 56 | + while (true) { |
| 57 | + $ppid = posix_getppid(); |
| 58 | + if ($ppid != $supervisorPid) { |
| 59 | + fwrite(STDERR, '--> Parent PID changed, config process exiting' . PHP_EOL); |
| 60 | + $this->shutdown(); |
| 61 | + } |
| 62 | + $haveNewConfig = $this->pollConfigurationSourceForChanges(); |
| 63 | + if ($haveNewConfig) { |
| 64 | + try { |
| 65 | + $this->sendConfigToSupervisor(); |
| 66 | + } catch (ConfigurationSendException $e) { |
| 67 | + fwrite(STDERR, '--> Could not send config to supervisor: ' . $e->getMessage() . PHP_EOL); |
| 68 | + } |
| 69 | + } |
| 70 | + if ($this->configPollIntervalSeconds > 0) { |
| 71 | + sleep($this->configPollIntervalSeconds); |
| 72 | + } |
| 73 | + pcntl_signal_dispatch(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private function shutdown(): void |
| 78 | + { |
| 79 | + $this->disconnectFromSupervisor(); |
| 80 | + exit(ExitCodes::EXIT_PPID_CHANGED); |
| 81 | + } |
| 82 | + |
| 83 | + private function pollConfigurationSourceForChanges(): bool |
| 84 | + { |
| 85 | + $haveNewConfig = false; |
| 86 | + $now = time(); |
| 87 | + if ($this->timeOfLastConfigPoll + $this->configPollIntervalSeconds <= $now) { |
| 88 | + fwrite(STDERR, '--> Polling configuration source' . PHP_EOL); |
| 89 | + $this->timeOfLastConfigPoll = $now; |
| 90 | + try { |
| 91 | + $newConfig = $this->configurationSource->loadConfiguration(); |
| 92 | + $haveNewConfig = static::isFresher($this->config, $newConfig); |
| 93 | + if ($haveNewConfig) { |
| 94 | + $this->config = $newConfig; |
| 95 | + } |
| 96 | + if (!$haveNewConfig) fwrite(STDERR, '--> No new config found' . PHP_EOL); |
| 97 | + } catch (Exception $e) { |
| 98 | + fwrite(STDERR, '--> Error loading configuration from source: ' . $e->getMessage() . PHP_EOL); |
| 99 | + } |
| 100 | + } |
| 101 | + return $haveNewConfig; |
| 102 | + } |
| 103 | + |
| 104 | + private static function isFresher(array $oldConfig, array $newConfig): bool |
| 105 | + { |
| 106 | + foreach ($newConfig as $newJobId => $newJobConfig) { |
| 107 | + if (array_key_exists($newJobId, $oldConfig)) { |
| 108 | + $oldJobConfig = $oldConfig[$newJobId]; |
| 109 | + if ($newJobConfig['mtime'] > $oldJobConfig['mtime']) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } else { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + foreach ($oldConfig as $oldJobId => $oldJobConfig) { |
| 117 | + if (!array_key_exists($oldJobId, $newConfig)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + private function initClient(): void |
| 125 | + { |
| 126 | + $this->findConfigSocket(); |
| 127 | + $recvBufSize = 4 * 1024; |
| 128 | + $this->client = new SocketStreamClient(new UnixDomainSocketAddress($this->configSocket), $recvBufSize); |
| 129 | + } |
| 130 | + |
| 131 | + private function disconnectFromSupervisor(): void |
| 132 | + { |
| 133 | + if (!is_null($this->client) && $this->client->isConnected()) { |
| 134 | + $this->client->disconnect(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private function sendConfigToSupervisor(): void |
| 139 | + { |
| 140 | + if (!$this->client->isConnected() && $this->client->connect() === false) { |
| 141 | + throw new RuntimeException("Could not connect to socket {$this->configSocket}"); |
| 142 | + } |
| 143 | + fwrite(STDERR, '--> Sending new configuration to supervisor' . PHP_EOL); |
| 144 | + $msg = Serialization::serialize($this->config); |
| 145 | + if ($this->client->sendMessage($msg) === false) { |
| 146 | + $this->client->disconnect(); |
| 147 | + throw new ConfigurationSendException("Could not send config over socket {$this->configSocket}"); |
| 148 | + } |
| 149 | + if (empty($response = $this->client->receiveMessage())) { |
| 150 | + $this->client->disconnect(); |
| 151 | + throw new ConfigurationSendException("Failed to read response from {$this->configSocket}"); |
| 152 | + } |
| 153 | + if ($response !== ConfigurationMessageHandler::RESP_OK) { |
| 154 | + throw new ConfigurationSendException("Supervisor at {$this->configSocket} did not acknowledge new config"); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments