start and stop commands

This commit is contained in:
Erik 2023-05-09 03:15:05 +03:00
parent a37fd81052
commit 268c3421dd
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
2 changed files with 76 additions and 6 deletions

View File

@ -1,6 +1,6 @@
import BaseCommand from "../BaseCommand.js";
import Controller from "../Controller.js";
import { OptionType } from '@navy.gif/commandparser';
import { ArgsResult, OptionType } from '@navy.gif/commandparser';
class StartCommand extends BaseCommand {
@ -12,12 +12,44 @@ class StartCommand extends BaseCommand {
aliases: [ 's' ],
flag: true,
type: OptionType.INTEGER
}, {
name: 'amount',
type: OptionType.INTEGER
}]
});
}
execute (_message: unknown, _args?: unknown): Promise<unknown> {
throw new Error("Method not implemented.");
async execute ({ args }: { subcommand: string, args: ArgsResult }): Promise<string> {
const { shard, amount } = args;
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;
}
}

View File

@ -1,15 +1,53 @@
import { ArgsResult, OptionType } from "@navy.gif/commandparser";
import BaseCommand from "../BaseCommand.js";
import Controller from "../Controller.js";
class StopCommand extends BaseCommand {
constructor (controller: 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 (_message: unknown, _args?: unknown) {
//
async execute ({ args }: { subcommand: string, args: ArgsResult }) {
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;
}
}