forked from PartTimeJS/WDR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wdr.js
79 lines (68 loc) · 2.39 KB
/
wdr.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
//------------------------------------------------------------------------------
// PACKAGE REQUIREMENTS
//------------------------------------------------------------------------------
const fs = require("fs");
const os = require("os");
const ini = require("ini");
const express = require("express");
const cluster = require("cluster");
const bodyParser = require("body-parser");
const config = ini.parse(fs.readFileSync("./config/config.ini", "utf-8"));
var avg_objs, objs = [], requests = 0;
//------------------------------------------------------------------------------
// MASTER PROCESS
//------------------------------------------------------------------------------
if (cluster.isMaster) {
let cpu_count = "";
if(config.Child_Processes){
cpu_count = config.Child_Processes;
} else {
cpu_count = 4;
}
console.info("[wdr.js] Now Listening on port "+config.LISTENING_PORT+".");
// SPAWN A FORK FOR EACH CORE/THREAD AVAILABLE
const cpuCount = os.cpus().length;
for (let i = 0; i < cpu_count; i++) {
cluster.fork({ "fork": i });
}
// RESTART FORK ON EXIT
cluster.on("exit", (worker, code, signal) => {
console.error("[wdr.js] Worker Died. Respawning...",worker);
process.exit(1);
});
//------------------------------------------------------------------------------
// CHILD PROCESSES
//------------------------------------------------------------------------------
} else {
if(config.DEBUG.Webhook_Load){
Start_Intervals();
}
// LOAD MAIN FOR EACH PROCESS (MANDATORY)
const MAIN = require("./modules/base/bot.js");
// DEFINE THE EXPRESS SERVER
var server = express().use(express.json({ limit: "1mb" }));
// CATCH REQUESTS AND SEND FOR PARSING
server.post("/", async (webhook, resolve) => {
requests++;
if(objs.length < 20){
objs.push(webhook.body.length);
} else {
objs.slice(1);
objs.push(webhook.body.length);
}
return MAIN.webhookParse(webhook.body);
});
// LISTEN TO THE SPECIFIED PORT FOR TRAFFIC
server.listen(config.LISTENING_PORT);
}
function Start_Intervals(){
setInterval( async function() {
let total = 0;
await objs.forEach((data,index) => { total = total + data; });
avg_objs = total / objs.length;
}, 60000 * 1);
setInterval(function() {
console.log("[wdr.js] Process: "+process.env.fork+" | Requests: "+requests+" | Avg Objects: "+avg_objs);
requests = 0;
}, 60000 * 15);
}