Software /
code /
prosody
Annotate
util/watchdog.lua @ 12545:5059a639f61e
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.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 11 Jun 2022 21:11:01 +0100 |
parent | 8555:4f0f5b49bb03 |
child | 12546:e78b35574aae |
rev | line source |
---|---|
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local timer = require "util.timer"; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local setmetatable = setmetatable; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local os_time = os.time; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4891
diff
changeset
|
5 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
6 -- luacheck: std none |
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local watchdog_methods = {}; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local watchdog_mt = { __index = watchdog_methods }; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4891
diff
changeset
|
11 local function new(timeout, callback) |
12545
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
12 local watchdog = setmetatable({ |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
13 timeout = timeout; |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
14 callback = callback; |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
15 timer_id = nil; |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
16 }, watchdog_mt); |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
17 |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
18 watchdog.timer_id = timer.add_task(timeout+1, function () |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
19 return watchdog:callback(); |
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end); |
12545
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
21 |
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return watchdog; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 function watchdog_methods:reset() |
12545
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
26 if self.timer_id then |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
27 timer.reschedule(self.timer_id, self.timeout); |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
28 end |
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 function watchdog_methods:cancel() |
12545
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
32 if self.timer_id then |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
33 timer.stop(self.timer_id); |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
34 self.timer_id = nil; |
5059a639f61e
util.watchdog: Update to use "new" util.timer API
Matthew Wild <mwild1@gmail.com>
parents:
8555
diff
changeset
|
35 end |
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 |
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4891
diff
changeset
|
38 return { |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4891
diff
changeset
|
39 new = new; |
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
4891
diff
changeset
|
40 }; |