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