mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
refactor: ♻️ Use node:cluster instead of Web Workers
This commit is contained in:
parent
3279f3098b
commit
0a1c2b8cb3
|
|
@ -27,6 +27,7 @@ export enum SonicIndexType {
|
||||||
export class SonicSearchManager {
|
export class SonicSearchManager {
|
||||||
private searchChannel: SonicChannelSearch;
|
private searchChannel: SonicChannelSearch;
|
||||||
private ingestChannel: SonicChannelIngest;
|
private ingestChannel: SonicChannelIngest;
|
||||||
|
private connected = false;
|
||||||
private logger = getLogger("sonic");
|
private logger = getLogger("sonic");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -55,6 +56,10 @@ export class SonicSearchManager {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.connected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
!silent && this.logger.info`Connecting to Sonic...`;
|
!silent && this.logger.info`Connecting to Sonic...`;
|
||||||
|
|
||||||
// Connect to Sonic
|
// Connect to Sonic
|
||||||
|
|
@ -110,6 +115,7 @@ export class SonicSearchManager {
|
||||||
this.searchChannel.ping(),
|
this.searchChannel.ping(),
|
||||||
this.ingestChannel.ping(),
|
this.ingestChannel.ping(),
|
||||||
]);
|
]);
|
||||||
|
this.connected = true;
|
||||||
!silent && this.logger.info`Connected to Sonic`;
|
!silent && this.logger.info`Connected to Sonic`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.fatal`Error while connecting to Sonic: ${error}`;
|
this.logger.fatal`Error while connecting to Sonic: ${error}`;
|
||||||
|
|
|
||||||
|
|
@ -35,17 +35,12 @@ export default class Start extends BaseCommand<typeof 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)
|
// Resolves the path to the main module
|
||||||
const index = (await Bun.file("index.ts").exists())
|
const resolved = import.meta.resolve("../../index");
|
||||||
? "index.ts"
|
|
||||||
: "index.js";
|
|
||||||
|
|
||||||
await import("../../setup");
|
process.env.NUM_CPUS = String(numCpus);
|
||||||
|
process.env.SILENT = flags.silent ? "true" : "false";
|
||||||
|
|
||||||
for (let i = 0; i < numCpus; i++) {
|
await import(resolved);
|
||||||
new Worker(index, {
|
|
||||||
type: "module",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
index.ts
15
index.ts
|
|
@ -1,16 +1,15 @@
|
||||||
import { configureLoggers } from "@/loggers";
|
import cluster from "node:cluster";
|
||||||
import { sentry } from "@/sentry";
|
import { sentry } from "@/sentry";
|
||||||
import { createServer } from "@/server";
|
import { createServer } from "@/server";
|
||||||
import { appFactory } from "~/app";
|
import { appFactory } from "~/app";
|
||||||
import { config } from "~/packages/config-manager/index";
|
import { config } from "~/packages/config-manager/index";
|
||||||
import { setupDatabase } from "./drizzle/db";
|
|
||||||
|
|
||||||
if (import.meta.main) {
|
if (cluster.isPrimary) {
|
||||||
|
for (let i = 0; i < (Number(process.env.NUM_CPUS) ?? 1); i++) {
|
||||||
|
cluster.fork();
|
||||||
|
}
|
||||||
await import("./setup");
|
await import("./setup");
|
||||||
sentry?.captureMessage("Server started", "info");
|
sentry?.captureMessage("Server started", "info");
|
||||||
|
} else {
|
||||||
|
createServer(config, await appFactory());
|
||||||
}
|
}
|
||||||
|
|
||||||
await setupDatabase();
|
|
||||||
await configureLoggers();
|
|
||||||
|
|
||||||
createServer(config, await appFactory());
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue