From 49a9d5e4274c26272761656d3fdaa6b2509c90f5 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 11 Jun 2022 21:11:01 +0100 Subject: util.watchdog: Update to use "new" util.timer API When this module was written, it wasn't possible to cancel or reschedule a timer. Times have changed, and we should take advantage of those new methods. This module becomes a very thin wrapper around util.timer now, but I'd argue it's still a very common and useful concept/abstraction to have around. Possible API change: this removes the 'last_reset' field of the watchdog. This was never really intended as a public thing, and I can't find any code that uses it, so I consider removal to be safe. --- util/watchdog.lua | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'util/watchdog.lua') diff --git a/util/watchdog.lua b/util/watchdog.lua index 516e60e4..1d30f22b 100644 --- a/util/watchdog.lua +++ b/util/watchdog.lua @@ -9,27 +9,30 @@ local watchdog_methods = {}; local watchdog_mt = { __index = watchdog_methods }; local function new(timeout, callback) - local watchdog = setmetatable({ timeout = timeout, last_reset = os_time(), callback = callback }, watchdog_mt); - timer.add_task(timeout+1, function (current_time) - local last_reset = watchdog.last_reset; - if not last_reset then - return; - end - local time_left = (last_reset + timeout) - current_time; - if time_left < 0 then - return watchdog:callback(); - end - return time_left + 1; + local watchdog = setmetatable({ + timeout = timeout; + callback = callback; + timer_id = nil; + }, watchdog_mt); + + watchdog.timer_id = timer.add_task(timeout+1, function () + return watchdog:callback(); end); + return watchdog; end function watchdog_methods:reset() - self.last_reset = os_time(); + if self.timer_id then + timer.reschedule(self.timer_id, self.timeout); + end end function watchdog_methods:cancel() - self.last_reset = nil; + if self.timer_id then + timer.stop(self.timer_id); + self.timer_id = nil; + end end return { -- cgit v1.2.3 From 029e0934783a60cf0be8c08e982440205fa9d337 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 11 Jun 2022 22:15:14 +0100 Subject: util.watchdog: Allow :reset() to restart a cancelled watchdog --- util/watchdog.lua | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'util/watchdog.lua') diff --git a/util/watchdog.lua b/util/watchdog.lua index 1d30f22b..6eb2e602 100644 --- a/util/watchdog.lua +++ b/util/watchdog.lua @@ -1,6 +1,5 @@ local timer = require "util.timer"; local setmetatable = setmetatable; -local os_time = os.time; local _ENV = nil; -- luacheck: std none @@ -15,16 +14,18 @@ local function new(timeout, callback) timer_id = nil; }, watchdog_mt); - watchdog.timer_id = timer.add_task(timeout+1, function () - return watchdog:callback(); - end); + watchdog:reset(); -- Kick things off return watchdog; end function watchdog_methods:reset() if self.timer_id then - timer.reschedule(self.timer_id, self.timeout); + timer.reschedule(self.timer_id, self.timeout+1); + else + self.timer_id = timer.add_task(self.timeout+1, function () + return self:callback(); + end); end end -- cgit v1.2.3 From 7532eac0e5e4cd51a765498bdcca2d52b0b3ca7a Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 11 Jun 2022 22:15:31 +0100 Subject: util.watchdog: Allow passing a new timeout to :reset() --- util/watchdog.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'util/watchdog.lua') diff --git a/util/watchdog.lua b/util/watchdog.lua index 6eb2e602..407028a5 100644 --- a/util/watchdog.lua +++ b/util/watchdog.lua @@ -19,7 +19,10 @@ local function new(timeout, callback) return watchdog; end -function watchdog_methods:reset() +function watchdog_methods:reset(new_timeout) + if new_timeout then + self.timeout = new_timeout; + end if self.timer_id then timer.reschedule(self.timer_id, self.timeout+1); else -- cgit v1.2.3