IMPORTANT: due to a drive failure, as of 13-Mar-2021, the Mercurial repository had to be re-mirrored, which changed every commit SHA. The old SHAs and trees are backed up in the vault branches. Please migrate to the new branches as soon as you can.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
prosody/util/watchdog.lua

44 lines
848 B

local timer = require "prosody.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(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
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;
};