From c3f550698de2951208ec74e96ae085548cae3079 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Tue, 25 Mar 2025 23:44:21 +0000 Subject: [PATCH] watch-outputs fix update logic it was only working by accident, when it worked, which was by no means all of the time note that we unconditionally perform the action (restart or whatever) once we've started and got the initial state of the outputs. That's because we have no idea whether the outputs changed in the interval between the controlled service initially starting and watch-outputs starting, so updates in that interval could be lost --- pkgs/watch-outputs/watch-outputs.fnl | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pkgs/watch-outputs/watch-outputs.fnl b/pkgs/watch-outputs/watch-outputs.fnl index 59ea7a0..38c427b 100644 --- a/pkgs/watch-outputs/watch-outputs.fnl +++ b/pkgs/watch-outputs/watch-outputs.fnl @@ -38,7 +38,6 @@ "/nix/store/s2" [["out1"] ["out2" "ifname"]]}} )) - (fn changed? [paths old-tree new-tree] (accumulate [changed? false _ path (ipairs paths)] @@ -52,8 +51,6 @@ (expect (not (changed? [["mtu"]] {:ifname "true"} {:ifname "false"}))) ) - - (fn do-action [action service] (case action :restart (system (%% "s6-svc -r /run/service/%s" service)) @@ -80,16 +77,19 @@ : controlled-service : action : watched-service - : paths } (parse-args arg)] + : paths } (parse-args arg) + services (open-services output-references)] (while true - (let [services (open-services output-references) - trees (collect [s _ (pairs services)] - (values s (s:output ".")))] - (wait-for-change services) - (each [service paths (pairs services)] - (let [new-tree (service:output ".")] - (when (changed? paths (. trees service) new-tree) - (do-action action controlled-service)))))))) + (when + (accumulate [changed false + service paths (pairs services)] + (let [new-tree (service:output ".")] + (if (changed? paths (or (. trees service) {}) new-tree) + (do (tset trees service new-tree) true) + changed))) + (do-action action controlled-service)) + (wait-for-change services)))) +