-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchildProcess.js
123 lines (113 loc) · 3.63 KB
/
childProcess.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const tools = require('./tools');
const Livescore = require('./hltv-livescore');
const args = process.argv[2];
const CircularJSON = require('circular-json');
const timers = require('./timers');
var self = this; // 'this', the child process
self.time = 0;
self.interval; // time remaining.
var maxInactive = timers["MAX_INACTIVE"]; // loops
var tick = timers["ALERT_EVERY_LOOP"]; // check every Nth loop. tick < loopEvery / 2
var oldMessage = '';
var currentGames = args.split(',').map(Number).filter(Boolean); // has to be INT array
var finishedGames = [];
const origGames = currentGames;
/*
childProcess asks for list of current game in every loop of the inactivity timer,
Any finished games are removed until current games is empty. then origGames is reported
back to serverParent.
*/
// call this whenever something happens. emit periodically. if inactive, exits.
var setInactivityTimer = function(time) {
clearInterval(self.interval);
self.time = time;
// "Infinite Loop" Execution ~ setInterval().
self.interval = setInterval(() => {
var _s;
self.time = self.time - 1;
_s = Number(self.time);
if (_s % tick === 0 && _s > -1) {
// request new array of current games
try {
process.send('current_games');
}
catch (e) {
console.log('childProcess exiting: error on current_games', e);
process.exit(1);
}
// report back and exit if finished
if (_s <= 0) {
try {
process.send(origGames + ' exiting due to inactivity');
}
catch (e) {
console.log('childProcess exiting: error on exiting', e);
process.exit(1);
}
process.exit(1); // child self-destructs
}
else {
try {
process.send(origGames + ' inactive time remaining ' + self.time);
}
catch (e) {
console.log('childProcess exiting: error on time remaining', e);
process.exit(1);
}
}
}
}, 1000); // fixed at 1 sec.
};
// new array of current games, expects format '{ "currentGames": [] }'
process.on('message', (msg) => {
// compare the two arrays, when none is left of the original set, exit.
try {
var _arr = JSON.parse(msg);
finishedGames = tools.leftDisjoin(origGames, _arr["currentGames"]);
currentGames = tools.leftDisjoin(origGames, finishedGames);
}
catch (e) {
console.log('childProcess exiting: error on parse message', e);
process.exit(1);
}
if (currentGames.length === 0) {
try {
process.send(origGames + ' exiting, all games finished');
process.send('process_exit'); // all list_id's may now be removed from childArray
process.exit(1);
}
catch (e) {
console.log('childProcess exiting: error on finished', e);
process.exit(1);
}
}
else {
try {process.send(currentGames + ' still running');}
catch (e) {
console.log('childProcess exiting: error on still running', e);
process.exit(1);
}
}
});
try {
process.send('Launching new child process, currentGames = ' + currentGames);
}
catch (e) {
console.log('childProcess exiting: error on Launching', e);
process.exit(1);
}
var live = new Livescore({
gamesList: currentGames
});
// raw data from socketio-wildcard
live.on('raw', function(data) {
if (oldMessage != data) {
try {process.send(CircularJSON.stringify(data, null, 2));}
catch (e) {
console.log('childProcess exiting: error on raw message', e);
process.exit(1);
}
oldMessage = data;
setInactivityTimer(maxInactive);
}
});