Add Shard#ready property and related events

This commit is contained in:
Schuyler Cebulskie
2017-11-16 22:49:38 -05:00
parent 09315ae9db
commit 196cf7652e
3 changed files with 69 additions and 14 deletions

View File

@@ -8,9 +8,9 @@ const { Error } = require('../errors');
*/ */
class Shard { class Shard {
/** /**
* @param {ShardingManager} manager The sharding manager * @param {ShardingManager} manager Manager that is spawning this shard
* @param {number} id The ID of this shard * @param {number} id ID of this shard
* @param {Array} [args=[]] Command line arguments to pass to the script * @param {string[]} [args=[]] Command line arguments to pass to the script
*/ */
constructor(manager, id, args = []) { constructor(manager, id, args = []) {
/** /**
@@ -26,7 +26,7 @@ class Shard {
this.id = id; this.id = id;
/** /**
* The environment variables for the shard * Environment variables for the shard's process
* @type {Object} * @type {Object}
*/ */
this.env = Object.assign({}, process.env, { this.env = Object.assign({}, process.env, {
@@ -41,14 +41,33 @@ class Shard {
*/ */
this.process = childProcess.fork(path.resolve(this.manager.file), args, { this.process = childProcess.fork(path.resolve(this.manager.file), args, {
env: this.env, env: this.env,
}); }).on('message', this._handleMessage.bind(this));
this.process.on('message', this._handleMessage.bind(this));
this.process.once('exit', () => {
if (this.manager.respawn) this.manager.createShard(this.id);
});
/**
* Whether the shard's {@link Client} is ready
* @type {boolean}
*/
this.ready = false;
/**
* Ongoing promises for calls to {@link Shard#eval}, mapped by the `script` they were called with
* @type {Map<string, Promise>}
* @private
*/
this._evals = new Map(); this._evals = new Map();
/**
* Ongoing promises for calls to {@link Shard#fetchClientValue}, mapped by the `prop` they were called with
* @type {Map<string, Promise>}
* @private
*/
this._fetches = new Map(); this._fetches = new Map();
// Handle the death of the process
this.process.once('exit', () => {
this.ready = false;
if (this.manager.respawn) this.manager.createShard(this.id).catch(err => { this.manager.emit('error', err); });
});
} }
/** /**
@@ -134,6 +153,39 @@ class Shard {
*/ */
_handleMessage(message) { _handleMessage(message) {
if (message) { if (message) {
// Shard is ready
if (message._ready) {
this.ready = true;
/**
* Emitted upon the shard's {@link Client#ready} event.
* @event Shard#ready
*/
this.emit('ready');
return;
}
// Shard has disconnected
if (message._disconnect) {
this.ready = false;
/**
* Emitted upon the shard's {@link Client#disconnect} event.
* @event Shard#disconnect
*/
this.emit('disconnect');
return;
}
// Shard is attempting to reconnect
if (message._reconnecting) {
this.ready = false;
/**
* Emitted upon the shard's {@link Client#reconnecting} event.
* @event Shard#reconnecting
*/
this.emit('reconnecting');
return;
}
// Shard is requesting a property fetch // Shard is requesting a property fetch
if (message._sFetchProp) { if (message._sFetchProp) {
this.manager.fetchClientValues(message._sFetchProp).then( this.manager.fetchClientValues(message._sFetchProp).then(

View File

@@ -7,11 +7,14 @@ const { Error } = require('../errors');
*/ */
class ShardClientUtil { class ShardClientUtil {
/** /**
* @param {Client} client The client of the current shard * @param {Client} client Client of the current shard
*/ */
constructor(client) { constructor(client) {
this.client = client; this.client = client;
process.on('message', this._handleMessage.bind(this)); process.on('message', this._handleMessage.bind(this));
client.on('ready', () => { process.send({ _ready: true }); });
client.on('disconnect', () => { process.send({ _disconnect: true }); });
client.on('reconnecting', () => { process.send({ _reconnecting: true }); });
} }
/** /**
@@ -49,7 +52,7 @@ class ShardClientUtil {
/** /**
* Fetches a client property value of each shard. * Fetches a client property value of each shard.
* @param {string} prop Name of the client property to get, using periods for nesting * @param {string} prop Name of the client property to get, using periods for nesting
* @returns {Promise<Array>} * @returns {Promise<Array<*>>}
* @example * @example
* client.shard.fetchClientValues('guilds.size') * client.shard.fetchClientValues('guilds.size')
* .then(results => { * .then(results => {
@@ -76,7 +79,7 @@ class ShardClientUtil {
/** /**
* Evaluates a script on all shards, in the context of the Clients. * Evaluates a script on all shards, in the context of the Clients.
* @param {string} script JavaScript to run on each shard * @param {string} script JavaScript to run on each shard
* @returns {Promise<Array>} Results of the script execution * @returns {Promise<Array<*>>} Results of the script execution
*/ */
broadcastEval(script) { broadcastEval(script) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@@ -168,7 +168,7 @@ class ShardingManager extends EventEmitter {
/** /**
* Evaluates a script on all shards, in the context of the Clients. * Evaluates a script on all shards, in the context of the Clients.
* @param {string} script JavaScript to run on each shard * @param {string} script JavaScript to run on each shard
* @returns {Promise<Array>} Results of the script execution * @returns {Promise<Array<*>>} Results of the script execution
*/ */
broadcastEval(script) { broadcastEval(script) {
const promises = []; const promises = [];
@@ -179,7 +179,7 @@ class ShardingManager extends EventEmitter {
/** /**
* Fetches a client property value of each shard. * Fetches a client property value of each shard.
* @param {string} prop Name of the client property to get, using periods for nesting * @param {string} prop Name of the client property to get, using periods for nesting
* @returns {Promise<Array>} * @returns {Promise<Array<*>>}
* @example * @example
* manager.fetchClientValues('guilds.size') * manager.fetchClientValues('guilds.size')
* .then(results => { * .then(results => {