Reload WebSocket on control source changes
This commit is contained in:
@@ -260,9 +260,34 @@ final class ControlWebSocket implements MessageComponentInterface
|
||||
$address = getenv('CONTROL_WS_ADDRESS') ?: '127.0.0.1:8088';
|
||||
$loop = Loop::get();
|
||||
$app = new ControlWebSocket();
|
||||
$watchFiles = [
|
||||
__FILE__,
|
||||
__DIR__ . '/../public/api.php',
|
||||
__DIR__ . '/../config/config.php',
|
||||
];
|
||||
$watchSignature = static function (array $files): string {
|
||||
$parts = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$parts[] = is_file($file)
|
||||
? $file . ':' . filemtime($file) . ':' . filesize($file)
|
||||
: $file . ':missing';
|
||||
}
|
||||
|
||||
return implode('|', $parts);
|
||||
};
|
||||
$startedSignature = $watchSignature($watchFiles);
|
||||
|
||||
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastStatus());
|
||||
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastDmesg());
|
||||
$loop->addPeriodicTimer(15.0, static function () use ($watchFiles, $watchSignature, $startedSignature): void {
|
||||
if ($watchSignature($watchFiles) === $startedSignature) {
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite(STDOUT, 'Control WebSocket source changed; restarting' . PHP_EOL);
|
||||
exit(0);
|
||||
});
|
||||
|
||||
$socket = new SocketServer($address, [], $loop);
|
||||
$server = new IoServer(
|
||||
|
||||
Reference in New Issue
Block a user