From 50c8015d39696408ef5f5c3da0b9bf4c1948d067 Mon Sep 17 00:00:00 2001 From: elenapan Date: Tue, 21 Apr 2020 01:06:18 +0300 Subject: [PATCH] Make remote_watch update exactly once during the specified interval --- config/awesome/helpers.lua | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/config/awesome/helpers.lua b/config/awesome/helpers.lua index 3a7b693..8c22502 100644 --- a/config/awesome/helpers.lua +++ b/config/awesome/helpers.lua @@ -386,26 +386,25 @@ end -- Useful for periodically checking the output of a command that -- requires internet access. --- Ensures that `command` will be run only once during the desired +-- Ensures that `command` will be run EXACTLY once during the desired -- `interval`, even if awesome restarts multiple times during this time. -- Saves output in `output_file` and checks its last modification -- time to determine whether to run the command again or not. -- Passes the output of `command` to `callback` function. function helpers.remote_watch(command, interval, output_file, callback) - local run_the_thing = function() -- Pass output to callback AND write it to file awful.spawn.easy_async_with_shell(command.." | tee "..output_file, function(out) callback(out) end) end - gears.timer { + local timer + timer = gears.timer { timeout = interval, call_now = true, autostart = true, single_shot = false, callback = function() awful.spawn.easy_async_with_shell("date -r "..output_file.." +%s", function(last_update, _, __, exitcode) - -- Probably the file does not exist yet (first time -- running after reboot) if exitcode == 1 then @@ -414,14 +413,21 @@ function helpers.remote_watch(command, interval, output_file, callback) end local diff = os.time() - tonumber(last_update) - if diff > interval then + if diff >= interval then run_the_thing() else -- Pass the date saved in the file since it is fresh enough awful.spawn.easy_async_with_shell("cat "..output_file, function(out) callback(out) end) + + -- Schedule an update for when the remaining time to complete the interval passes + timer:stop() + gears.timer.start_new(interval - diff, function() + run_the_thing() + timer:again() + end) end end) - end + end } end