atomic update

master
Dirk Heilig 2024-04-19 14:52:11 +02:00
parent 5f188442a1
commit d4fce11f5a
1 changed files with 3 additions and 2 deletions

View File

@ -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 = [];