Permalink
Browse files

add: Shard.js extension for cluster instead of child_process

  • Loading branch information...
AdityaTD committed Aug 24, 2018
1 parent af23dfa commit 9bd3b99472d5cd7754d11d3ef978798f62a57206
Showing with 36 additions and 0 deletions.
  1. +34 −0 lib/extensions/Shard.js
  2. +2 −0 start.js
View
@@ -0,0 +1,34 @@
const cluster = require("cluster");
const path = require("path");
const { Shard } = require("discord.js");
Shard.prototype.spawn = async function spawn(waitForReady = true) {
if (this.process) throw new Error("SHARDING_PROCESS_EXISTS", this.id);
cluster.setupMaster({
exec: path.resolve(this.manager.file),
args: this.args || [],
env: this.env,
execArgv: this.execArgv || []
});
this.process = cluster.fork(this.env)
.on("message", this._handleMessage.bind(this))
.on("exit", this._exitListener);
/**
* Emitted upon the creation of the shard's child process.
* @event Shard#spawn
* @param {ChildProcess} process Child process that was created
*/
this.emit("spawn", this.process);
if (!waitForReady) return this.process;
await new Promise((resolve, reject) => {
this.once("ready", resolve);
this.once("disconnect", () => reject(new Error("SHARDING_READY_DISCONNECTED", this.id)));
this.once("death", () => reject(new Error("SHARDING_READY_DIED", this.id)));
setTimeout(() => reject(new Error("SHARDING_READY_TIMEOUT", this.id)), 30000);
});
return this.process;
};
View
@@ -1,6 +1,8 @@
const { ShardingManager } = require("discord.js");
const { main } = require("./config.json");
require("./lib/extensions/Shard");
const manager = new ShardingManager(`./app.js`, { totalShards: main.shards, token: main.token, respawn: true });
manager.spawn(manager.totalShards, 5500, false);

0 comments on commit 9bd3b99

Please sign in to comment.