blob: 6eb2e602f3abc66d08b52cb84ee2abd3be4bd4e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
local timer = require "util.timer";
local setmetatable = setmetatable;
local _ENV = nil;
-- luacheck: std none
local watchdog_methods = {};
local watchdog_mt = { __index = watchdog_methods };
local function new(timeout, callback)
local watchdog = setmetatable({
timeout = timeout;
callback = callback;
timer_id = nil;
}, watchdog_mt);
watchdog:reset(); -- Kick things off
return watchdog;
end
function watchdog_methods:reset()
if self.timer_id then
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
function watchdog_methods:cancel()
if self.timer_id then
timer.stop(self.timer_id);
self.timer_id = nil;
end
end
return {
new = new;
};
|