mirror of
https://github.com/danbulant/discord.js
synced 2026-05-27 05:52:05 +00:00
Tidy up shards a bit
This commit is contained in:
parent
c70dfa83fb
commit
cde3bcb3bd
4 changed files with 17 additions and 11 deletions
File diff suppressed because one or more lines are too long
|
|
@ -1,6 +1,7 @@
|
||||||
const childProcess = require('child_process');
|
const childProcess = require('child_process');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
class ShardingManager extends EventEmitter {
|
class ShardingManager extends EventEmitter {
|
||||||
constructor(file, totalShards) {
|
constructor(file, totalShards) {
|
||||||
|
|
@ -10,23 +11,24 @@ class ShardingManager extends EventEmitter {
|
||||||
this.file = path.resolve(`${process.cwd()}${file}`);
|
this.file = path.resolve(`${process.cwd()}${file}`);
|
||||||
}
|
}
|
||||||
this.totalShards = totalShards;
|
this.totalShards = totalShards;
|
||||||
this.runningShards = 0;
|
this.shards = new Collection();
|
||||||
this.createShards();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createShard(id) {
|
createShard() {
|
||||||
const shard = childProcess.fork(path.resolve(this.file), [id, this.totalShards]);
|
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);
|
this.emit('launch', id, shard);
|
||||||
}
|
}
|
||||||
|
|
||||||
createShards() {
|
spawn(amount) {
|
||||||
this.createShard(0);
|
this.totalShards = amount;
|
||||||
|
this.createShard();
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
this.runningShards++;
|
if (this.shards.size === this.totalShards) {
|
||||||
if (this.runningShards === this.totalShards) {
|
|
||||||
return clearInterval(interval);
|
return clearInterval(interval);
|
||||||
}
|
}
|
||||||
this.createShard(this.runningShards);
|
this.createShard();
|
||||||
}, 5500);
|
}, 5500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ client.on('message', msg => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
process.send(123);
|
||||||
|
|
||||||
client.on('ready', () => {
|
client.on('ready', () => {
|
||||||
console.log('Ready');
|
console.log('Ready');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
const Discord = require('../');
|
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.on('launch', id => console.log(`launched ${id}`));
|
||||||
|
|
||||||
|
sharder.spawn(5);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue