Changeset

12546:e78b35574aae

util.watchdog: Allow :reset() to restart a cancelled watchdog
author Matthew Wild <mwild1@gmail.com>
date Sat, 11 Jun 2022 22:15:14 +0100
parents 12545:5059a639f61e
children 12547:e79c64b2dfed
files util/watchdog.lua
diffstat 1 files changed, 6 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/util/watchdog.lua	Sat Jun 11 21:11:01 2022 +0100
+++ b/util/watchdog.lua	Sat Jun 11 22:15:14 2022 +0100
@@ -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 @@
 		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