mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
refactor(api): ♻️ Use Web Workers instead of spawning the same process once for each thread
This commit is contained in:
parent
bc8220c8f9
commit
d29603275a
7 changed files with 213 additions and 164 deletions
|
|
@ -33,21 +33,18 @@ export default class Start extends BaseCommand<typeof Start> {
|
|||
public async run(): Promise<void> {
|
||||
const { flags } = await this.parse(Start);
|
||||
|
||||
const numCpUs = flags["all-threads"] ? os.cpus().length : flags.threads;
|
||||
const numCpus = flags["all-threads"] ? os.cpus().length : flags.threads;
|
||||
|
||||
// Check if index is a JS or TS file (depending on the environment)
|
||||
const index = (await Bun.file("index.ts").exists())
|
||||
? "index.ts"
|
||||
: "index.js";
|
||||
|
||||
for (let i = 0; i < numCpUs; i++) {
|
||||
const args = ["bun", index];
|
||||
if (i !== 0 || flags.silent) {
|
||||
args.push("--silent");
|
||||
}
|
||||
Bun.spawn(args, {
|
||||
stdio: ["inherit", "inherit", "inherit"],
|
||||
env: { ...process.env },
|
||||
await import("../../setup");
|
||||
|
||||
for (let i = 0; i < numCpus; i++) {
|
||||
new Worker(index, {
|
||||
type: "module",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue