make shardmanager better (#731)

* add respawn thing for shards, and make it easier to recieve messages from shards

* run docs
This commit is contained in:
Gus Caplan
2016-09-23 12:36:14 -05:00
committed by Amish Shah
parent 4bf6ad30f3
commit fc9d049cc1
3 changed files with 15 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@@ -27,6 +27,14 @@ class Shard {
* @type {ChildProcess}
*/
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]);
this.process.once('exit', () => {
if (this.manager.respawn) this.manager.createShard(this.id);
});
this.process.on('message', message => {
this.manager.emit('message', this, message);
});
}
/**

View File

@@ -15,8 +15,9 @@ class ShardingManager extends EventEmitter {
/**
* @param {string} file Path to your shard script file
* @param {number} [totalShards=1] Number of shards to default to spawning
* @param {boolean} [respawn=true] Respawn a shard when it dies
*/
constructor(file, totalShards) {
constructor(file, totalShards, respawn = true) {
super();
/**
@@ -39,6 +40,8 @@ class ShardingManager extends EventEmitter {
}
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
this.respawn = respawn;
/**
* A collection of shards that this manager has spawned
* @type {Collection<number, Shard>}
@@ -48,9 +51,9 @@ class ShardingManager extends EventEmitter {
/**
* Spawns a single shard.
* @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE!
*/
createShard() {
const id = this.shards.size;
createShard(id = this.shards.size) {
const shard = new Shard(this, id);
this.shards.set(id, shard);
/**