fix(nix-monitor): add cancel system stats check

This commit is contained in:
avivbintangaringga
2026-07-15 11:37:35 +07:00
parent 0d953ea633
commit 74f22f2c34
+27 -11
View File
@@ -21,6 +21,17 @@ function watchState(key, cb)
noctalia.state.watch(key, cb) noctalia.state.watch(key, cb)
end end
function killWithPidFile(file)
if noctalia.fileExists(file) then
local pid = noctalia.string.trim(noctalia.readFile(file))
if type(pid) == "string" and pid ~= "" then
noctalia.runAsync(`pkill -9 -P {pid}`)
end
noctalia.removeFile(file)
end
end
-- Vars -- Vars
local branch = cfg("branch") local branch = cfg("branch")
local updateCheckInterval = cfg("check_interval") local updateCheckInterval = cfg("check_interval")
@@ -28,9 +39,11 @@ local checkDurationThreshold = cfg("check_duration_threshold")
local systemStatsCheckInterval = cfg("system_stats_check_interval") local systemStatsCheckInterval = cfg("system_stats_check_interval")
local stateDir = "/tmp/nix-monitor" local stateDir = "/tmp/nix-monitor"
local remoteHashFile = stateDir .. "/remoteHash" local remoteHashFile = stateDir .. "/remoteHash"
local remotePidFile = stateDir .. "/remoteHash.pid"
local storeSizeFile = stateDir .. "/storeSize" local storeSizeFile = stateDir .. "/storeSize"
local closureSizeFile = stateDir .. "/closureSize" local closureSizeFile = stateDir .. "/closureSize"
local remotePidFile = stateDir .. "/remoteHash.pid"
local storePidFile = stateDir .. "/storeSize.pid"
local closurePidFile = stateDir .. "/closureSize.pid"
local showUpdateNotification = true local showUpdateNotification = true
local localHashCmd = "nixos-version --hash 2> /dev/null | cut -c -7" local localHashCmd = "nixos-version --hash 2> /dev/null | cut -c -7"
@@ -111,18 +124,12 @@ function onRemoteHashCheckCompleted()
setState("lastRemoteCheckTime", os.time()) setState("lastRemoteCheckTime", os.time())
noctalia.runAsync("date '+%Y-%m-%d %H:%M'", function(date) setState("lastRemoteCheckTimeStr", date.stdout) end) noctalia.runAsync("date '+%Y-%m-%d %H:%M'", function(date) setState("lastRemoteCheckTimeStr", date.stdout) end)
noctalia.removeFile(remotePidFile)
end end
function cancelRemoteCheck() function cancelRemoteCheck()
setState("isRemoteCheckRunning", false) setState("isRemoteCheckRunning", false)
if noctalia.fileExists(remotePidFile) then killWithPidFile(remotePidFile)
local pid = noctalia.string.trim(noctalia.readFile(remotePidFile))
if type(pid) == "string" and pid ~= "" then
noctalia.runAsync(`pkill -9 -P {pid}`)
end
noctalia.removeFile(remotePidFile)
end
end end
-- Generation check -- Generation check
@@ -157,8 +164,9 @@ function checkSystemStats()
setState("isSystemStatsCheckRunning", true) setState("isSystemStatsCheckRunning", true)
setState("lastSystemStatsCheckTime", os.time()) setState("lastSystemStatsCheckTime", os.time())
noctalia.runAsync(`{storeSizeCmd} > {storeSizeFile} &`) -- Sleep 0 to trigger noctalia calling /bin/sh
noctalia.runAsync(`{closureSizeCmd} > {closureSizeFile} &`) noctalia.runAsync(`sleep 0 && {storeSizeCmd} > {storeSizeFile} & echo $! > {storePidFile}`)
noctalia.runAsync(`sleep 0 && {closureSizeCmd} > {closureSizeFile} & echo $! > {closurePidFile}`)
end end
function checkSystemStatsFile() function checkSystemStatsFile()
@@ -178,9 +186,16 @@ function checkSystemStatsFile()
if success then if success then
setState("isSystemStatsCheckRunning", false) setState("isSystemStatsCheckRunning", false)
noctalia.removeFile(storePidFile)
noctalia.removeFile(closurePidFile)
end end
end end
function cancelSystemCheck()
setState("isSystemStatsCheckRunning", false)
killWithPidFile(storePidFile)
killWithPidFile(closurePidFile)
end
-- Init -- Init
checkLocalHash() checkLocalHash()
@@ -198,6 +213,7 @@ watchState("command", function(command)
if command == "cancel" then if command == "cancel" then
cancelRemoteCheck() cancelRemoteCheck()
cancelSystemCheck()
end end
end) end)