Improve sharding stuff (#608)

* Improve sharding stuff

* Build docs
This commit is contained in:
Schuyler Cebulskie
2016-09-04 17:02:52 -04:00
committed by Amish Shah
parent 7b870d13de
commit 2c6b804fc9
3 changed files with 44 additions and 18 deletions

File diff suppressed because one or more lines are too long

View File

@@ -5,6 +5,10 @@ const path = require('path');
* Represents a Shard spawned by the ShardingManager. * Represents a Shard spawned by the ShardingManager.
*/ */
class Shard { class Shard {
/**
* @param {ShardingManager} manager The sharding manager
* @param {number} id The ID of this shard
*/
constructor(manager, id) { constructor(manager, id) {
/** /**
* The manager of the spawned shard * The manager of the spawned shard
@@ -12,13 +16,13 @@ class Shard {
*/ */
this.manager = manager; this.manager = manager;
/** /**
* The shard id * The shard ID
* @type {number} * @type {number}
*/ */
this.id = id; this.id = id;
/** /**
* The process of the shard * The process of the shard
* @type {process} * @type {ChildProcess}
*/ */
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]); this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]);
} }

View File

@@ -1,7 +1,9 @@
const path = require('path'); const path = require('path');
const fs = require('fs');
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const Collection = require('../util/Collection');
const Shard = require('./Shard'); const Shard = require('./Shard');
const Collection = require('../util/Collection');
/** /**
* This is a utility class that can be used to help you spawn shards of your Client. Each shard is completely separate * This is a utility class that can be used to help you spawn shards of your Client. Each shard is completely separate
@@ -11,29 +13,42 @@ const Shard = require('./Shard');
*/ */
class ShardingManager extends EventEmitter { class ShardingManager extends EventEmitter {
/** /**
* Creates an instance of ShardingManager. * @param {string} file Path to your shard script file
* @param {string} file the path to your file * @param {number} [totalShards=1] Number of shards to default to spawning
* @param {number} totalShards the number of shards you would like to spawn
*/ */
constructor(file, totalShards) { constructor(file, totalShards) {
super(); super();
/**
* Path to the shard script file
* @type {string}
*/
this.file = file; this.file = file;
if (!path.isAbsolute(file)) { if (!file) throw new Error('file must be specified');
this.file = path.resolve(`${process.cwd()}${file}`); if (!path.isAbsolute(file)) this.file = path.resolve(process.cwd(), file);
} const stats = fs.statSync(this.file);
if (!stats.isFile()) throw new Error('file path does not point to a file');
/** /**
* The amount of shards that this manager is going to spawn * The amount of shards that this manager is going to spawn
* @type {number} * @type {number}
*/ */
this.totalShards = totalShards; this.totalShards = typeof totalShards !== 'undefined' ? totalShards : 1;
if (typeof this.totalShards !== 'number' || isNaN(this.totalShards)) {
throw new TypeError('amout of shards must be a number');
}
if (this.totalShards < 1) throw new RangeError('amount of shards must be at least 1');
/** /**
* A collection of shards that this manager has spawned. * A collection of shards that this manager has spawned
* @type {Collection<number, Shard>} * @type {Collection<number, Shard>}
*/ */
this.shards = new Collection(); this.shards = new Collection();
this.waiting = new Collection();
} }
/**
* Spawns a single shard.
*/
createShard() { createShard() {
const id = this.shards.size; const id = this.shards.size;
const shard = new Shard(this, id); const shard = new Shard(this, id);
@@ -41,14 +56,21 @@ class ShardingManager extends EventEmitter {
this.emit('launch', id, shard); this.emit('launch', id, shard);
} }
/**
* Spawns multiple shards.
* @param {number} [amount=this.totalShards] The number of shards to spawn
*/
spawn(amount) { spawn(amount) {
this.totalShards = amount; if (typeof amount !== 'undefined') {
if (typeof amount !== 'number' || isNaN(amount)) throw new TypeError('amout of shards must be a number');
if (amount < 1) throw new RangeError('amount of shards must be at least 1');
this.totalShards = amount;
}
this.createShard(); this.createShard();
const interval = setInterval(() => { const interval = setInterval(() => {
if (this.shards.size === this.totalShards) { if (this.shards.size === this.totalShards) clearInterval(interval);
return clearInterval(interval); else this.createShard();
}
return this.createShard();
}, 5500); }, 5500);
} }
} }