From d4fce11f5a949dd08b99184cf2fbebe9ad17ac3c Mon Sep 17 00:00:00 2001 From: Dirk Heilig Date: Fri, 19 Apr 2024 14:52:11 +0200 Subject: [PATCH] atomic update --- generator/generator.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/generator/generator.php b/generator/generator.php index c5a2fba..e67f6d1 100755 --- a/generator/generator.php +++ b/generator/generator.php @@ -35,7 +35,6 @@ $c->onMessage(function (\Mosquitto\Message $message) use (&$data, $conf) { " unknown\n"; die(1); } - var_dump($data); $data[$message->topic]["changed"] = time(); $out = file_get_contents(__DIR__ . "/template.json"); $out = preg_replace_callback( @@ -69,7 +68,9 @@ $c->onMessage(function (\Mosquitto\Message $message) use (&$data, $conf) { if (!is_dir("public")) { mkdir("public"); } - file_put_contents("public/spaceapi.json", $out); + file_put_contents("public/spaceapi.json.new", $out); + // ensure atomic update + rename("public/spaceapi.json.new", "public/spaceapi.json"); }); $c->connect($conf["mqtt"]["host"]); $data = [];