rename class members
This commit is contained in:
parent
1570f347f0
commit
9ab9084391
@ -40,87 +40,87 @@ const CLIOptions: CommandOption[] = [
|
||||
class Controller extends EventEmitter
|
||||
{
|
||||
|
||||
#_directory: string;
|
||||
#_srcDir: string;
|
||||
#directory: string;
|
||||
#srcDir: string;
|
||||
|
||||
#_options: ControllerOptions;
|
||||
#_built: boolean;
|
||||
#_logger: MasterLogger;
|
||||
#_parser?: CommandParser;
|
||||
#options: ControllerOptions;
|
||||
#built: boolean;
|
||||
#logger: MasterLogger;
|
||||
#parser?: CommandParser;
|
||||
|
||||
#_serverFilePath: string;
|
||||
#_shards: Collection<number, Shard>;
|
||||
#_readyAt?: number;
|
||||
#_commands: BaseCommand[] = [];
|
||||
#serverFilePath: string;
|
||||
#shards: Collection<number, Shard>;
|
||||
#readyAt?: number;
|
||||
#commands: BaseCommand[] = [];
|
||||
|
||||
constructor (dir: string, options: ControllerOptions)
|
||||
{
|
||||
super();
|
||||
|
||||
this.#_directory = dir;
|
||||
this.#_srcDir = options.srcDir;
|
||||
this.#directory = dir;
|
||||
this.#srcDir = options.srcDir;
|
||||
|
||||
this.#_options = options;
|
||||
this.#_built = false;
|
||||
this.#options = options;
|
||||
this.#built = false;
|
||||
|
||||
this.#_logger = new MasterLogger({ ...options.logger, webhook: { url: process.env.LOGGER_WEBHOOK } });
|
||||
this.#logger = new MasterLogger({ ...options.logger, webhook: { url: process.env.LOGGER_WEBHOOK } });
|
||||
|
||||
// Path to the file that gets forked into shards -- relative to process.cwd() (e.g. the directory in which the process was started)
|
||||
this.#_serverFilePath = path.resolve(this.#_srcDir, options.serverFilePath);
|
||||
this.#_shards = new Collection();
|
||||
this.#serverFilePath = path.resolve(this.#srcDir, options.serverFilePath);
|
||||
this.#shards = new Collection();
|
||||
|
||||
process.on('warning', (warn) =>
|
||||
{
|
||||
this.#_logger.warn(warn.stack || warn.message);
|
||||
this.#logger.warn(warn.stack || warn.message);
|
||||
});
|
||||
|
||||
process.on('error', (error) =>
|
||||
{
|
||||
this.#_logger.error(error.stack);
|
||||
this.#logger.error(error.stack);
|
||||
});
|
||||
|
||||
process.on('uncaughtException', (ex) =>
|
||||
{
|
||||
this.#_logger.error(ex.stack || ex.message);
|
||||
this.#logger.error(ex.stack || ex.message);
|
||||
});
|
||||
|
||||
process.on('SIGINT', this.#shutdown.bind(this));
|
||||
|
||||
this.on('built', () =>
|
||||
{
|
||||
this.#_logger.info('API Controller built');
|
||||
this.#_built = true;
|
||||
this.#_readyAt = Date.now();
|
||||
this.#logger.info('API Controller built');
|
||||
this.#built = true;
|
||||
this.#readyAt = Date.now();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
get built ()
|
||||
{
|
||||
return this.#_built;
|
||||
return this.#built;
|
||||
}
|
||||
|
||||
get readyAt ()
|
||||
{
|
||||
return this.#_readyAt;
|
||||
return this.#readyAt;
|
||||
}
|
||||
|
||||
get shards ()
|
||||
{
|
||||
return this.#_shards;
|
||||
return this.#shards;
|
||||
}
|
||||
|
||||
get srcDir ()
|
||||
{
|
||||
return this.#_srcDir;
|
||||
return this.#srcDir;
|
||||
}
|
||||
|
||||
async init (): Promise<void>
|
||||
{
|
||||
|
||||
this.#_logger.info('Initialising Controller');
|
||||
const files: string[] = Util.readdirRecursive(path.resolve(this.#_directory, './commands')).filter(name => !name.includes('.d.ts') && !name.includes('.map'));
|
||||
this.#_commands = [];
|
||||
this.#logger.info('Initialising Controller');
|
||||
const files: string[] = Util.readdirRecursive(path.resolve(this.#directory, './commands')).filter(name => !name.includes('.d.ts') && !name.includes('.map'));
|
||||
this.#commands = [];
|
||||
for (const file of files)
|
||||
{
|
||||
const imported = await import(`file://${file}`).catch(() => null);
|
||||
@ -128,16 +128,16 @@ class Controller extends EventEmitter
|
||||
throw new Error(`Invalid file ${file}`);
|
||||
const cmd = imported.default;
|
||||
if (typeof cmd !== 'function')
|
||||
return void this.#_logger.warn(`Attempted to load an invalid command: ${file}`);
|
||||
return void this.#logger.warn(`Attempted to load an invalid command: ${file}`);
|
||||
|
||||
|
||||
const command = new cmd(this);
|
||||
this.#_commands.push(command);
|
||||
this.#commands.push(command);
|
||||
}
|
||||
|
||||
|
||||
this.#_parser = new CommandParser({
|
||||
commands: this.#_commands,
|
||||
this.#parser = new CommandParser({
|
||||
commands: this.#commands,
|
||||
prefix: '',
|
||||
resolver: {
|
||||
resolveBoolean: (input: string) =>
|
||||
@ -173,8 +173,8 @@ class Controller extends EventEmitter
|
||||
projectName,
|
||||
shardCount = 1,
|
||||
serverOptions = {} as ServerOptions,
|
||||
} = this.#_options;
|
||||
this.#_logger.info(`Spawning ${shardCount} shards`);
|
||||
} = this.#options;
|
||||
this.#logger.info(`Spawning ${shardCount} shards`);
|
||||
|
||||
serverOptions.name = projectName;
|
||||
|
||||
@ -197,7 +197,7 @@ class Controller extends EventEmitter
|
||||
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.#logger.attach(shard);
|
||||
this.#setListeners(shard);
|
||||
return shard;
|
||||
|
||||
@ -210,14 +210,14 @@ class Controller extends EventEmitter
|
||||
serverOptions = {} as ServerOptions,
|
||||
logger = {},
|
||||
databases = {}
|
||||
} = this.#_options;
|
||||
} = this.#options;
|
||||
return {
|
||||
serverOptions: {
|
||||
...serverOptions, logger, databases
|
||||
},
|
||||
...shardOptions,
|
||||
env: this.#_options.env,
|
||||
path: this.#_serverFilePath
|
||||
env: this.#options.env,
|
||||
path: this.#serverFilePath
|
||||
};
|
||||
}
|
||||
|
||||
@ -225,19 +225,19 @@ class Controller extends EventEmitter
|
||||
{
|
||||
if (msg._logger)
|
||||
return;
|
||||
this.#_logger.debug(`Message from ${shard.id}: ${inspect(msg)}`);
|
||||
this.#logger.debug(`Message from ${shard.id}: ${inspect(msg)}`);
|
||||
}
|
||||
|
||||
#setListeners (shard: Shard)
|
||||
{
|
||||
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('shutdown', () => this.#_logger.info(`Shard ${shard.id} is shutting down gracefully`));
|
||||
shard.on('ready', () => this.#_logger.info(`Shard ${shard.id} is ready`));
|
||||
shard.on('disconnect', () => this.#_logger.warn(`Shard ${shard.id} has disconnected`));
|
||||
shard.on('spawn', () => this.#_logger.info(`Shard ${shard.id} spawned`));
|
||||
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('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('shutdown', () => this.#logger.info(`Shard ${shard.id} is shutting down gracefully`));
|
||||
shard.on('ready', () => this.#logger.info(`Shard ${shard.id} is ready`));
|
||||
shard.on('disconnect', () => this.#logger.warn(`Shard ${shard.id} has disconnected`));
|
||||
shard.on('spawn', () => this.#logger.info(`Shard ${shard.id} spawned`));
|
||||
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('message', (msg) => this.#handleMessage(shard, msg));
|
||||
}
|
||||
@ -250,19 +250,19 @@ class Controller extends EventEmitter
|
||||
return;
|
||||
|
||||
let result = null;
|
||||
if (!this.#_parser)
|
||||
if (!this.#parser)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
result = await this.#_parser.parseMessage(words.join(' '));
|
||||
result = await this.#parser.parseMessage(words.join(' '));
|
||||
if (!result)
|
||||
return this.#_logger.error(`${words[0]} is not a valid command`);
|
||||
return this.#logger.error(`${words[0]} is not a valid command`);
|
||||
}
|
||||
catch (err)
|
||||
{
|
||||
const error = err as Error;
|
||||
return this.#_logger.error(error.message);
|
||||
return this.#logger.error(error.message);
|
||||
}
|
||||
|
||||
const { command, ...rest } = result;
|
||||
@ -272,7 +272,7 @@ class Controller extends EventEmitter
|
||||
if (command.help)
|
||||
// eslint-disable-next-line no-console
|
||||
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`);
|
||||
}
|
||||
|
||||
let response = null;
|
||||
@ -284,8 +284,8 @@ class Controller extends EventEmitter
|
||||
{
|
||||
const error = err as Error;
|
||||
if (error.constructor.name === 'CommandError')
|
||||
return this.#_logger.error(error.message);
|
||||
this.#_logger.error(error.stack || error.message);
|
||||
return this.#logger.error(error.message);
|
||||
this.#logger.error(error.stack || error.message);
|
||||
}
|
||||
|
||||
if (response)
|
||||
@ -298,31 +298,31 @@ class Controller extends EventEmitter
|
||||
{
|
||||
|
||||
const params = process.argv.slice(2);
|
||||
if (!this.#_parser)
|
||||
if (!this.#parser)
|
||||
return;
|
||||
|
||||
const args = await this.#_parser.parseOptions(params, CLIOptions);
|
||||
const args = await this.#parser.parseOptions(params, CLIOptions);
|
||||
|
||||
if (args.port)
|
||||
this.#_options.serverOptions.http.port = args.port.value as number;
|
||||
this.#options.serverOptions.http.port = args.port.value as number;
|
||||
|
||||
if (args.respawn)
|
||||
this.#_options.shardOptions.respawn = args.respawn.value as boolean;
|
||||
this.#options.shardOptions.respawn = args.respawn.value as boolean;
|
||||
|
||||
if (args.shards)
|
||||
this.#_options.shardCount = args.shards.valeu as number;
|
||||
this.#options.shardCount = args.shards.valeu as number;
|
||||
|
||||
}
|
||||
|
||||
async #shutdown ()
|
||||
{
|
||||
this.#_logger.info('Received SIGINT, shutting down');
|
||||
this.#logger.info('Received SIGINT, shutting down');
|
||||
setTimeout(process.exit, 90_000);
|
||||
const promises = this.#_shards.filter(shard => shard.ready).map(shard => shard.awaitShutdown());
|
||||
const promises = this.#shards.filter(shard => shard.ready).map(shard => shard.awaitShutdown());
|
||||
if (promises.length)
|
||||
await Promise.all(promises);
|
||||
this.#_logger.status('Shutdown completed, goodbye');
|
||||
this.#_logger.close();
|
||||
this.#logger.status('Shutdown completed, goodbye');
|
||||
this.#logger.close();
|
||||
|
||||
// eslint-disable-next-line no-process-exit
|
||||
process.exit();
|
||||
|
Loading…
Reference in New Issue
Block a user