feat(api): Add support for multithreaded API servers

This commit is contained in:
Jesse Wierzbinski 2024-05-13 11:36:46 -10:00
parent e502a2d8c8
commit 6c3fcf699e
No known key found for this signature in database
6 changed files with 66 additions and 1 deletions

49
cli/commands/start.ts Normal file
View file

@ -0,0 +1,49 @@
import os from "node:os";
import { Flags } from "@oclif/core";
import { BaseCommand } from "~cli/base";
export default class Start extends BaseCommand<typeof Start> {
static override args = {};
static override description = "Starts Lysand";
static override examples = [
"<%= config.bin %> <%= command.id %> --threads 4",
"<%= config.bin %> <%= command.id %> --all-threads",
];
static override flags = {
threads: Flags.integer({
char: "t",
description: "Number of threads to use",
default: 1,
exclusive: ["all-threads"],
}),
"all-threads": Flags.boolean({
description: "Use all available threads",
default: false,
exclusive: ["threads"],
}),
silent: Flags.boolean({
description: "Don't show logs in console",
default: false,
}),
};
public async run(): Promise<void> {
const { flags } = await this.parse(Start);
const numCPUs = flags["all-threads"] ? os.cpus().length : flags.threads;
for (let i = 0; i < numCPUs; i++) {
const args = ["bun", "index.ts"];
if (i !== 0 || flags.silent) {
args.push("--silent");
}
Bun.spawn(args, {
stdio: ["inherit", "inherit", "inherit"],
env: { ...process.env },
});
}
}
}

View file

@ -3,6 +3,7 @@ import EmojiAdd from "./commands/emoji/add";
import EmojiDelete from "./commands/emoji/delete";
import EmojiImport from "./commands/emoji/import";
import EmojiList from "./commands/emoji/list";
import Start from "./commands/start";
import UserCreate from "./commands/user/create";
import UserDelete from "./commands/user/delete";
import UserList from "./commands/user/list";
@ -18,6 +19,7 @@ export const commands = {
"emoji:delete": EmojiDelete,
"emoji:list": EmojiList,
"emoji:import": EmojiImport,
start: Start,
};
if (import.meta.path === Bun.main) {