-
Notifications
You must be signed in to change notification settings - Fork 1
/
data-loop.js
128 lines (97 loc) · 2.47 KB
/
data-loop.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
124
125
126
127
128
import { updateWorldJSON, checkIfServerIsUp } from "./data.js";
import { updateStaffJSON } from "./staff.js";
import { getServers, getServerByName } from "./server.js";
import { trackAverage } from "./average.js";
import { muted, success, warning, info as _info } from "./colors.js";
async function worldJSON(serverName) {
const server = getServerByName(serverName);
let timeout = 1000;
if (!server.down && !server.failed) {
const start = Date.now();
try {
const clientData = await updateWorldJSON(server);
process.send({
type: "world",
data: clientData,
});
} catch (e) {
server.down = true;
server.downError = e.message;
console.error(`${warning("Failed to load world.json")} ${_info(String(server.url))}: ${muted(e)}`);
}
const took = Date.now() - start;
trackAverage("world", took);
timeout = Math.max(0, 1000 - took);
} else {
timeout = 5000;
}
setTimeout(() => {
worldJSON(serverName);
}, timeout);
}
async function staffJSON(serverName) {
const server = getServerByName(serverName);
let timeout = 3000;
if (!server.down && !server.failed) {
const start = Date.now();
try {
const clientData = await updateStaffJSON(server);
process.send({
type: "staff",
data: clientData,
});
} catch (e) {
server.down = true;
server.downError = e.message;
console.error(`${warning("Failed to load staffChat.json")} ${_info(String(server.url))}: ${muted(e)}`);
}
const took = Date.now() - start;
trackAverage("staff", took);
timeout = Math.max(0, 3000 - took);
} else {
timeout = 5000;
}
setTimeout(() => {
staffJSON(serverName);
}, timeout);
}
async function downChecker(serverName) {
const server = getServerByName(serverName),
info = await checkIfServerIsUp(server);
if (info) {
server.info = info;
if (server.down) {
console.error(`${success("Server back up")} ${_info(server.server)}`);
server.down = false;
server.downError = null;
}
}
}
export function initDataLoop() {
const servers = getServers();
for (const serverName in servers) {
// Stagger
runIntervalDelayed(
() => {
downChecker(serverName);
},
1000,
10000
);
setTimeout(() => {
worldJSON(serverName);
}, 2000);
setTimeout(() => {
staffJSON(serverName);
}, 3000);
}
}
export function isValidType(type) {
return type && ["world", "staff"].includes(type);
}
function runIntervalDelayed(fn, delay, interval) {
setTimeout(() => {
fn();
setInterval(fn, interval);
}, delay);
}