Tidy up shards a bit

This commit is contained in:
Amish Shah
2016-08-30 19:07:05 +01:00
parent c70dfa83fb
commit cde3bcb3bd
4 changed files with 17 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@@ -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);
} }
} }

View File

@@ -17,6 +17,8 @@ client.on('message', msg => {
} }
}); });
process.send(123);
client.on('ready', () => { client.on('ready', () => {
console.log('Ready'); console.log('Ready');
}); });

View File

@@ -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);