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(-) 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