Commit d9749b74e38bd25853e92f123f28e2cff1bd1bc8
1 parent
b2a8356006
Exists in
master
Add kill-on-idle
Showing 5 changed files with 63 additions and 0 deletions Side-by-side Diff
config.sample.json
lib/kill-on-idle.js
... | ... | @@ -0,0 +1,50 @@ |
1 | +const MODULE_NAME = 'KILL-ON-IDLE'; | |
2 | + | |
3 | +const moment = require('moment'); | |
4 | +const config = require('komodo-sdk/config'); | |
5 | +const logger = require('tektrans-logger'); | |
6 | + | |
7 | +const maxIdleMs = (config.kill_on_idle && config.kill_on_idle.max_idle_ms) || 60 * 1000; | |
8 | + | |
9 | +let lastIncomingTs = new Date(); | |
10 | + | |
11 | +const touch = () => { | |
12 | + lastIncomingTs = new Date(); | |
13 | +}; | |
14 | +exports.touch = touch; | |
15 | + | |
16 | +const getDisabled = () => !config.kill_on_idle || config.kill_on_idle.disable; | |
17 | +exports.getDisabled = getDisabled; | |
18 | + | |
19 | +const killOnIdle = () => { | |
20 | + if (getDisabled()) { | |
21 | + return; | |
22 | + } | |
23 | + | |
24 | + const ageMs = new Date() - lastIncomingTs; | |
25 | + | |
26 | + if (ageMs > maxIdleMs) { | |
27 | + logger.warn(`${MODULE_NAME} 74A43DF4: Idle deadline exceeded. Terminating`, { | |
28 | + lastIncomingTs: moment(lastIncomingTs).format('YYYY-MM-DD HH:mm:ss'), | |
29 | + ageMs, | |
30 | + maxIdleMs, | |
31 | + }); | |
32 | + } | |
33 | + | |
34 | + process.exit(1); | |
35 | +}; | |
36 | + | |
37 | +const init = () => { | |
38 | + if (getDisabled()) { | |
39 | + return; | |
40 | + } | |
41 | + | |
42 | + logger.verbose(`${MODULE_NAME} CF75F9CE: Registering kill on idle checker`, { | |
43 | + maxIdleMs, | |
44 | + }); | |
45 | + | |
46 | + setInterval(() => { | |
47 | + killOnIdle(); | |
48 | + }, 2 * 1000); | |
49 | +}; | |
50 | +exports.init = init; |
lib/transport.js
... | ... | @@ -8,6 +8,7 @@ const messagingService = require('komodo-center-messaging-client-lib'); |
8 | 8 | const uniqid = require('uniqid'); |
9 | 9 | |
10 | 10 | const customPing = require('./custom-ping'); |
11 | +const killOnIdle = require('./kill-on-idle'); | |
11 | 12 | |
12 | 13 | let isReady; |
13 | 14 | |
... | ... | @@ -18,6 +19,10 @@ bot.on('online', (data) => { |
18 | 19 | customPing.setBot(bot); |
19 | 20 | } |
20 | 21 | |
22 | + if (!killOnIdle.getDisabled()) { | |
23 | + killOnIdle.init(); | |
24 | + } | |
25 | + | |
21 | 26 | bot.getRoster(); |
22 | 27 | |
23 | 28 | setTimeout( |
... | ... | @@ -46,6 +51,8 @@ bot.on('chat', (partner, msg) => { |
46 | 51 | return; |
47 | 52 | } |
48 | 53 | |
54 | + killOnIdle.touch(); | |
55 | + | |
49 | 56 | const xid = uniqid(); |
50 | 57 | |
51 | 58 | if (customPing.isPongMessage(msg)) { |
package-lock.json
... | ... | @@ -11,6 +11,7 @@ |
11 | 11 | "dependencies": { |
12 | 12 | "komodo-center-messaging-client-lib": "git+https://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git", |
13 | 13 | "komodo-sdk": "^1.43.8", |
14 | + "moment": "^2.29.1", | |
14 | 15 | "simple-xmpp": "^1.3.1", |
15 | 16 | "tektrans-logger": "^1.2.2", |
16 | 17 | "uniqid": "^5.4.0" |
package.json
... | ... | @@ -28,6 +28,7 @@ |
28 | 28 | "dependencies": { |
29 | 29 | "komodo-center-messaging-client-lib": "git+https://gitlab.kodesumber.com/komodo/komodo-center-messaging-client-lib.git", |
30 | 30 | "komodo-sdk": "^1.43.8", |
31 | + "moment": "^2.29.1", | |
31 | 32 | "simple-xmpp": "^1.3.1", |
32 | 33 | "tektrans-logger": "^1.2.2", |
33 | 34 | "uniqid": "^5.4.0" |