Commit 215b5d599536fb7d28c8d6282af777ab159290dc

Authored by Adhidarma Hadiwinoto
1 parent e0cc84916c
Exists in master

Fix process.exit position on killOnIdle

Showing 1 changed file with 4 additions and 2 deletions Inline Diff

1 const MODULE_NAME = 'KILL-ON-IDLE'; 1 const MODULE_NAME = 'KILL-ON-IDLE';
2 2
3 const moment = require('moment'); 3 const moment = require('moment');
4 const config = require('komodo-sdk/config'); 4 const config = require('komodo-sdk/config');
5 const logger = require('tektrans-logger'); 5 const logger = require('tektrans-logger');
6 6
7 const maxIdleMs = (config.kill_on_idle && config.kill_on_idle.max_idle_ms) || 60 * 1000; 7 const maxIdleMs = (config.kill_on_idle && config.kill_on_idle.max_idle_ms) || 60 * 1000;
8 8
9 let lastIncomingTs = new Date(); 9 let lastIncomingTs = new Date();
10 10
11 const touch = () => { 11 const touch = () => {
12 lastIncomingTs = new Date(); 12 lastIncomingTs = new Date();
13 }; 13 };
14 exports.touch = touch; 14 exports.touch = touch;
15 15
16 const getDisabled = () => !config.kill_on_idle || config.kill_on_idle.disable; 16 const getDisabled = () => !config.kill_on_idle || config.kill_on_idle.disable;
17 exports.getDisabled = getDisabled; 17 exports.getDisabled = getDisabled;
18 18
19 const killOnIdle = () => { 19 const killOnIdle = () => {
20 if (getDisabled()) { 20 if (getDisabled()) {
21 return; 21 return;
22 } 22 }
23 23
24 const ageMs = new Date() - lastIncomingTs; 24 const ageMs = new Date() - lastIncomingTs;
25 25
26 if (ageMs > maxIdleMs) { 26 if (ageMs > maxIdleMs) {
27 logger.warn(`${MODULE_NAME} 74A43DF4: Idle deadline exceeded. Terminating`, { 27 logger.warn(`${MODULE_NAME} 74A43DF4: Idle deadline exceeded. Terminating`, {
28 lastIncomingTs: moment(lastIncomingTs).format('YYYY-MM-DD HH:mm:ss'), 28 lastIncomingTs: moment(lastIncomingTs).format('YYYY-MM-DD HH:mm:ss'),
29 ageMs, 29 ageMs,
30 maxIdleMs, 30 maxIdleMs,
31 }); 31 });
32 }
33 32
34 process.exit(1); 33 process.exit(1);
34 }
35 }; 35 };
36 36
37 const init = () => { 37 const init = () => {
38 if (getDisabled()) { 38 if (getDisabled()) {
39 return; 39 return;
40 } 40 }
41 41
42 touch();
43
42 logger.verbose(`${MODULE_NAME} CF75F9CE: Registering kill on idle checker`, { 44 logger.verbose(`${MODULE_NAME} CF75F9CE: Registering kill on idle checker`, {
43 maxIdleMs, 45 maxIdleMs,
44 }); 46 });
45 47
46 setInterval(() => { 48 setInterval(() => {
47 killOnIdle(); 49 killOnIdle();
48 }, 2 * 1000); 50 }, 2 * 1000);
49 }; 51 };
50 exports.init = init; 52 exports.init = init;