Software /
code /
prosody
Annotate
util/watchdog.lua @ 6097:538cdc3d8225
plugins/muc/muc.lib: Move (de)construct_stanza_id into `handle_iq_to_occupant`
It is the only place they were used; and I left the old function names in as comments.
One reason for doing this was to reduce accesses to _occupants; which may be in a database in future revisions
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Fri, 21 Feb 2014 17:17:01 -0500 |
parent | 4891:189cfe565d03 |
child | 6777:5de6b93d0190 |
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 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 module "watchdog" |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local watchdog_methods = {}; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local watchdog_mt = { __index = watchdog_methods }; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 function new(timeout, callback) |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local watchdog = setmetatable({ timeout = timeout, last_reset = os_time(), callback = callback }, watchdog_mt); |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 timer.add_task(timeout+1, function (current_time) |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local last_reset = watchdog.last_reset; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if not last_reset then |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 return; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 local time_left = (last_reset + timeout) - current_time; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if time_left < 0 then |
4891
189cfe565d03
util.watchdog: Pass watchdog object to callback so that it doesn't always have to be a closure
Matthew Wild <mwild1@gmail.com>
parents:
4401
diff
changeset
|
19 return watchdog:callback(); |
4401
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 return time_left + 1; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end); |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 return watchdog; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 function watchdog_methods:reset() |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 self.last_reset = os_time(); |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 function watchdog_methods:cancel() |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 self.last_reset = nil; |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 |
0ed617f58404
util.watchdog: Watchdog timer library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return _M; |