mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Tidy up shards a bit
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,6 +1,7 @@
|
||||
const childProcess = require('child_process');
|
||||
const path = require('path');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
class ShardingManager extends EventEmitter {
|
||||
constructor(file, totalShards) {
|
||||
@@ -10,23 +11,24 @@ class ShardingManager extends EventEmitter {
|
||||
this.file = path.resolve(`${process.cwd()}${file}`);
|
||||
}
|
||||
this.totalShards = totalShards;
|
||||
this.runningShards = 0;
|
||||
this.createShards();
|
||||
this.shards = new Collection();
|
||||
}
|
||||
|
||||
createShard(id) {
|
||||
const shard = childProcess.fork(path.resolve(this.file), [id, this.totalShards]);
|
||||
createShard() {
|
||||
const id = this.shards.size;
|
||||
const shard = childProcess.fork(path.resolve(this.file), [id, this.totalShards], { silent: true });
|
||||
this.shards.set(id, shard);
|
||||
this.emit('launch', id, shard);
|
||||
}
|
||||
|
||||
createShards() {
|
||||
this.createShard(0);
|
||||
spawn(amount) {
|
||||
this.totalShards = amount;
|
||||
this.createShard();
|
||||
const interval = setInterval(() => {
|
||||
this.runningShards++;
|
||||
if (this.runningShards === this.totalShards) {
|
||||
if (this.shards.size === this.totalShards) {
|
||||
return clearInterval(interval);
|
||||
}
|
||||
this.createShard(this.runningShards);
|
||||
this.createShard();
|
||||
}, 5500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ client.on('message', msg => {
|
||||
}
|
||||
});
|
||||
|
||||
process.send(123);
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log('Ready');
|
||||
});
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
const Discord = require('../');
|
||||
|
||||
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`, 5);
|
||||
const sharder = new Discord.ShardingManager(`${process.cwd()}/test/shard.js`);
|
||||
|
||||
sharder.on('launch', id => console.log(`launched ${id}`));
|
||||
|
||||
sharder.spawn(5);
|
||||
|
||||
Reference in New Issue
Block a user