galactic-bot/middleware/Shard.js

217 lines
7.1 KiB
JavaScript
Raw Normal View History

2020-04-08 18:08:46 +02:00
/* Adopted from Discord.js */
const path = require('path');
const EventEmitter = require('events');
2020-04-09 16:30:52 +02:00
const { Util } = require('../util/');
2020-04-08 18:08:46 +02:00
let childProcess = null;
let Worker = null;
2020-04-08 16:27:34 +02:00
class Shard extends EventEmitter {
2020-04-09 16:30:52 +02:00
constructor(manager, id) {
2020-04-08 18:08:46 +02:00
super();
2020-04-09 16:30:52 +02:00
if(manager.mode === 'process') childProcess = require('child_process');
else if(manager.mode === 'worker') Worker = require('worker_threads').Worker;
2020-04-08 18:08:46 +02:00
2020-04-09 16:30:52 +02:00
this.manager = manager;
2020-04-08 18:08:46 +02:00
this.id = id;
2020-04-09 16:30:52 +02:00
this.args = manager.shardArgs || [];
this.execArgv = manager.execArgv;
2020-04-08 18:08:46 +02:00
this.env = Object.assign({}, process.env, {
2020-04-09 16:30:52 +02:00
SHARDING_MANAGER: true,
2020-04-08 18:08:46 +02:00
SHARDS: this.id,
2020-04-09 16:30:52 +02:00
TOTAL_SHARD_COUNT: this.manager.totalShards,
DISCORD_TOKEN: this.manager.token
2020-04-08 18:08:46 +02:00
});
this.ready = false;
this.process = null;
this.worker = null;
this._evals = new Map();
this._fetches = new Map();
this._exitListener = this._handleExit.bind(this, undefined);
}
async spawn(waitForReady = true) {
if(this.process) throw new Error(`[shard${this.id}] Sharding process already exists.`);
if(this.worker) throw new Error(`[shard${this.id}] Sharding worker already exists.`);
2020-04-09 16:30:52 +02:00
if(this.manager.mode === 'process') {
this.process = childProcess.fork(path.resolve(this.manager.file), this.args, {
2020-04-08 18:08:46 +02:00
env: this.env,
execArgv: this.execArgv
})
.on('message', this._handleMessage.bind(this))
.on('exit', this._exitListener);
2020-04-09 16:30:52 +02:00
} else if(this.manager.mode === 'worker') {
this.worker = new Worker(path.resolve(this.manager.file), { workerData: this.env })
2020-04-08 18:08:46 +02:00
.on('message', this._handleMessage.bind(this))
.on('exit', this._exitListener);
}
this.emit('spawn', this.process || this.worker);
if(!waitForReady) return this.process || this.worker;
await new Promise((resolve, reject) => {
this.once('ready', resolve);
this.once('disconnect', () => reject(new Error(`[shard${this.id}] Shard disconnected while readying.`)));
this.once('death', () => reject(new Error(`[shard${this.id}] Shard died while readying.`)));
2020-04-09 16:30:52 +02:00
setTimeout(() => reject(new Error(`[shard${this.id}] Shard timed out while readying.`)), 10000);
2020-04-08 18:08:46 +02:00
});
return this.process || this.worker;
}
kill() {
if(this.process) {
this.process.removeListener('exit', this._exitListener);
this.process.kill();
} else {
this.worker.removeListener('exit', this._exitListener);
this.worker.terminate();
}
this._handleExit(false);
}
async respawn(delay = 500, waitForReady = true) {
this.kill();
if(delay > 0) await Util.delayFor(delay);
return this.spawn(waitForReady);
}
send(message) {
return new Promise((resolve, reject) => {
if(this.process) {
this.process.send(message, error => {
if(error) reject(error); else resolve(this);
});
} else {
this.worker.postMessage(message);
resolve(this);
}
});
}
fetchClientValue(prop) {
if(this._fetches.has(prop)) return this._fetches.get(prop);
const promise = new Promise((resolve, reject) => {
const child = this.process || this.worker;
const listener = message => {
if(!message || message._fetchProp !== prop) return;
child.removeListener('message', listener);
this._fetches.delete(prop);
resolve(message._result);
};
child.on('message', listener);
this.send({ _fetchProp: prop }).catch(err => {
child.removeListener('message', listener);
this._fetches.delete(prop);
reject(err);
});
});
this._fetches.set(prop, promise);
return promise;
}
eval(script) {
if(this._evals.has(script)) return this._evals.get(script);
const promise = new Promise((resolve, reject) => {
const child = this.process || this.worker;
const listener = message => {
if(!message || message._eval !== script) return;
child.removeListener('message', listener);
this._evals.delete(script);
if(!message._error) resolve(message._result); else reject(new Error(message._error));
};
child.on('message', listener);
const _eval = typeof script === 'function' ? `(${script})(this)` : script;
this.send({ _eval }).catch(err => {
child.removeListener('message', listener);
this._evals.delete(script);
reject(err);
});
});
this._evals.set(script, promise);
return promise;
}
_handleMessage(message) {
if(message) {
if(message._ready) { //Shard ready
this.ready = true;
this.emit('ready');
return;
}
if(message._disconnect) { //Shard disconnected
this.ready = false;
this.emit('disconnect');
return;
}
if(message._reconnecting) { //Shard attempting to reconnect
this.ready = false;
this.emit('reconnecting');
return;
}
if(message._sFetchProp) { //Shard requesting property fetch
2020-04-09 16:30:52 +02:00
this.manager.fetchClientValues(message._sFetchProp).then(
2020-04-08 18:08:46 +02:00
results => this.send({ _sFetchProp: message._sFetchProp, _result: results }),
err => this.send({ _sFetchProp: message._sFetchProp, _error: Util.makePlainError(err) })
);
return;
}
if(message._sEval) { //Shard requesting eval broadcast
2020-04-09 16:30:52 +02:00
this.manager.broadcastEval(message._sEval).then(
2020-04-08 18:08:46 +02:00
results => this.send({ _sEval: message._sEval, _result: results }),
err => this.send({ _sEval: message._sEval, _error: Util.makePlainError(err) })
);
return;
}
if(message._sRespawnAll) { //Shard requesting to respawn all shards.
const { shardDelay, respawnDelay, waitForReady } = message._sRespawnAll;
2020-04-09 16:30:52 +02:00
this.manager.respawnAll(shardDelay, respawnDelay, waitForReady).catch(() => {
2020-04-08 18:08:46 +02:00
});
return;
}
}
2020-04-09 16:30:52 +02:00
this.manager.emit('message', this, message);
2020-04-08 18:08:46 +02:00
}
2020-04-09 16:30:52 +02:00
_handleExit(respawn = this.manager.respawn) {
2020-04-08 18:08:46 +02:00
this.emit('death', this.process || this.worker);
this.ready = false;
this.process = null;
this.worker = null;
this._evals.clear();
this._fetches.clear();
if(respawn) this.spawn().catch(err => this.emit('error', err));
}
2020-04-08 16:27:34 +02:00
}
module.exports = Shard;