Compare commits
No commits in common. "68512ec8718d70bb44532c6e86be4c1d06e9993d" and "a37fd81052f296f010e64d1a66f3e045d4a0e0b4" have entirely different histories.
68512ec871
...
a37fd81052
@ -94,7 +94,12 @@ class Controller extends EventEmitter {
|
|||||||
const {
|
const {
|
||||||
projectName,
|
projectName,
|
||||||
shardCount = 1,
|
shardCount = 1,
|
||||||
|
shardOptions = {},
|
||||||
serverOptions = {} as ServerOptions,
|
serverOptions = {} as ServerOptions,
|
||||||
|
logger = {},
|
||||||
|
discord = {},
|
||||||
|
databases = {},
|
||||||
|
rabbitConfig = {} as BrokerOptions
|
||||||
} = this.#_options;
|
} = this.#_options;
|
||||||
this.#_logger.info(`Spawning ${shardCount} shards`);
|
this.#_logger.info(`Spawning ${shardCount} shards`);
|
||||||
|
|
||||||
@ -119,12 +124,22 @@ class Controller extends EventEmitter {
|
|||||||
process.stdin.on('data', (data) => {
|
process.stdin.on('data', (data) => {
|
||||||
const raw = data.toString('utf-8');
|
const raw = data.toString('utf-8');
|
||||||
const words = raw.split(' ').map(word => word.trim());
|
const words = raw.split(' ').map(word => word.trim());
|
||||||
this.#handleStdin(words);
|
this._handleStdin(words);
|
||||||
});
|
});
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
for (let i = 0; i < shardCount; i++) {
|
for (let i = 0; i < shardCount; i++) {
|
||||||
const shard = this.createShard();
|
const shard = new Shard(this, i, {
|
||||||
|
serverOptions: {
|
||||||
|
...serverOptions, logger, discord, databases, rabbitConfig
|
||||||
|
},
|
||||||
|
...shardOptions,
|
||||||
|
env: this.#_options.env,
|
||||||
|
path: this.#_serverFilePath
|
||||||
|
});
|
||||||
|
this.#_logger.attach(shard);
|
||||||
|
this._setListeners(shard);
|
||||||
|
this.#_shards.set(i, shard);
|
||||||
promises.push(shard.spawn());
|
promises.push(shard.spawn());
|
||||||
|
|
||||||
// setTimeout(() => {
|
// setTimeout(() => {
|
||||||
@ -138,44 +153,13 @@ class Controller extends EventEmitter {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createShard () {
|
_handleMessage (shard: Shard, msg: IPCMessage) {
|
||||||
|
|
||||||
const ids = this.shards.map(s => s.id);
|
|
||||||
const id = ids.length ? Math.max(...ids) + 1 : 0;
|
|
||||||
const shard = new Shard(this, id, this.shardOptions);
|
|
||||||
this.shards.set(shard.id, shard);
|
|
||||||
this.#_logger.attach(shard);
|
|
||||||
this.#setListeners(shard);
|
|
||||||
return shard;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
get shardOptions () {
|
|
||||||
const {
|
|
||||||
shardOptions = {},
|
|
||||||
serverOptions = {} as ServerOptions,
|
|
||||||
logger = {},
|
|
||||||
discord = {},
|
|
||||||
databases = {},
|
|
||||||
rabbitConfig = {} as BrokerOptions
|
|
||||||
} = this.#_options;
|
|
||||||
return {
|
|
||||||
serverOptions: {
|
|
||||||
...serverOptions, logger, discord, databases, rabbitConfig
|
|
||||||
},
|
|
||||||
...shardOptions,
|
|
||||||
env: this.#_options.env,
|
|
||||||
path: this.#_serverFilePath
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#handleMessage (shard: Shard, msg: IPCMessage) {
|
|
||||||
if (msg._logger)
|
if (msg._logger)
|
||||||
return;
|
return;
|
||||||
this.#_logger.debug(`Message from ${shard.id}: ${inspect(msg)}`);
|
this.#_logger.debug(`Message from ${shard.id}: ${inspect(msg)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
#setListeners (shard: Shard) {
|
_setListeners (shard: Shard) {
|
||||||
shard.on('death', () => this.#_logger.info(`Shard ${shard.id} has died`));
|
shard.on('death', () => this.#_logger.info(`Shard ${shard.id} has died`));
|
||||||
shard.on('fatal', ({ message }) => this.#_logger.warn(`Shard ${shard.id} has died fatally: ${message}`));
|
shard.on('fatal', ({ message }) => this.#_logger.warn(`Shard ${shard.id} has died fatally: ${message}`));
|
||||||
shard.on('shutdown', () => this.#_logger.info(`Shard ${shard.id} is shutting down gracefully`));
|
shard.on('shutdown', () => this.#_logger.info(`Shard ${shard.id} is shutting down gracefully`));
|
||||||
@ -185,10 +169,10 @@ class Controller extends EventEmitter {
|
|||||||
shard.on('error', (err) => this.#_logger.error(`Shard ${shard.id} ran into an error:\n${err.stack}`));
|
shard.on('error', (err) => this.#_logger.error(`Shard ${shard.id} ran into an error:\n${err.stack}`));
|
||||||
shard.on('warn', (msg) => this.#_logger.warn(`Warning from shard ${shard.id}: ${msg}`, { broadcast: true }));
|
shard.on('warn', (msg) => this.#_logger.warn(`Warning from shard ${shard.id}: ${msg}`, { broadcast: true }));
|
||||||
|
|
||||||
shard.on('message', (msg) => this.#handleMessage(shard, msg));
|
shard.on('message', (msg) => this._handleMessage(shard, msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
async #handleStdin (words: string[]) {
|
async _handleStdin (words: string[]) {
|
||||||
|
|
||||||
words = words.filter(word => word.length);
|
words = words.filter(word => word.length);
|
||||||
if (!words.length)
|
if (!words.length)
|
||||||
@ -211,8 +195,7 @@ class Controller extends EventEmitter {
|
|||||||
|
|
||||||
if (rest.args.help) {
|
if (rest.args.help) {
|
||||||
if (command.help)
|
if (command.help)
|
||||||
// eslint-disable-next-line no-console
|
return command.help;
|
||||||
return console.log(command.help);
|
|
||||||
return this.#_logger.info(`Help for ${command.name} unavailable at this time`);
|
return this.#_logger.info(`Help for ${command.name} unavailable at this time`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import BaseCommand from "../BaseCommand.js";
|
import BaseCommand from "../BaseCommand.js";
|
||||||
import Controller from "../Controller.js";
|
import Controller from "../Controller.js";
|
||||||
import { ArgsResult, OptionType } from '@navy.gif/commandparser';
|
import { OptionType } from '@navy.gif/commandparser';
|
||||||
|
|
||||||
class StartCommand extends BaseCommand {
|
class StartCommand extends BaseCommand {
|
||||||
|
|
||||||
@ -12,44 +12,12 @@ class StartCommand extends BaseCommand {
|
|||||||
aliases: [ 's' ],
|
aliases: [ 's' ],
|
||||||
flag: true,
|
flag: true,
|
||||||
type: OptionType.INTEGER
|
type: OptionType.INTEGER
|
||||||
}, {
|
|
||||||
name: 'amount',
|
|
||||||
type: OptionType.INTEGER
|
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute ({ args }: { subcommand: string, args: ArgsResult }): Promise<string> {
|
execute (_message: unknown, _args?: unknown): Promise<unknown> {
|
||||||
const { shard, amount } = args;
|
throw new Error("Method not implemented.");
|
||||||
const { shards } = this.controller;
|
|
||||||
|
|
||||||
const n = amount?.value as number || 0;
|
|
||||||
let id = -1;
|
|
||||||
|
|
||||||
if (shard)
|
|
||||||
id = shard.value as number;
|
|
||||||
|
|
||||||
let out = '';
|
|
||||||
if (id >= 0) {
|
|
||||||
const s = shards.get(id);
|
|
||||||
if (!s)
|
|
||||||
return `Shard ${id} doesn't exist`;
|
|
||||||
if (s.ready)
|
|
||||||
return `Shard ${id} is already running`;
|
|
||||||
await s.spawn();
|
|
||||||
out += `Started shard ${id}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n > 0) {
|
|
||||||
for (let i = 0; i < n; i++) {
|
|
||||||
const s = this.controller.createShard();
|
|
||||||
await s.spawn(true);
|
|
||||||
}
|
|
||||||
out += `\nSpawned ${n} shards`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,53 +1,15 @@
|
|||||||
import { ArgsResult, OptionType } from "@navy.gif/commandparser";
|
|
||||||
import BaseCommand from "../BaseCommand.js";
|
import BaseCommand from "../BaseCommand.js";
|
||||||
import Controller from "../Controller.js";
|
import Controller from "../Controller.js";
|
||||||
|
|
||||||
class StopCommand extends BaseCommand {
|
class StopCommand extends BaseCommand {
|
||||||
constructor (controller: Controller) {
|
constructor (controller: Controller) {
|
||||||
super(controller, {
|
super(controller, {
|
||||||
name: 'stop',
|
name: 'stop'
|
||||||
options: [{
|
|
||||||
name: 'shard',
|
|
||||||
type: OptionType.INTEGER,
|
|
||||||
flag: true,
|
|
||||||
}, {
|
|
||||||
name: 'all',
|
|
||||||
flag: true,
|
|
||||||
type: OptionType.BOOLEAN,
|
|
||||||
valueOptional: true,
|
|
||||||
defaultValue: true
|
|
||||||
}, {
|
|
||||||
name: 'prune',
|
|
||||||
flag: true,
|
|
||||||
type: OptionType.BOOLEAN,
|
|
||||||
valueOptional: true,
|
|
||||||
defaultValue: true
|
|
||||||
}]
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async execute ({ args }: { subcommand: string, args: ArgsResult }) {
|
async execute (_message: unknown, _args?: unknown) {
|
||||||
const id = args.shard?.value;
|
//
|
||||||
const all = args.all?.value || false;
|
|
||||||
const prune = args.prune?.value || false;
|
|
||||||
if (typeof id !== 'number' && !all)
|
|
||||||
return `Must supply either --all or --shard option`;
|
|
||||||
|
|
||||||
const { shards } = this.controller;
|
|
||||||
const targets = all ? shards : shards.filter(shard => shard.id === id);
|
|
||||||
const ids = targets.map(s => s.id);
|
|
||||||
|
|
||||||
for (const shard of targets.values()) {
|
|
||||||
await shard.kill();
|
|
||||||
if (prune)
|
|
||||||
shards.delete(shard.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
let out = `Stopped ${all ? `all shards (${ids.join(', ')})` : `shard ${id}`}.`;
|
|
||||||
if (prune)
|
|
||||||
out += ' Pruned stopped shards';
|
|
||||||
return out;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user