-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.js
178 lines (151 loc) · 5.71 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
console.time(`Loading Node.js libraries.`);
global.Cluster = require("cluster");
global._ = require("underscore");
global.bn_graph = require("ngraph.graph");
global.bn_path = require("ngraph.path");
global.Canvas = require("canvas");
global.diacriticless = require("diacriticless");
global.Discord = require("discord.js");
global.fs = require("fs");
global.HTML = require("node-html-parser");
global.JSONPack = require("jsonpack");
global.opus = require("opusscript");
global.OS = require("os");
global.path = require("path");
global.SVG = require("convert-svg-to-png");
global.voice = require("@discordjs/voice");
global.WorkerThreads = require("worker_threads");
//Import Core Framework
global.FileManager = require("./core/file_manager");
FileManager.import("./startup");
//Import Core Functions - See startup.js for full directory
loadBotFiles();
//Start up main thread (command handling/UI)
if (Cluster.isMaster) {
console.log(`Bot instance started on Thread #1 on Core #1.`);
global.thread_type = 1;
//Main thread logic
threadOneHandler();
//Fetch number of cores
var core_amount = OS.cpus().length;
var thread_amount = (settings.threads) ? settings.threads : core_amount;
global.thread_two_workers = [];
global.thread_three_workers = [];
//Create thread; log to console and distribute 3 threads over available cores
log.info(`Creating Master Core. (Command Handling/UI)`);
log.info(`${core_amount} core(s) are available. ${thread_amount} Thread(s) have been specified in settings. Will utilise them as necessary.\n-`);
//Reserve this core for main command processing; distribute the others for data processing; map/file
for (var i = 0; i < thread_amount - 1; i++) {
var local_worker = Cluster.fork();
//Receive data from worker
local_worker.on("message", (data) => {
//Busy worker handling
if (data.busy_worker) {
for (var x = 0; x < thread_two_workers.length; x++)
if (thread_two_workers[x].id == data.busy_worker)
thread_two_workers[x].busy = true;
for (var x = 0; x < thread_three_workers.length; x++)
if (thread_three_workers[x].id == data.busy_worker)
thread_three_workers[x].busy = true;
} else if (data.free_worker) {
for (var x = 0; x < thread_two_workers.length; x++)
if (thread_two_workers[x].id == data.free_worker)
delete thread_two_workers[x].busy;
for (var x = 0; x < thread_three_workers.length; x++)
if (thread_three_workers[x].id == data.free_worker)
delete thread_three_workers[x].busy;
}
//If main_object is received, sync; but only for selected variables
if (data.main_object) {
global.main.round_count = data.main_object.round_count;
global.main.global = data.main_object.global;
global.main.provinces = data.main_object.provinces;
global.main.users = data.main_object.users;
}
syncMasterToWorker(data);
});
local_worker.on("exit", (code) => {
log.info(`Worker #${local_worker.id} stopped with exit code ${code}.`);
});
//Add the worker to thread_two or thread_three depending on parity
local_worker.send(getMasterObject());
local_worker.send({ client: true });
if (i % 2 == 0) {
//Pass global down to local_worker
local_worker.send({
type: 2,
ram_size: 1024 //1GB max RAM
});
thread_two_workers.push(local_worker);
} else {
//Pass global down to local_worker
local_worker.send({
type: 3,
ram_size: 1024 //1GB max RAM
});
thread_three_workers.push(local_worker);
}
}
log.info(`-\nAssigned ${thread_two_workers.length} core(s) to Thread #2.`);
log.info(`Assigned ${thread_three_workers.length} core(s) to Thread #3.`);
syncWorkersToMaster(); //Sync at start up just in case
} else {
//Other thread handling (Threads 2 and 3)
process.on("message", function (data) {
//Global error handling
process.on("unhandledRejection", (error) => {
//Log enabled only with debug mode
if (settings.debug_mode) {
log.error(`Unhandled promise rejection. ${error.toString()}`);
console.log(error);
}
});
//Update global variables (lookup, main) to main thread
if (data.client)
if (!global.client) {
log.debug(`Start client called on Worker #${Cluster.worker.id}!`);
global.client = startClient();
console.log(`Client logged in as: `, global.client);
}
if (data.backup_loaded)
global.backup_loaded = data.backup_loaded;
if (data.config)
global.config = data.config;
if (data.interfaces)
global.interfaces = data.interfaces;
if (data.load_maps)
loadMaps();
if (data.lookup)
global.lookup = data.lookup;
if (data.main)
global.main = data.main;
if (data.mapmodes)
global.mapmodes = data.mapmodes;
if (data.reserved)
global.reserved = data.reserved;
if (data.settings)
global.settings = data.settings;
if (data.worker_eval)
eval(data.worker_eval);
log.debug(`Worker #${Cluster.worker.id} received message from master!`);
//log.debug(`Mapmodes:`, mapmodes);
//Set up worker type
if (data.type) {
if (data.type == 2) {
global.thread_type = 2;
} else if (data.type == 3) {
global.thread_type = 3;
}
//Fetch CPU core
var cpu_index = process.env.cpuIndex || 0;
var cpu_info = OS.cpus()[cpu_index];
log.info(`Started Thread #${global.thread_type} type worker on Core #${cpu_index} (${cpu_info.model})`);
}
//Command handler
if (global.thread_type == 2) {
threadTwoHandler(data);
} else if (global.thread_type == 3) {
threadThreeHandler(data);
}
});
}