chore: combine processes
This commit is contained in:
73
src/index.ts
73
src/index.ts
@@ -2,6 +2,8 @@ import { AuroraClient } from "@/lib/BotClient";
|
|||||||
import { env } from "@lib/env";
|
import { env } from "@lib/env";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
import { startWebServerFromRoot } from "./web/src/server";
|
||||||
|
|
||||||
// Load commands & events
|
// Load commands & events
|
||||||
await AuroraClient.loadCommands();
|
await AuroraClient.loadCommands();
|
||||||
await AuroraClient.loadEvents();
|
await AuroraClient.loadEvents();
|
||||||
@@ -9,62 +11,17 @@ await AuroraClient.deployCommands();
|
|||||||
|
|
||||||
console.log("🌐 Starting web server...");
|
console.log("🌐 Starting web server...");
|
||||||
|
|
||||||
const webProjectPath = join(import.meta.dir, "web");
|
|
||||||
const isProduction = process.env.NODE_ENV === "production";
|
|
||||||
let shuttingDown = false;
|
let shuttingDown = false;
|
||||||
|
|
||||||
const startWebServer = () => {
|
const webProjectPath = join(import.meta.dir, "web");
|
||||||
const args = isProduction
|
const webPort = Number(process.env.WEB_PORT) || 3000;
|
||||||
? [process.execPath, "src/index.ts"]
|
const webHost = process.env.HOST || "0.0.0.0";
|
||||||
: [process.execPath, "--hot", "src/index.ts"];
|
|
||||||
|
|
||||||
return Bun.spawn(args, {
|
// Start web server in the same process
|
||||||
cwd: webProjectPath,
|
const webServer = await startWebServerFromRoot(webProjectPath, {
|
||||||
stdout: "inherit",
|
port: webPort,
|
||||||
stderr: "inherit",
|
hostname: webHost,
|
||||||
env: {
|
|
||||||
...process.env,
|
|
||||||
WEB_PORT: process.env.WEB_PORT || "3000",
|
|
||||||
...(process.env.HOST && { WEB_HOST: process.env.HOST }),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
let webServer = startWebServer();
|
|
||||||
|
|
||||||
// Monitor web server and restart on unexpected exit
|
|
||||||
const monitorWebServer = async () => {
|
|
||||||
const exitCode = await webServer.exited;
|
|
||||||
if (!shuttingDown && exitCode !== 0) {
|
|
||||||
console.warn(`⚠️ Web server exited with code ${exitCode}, restarting in 1s...`);
|
|
||||||
await Bun.sleep(1000);
|
|
||||||
webServer = startWebServer();
|
|
||||||
monitorWebServer(); // Continue monitoring the new process
|
|
||||||
}
|
|
||||||
};
|
|
||||||
monitorWebServer();
|
|
||||||
|
|
||||||
// Wait for web server to be ready
|
|
||||||
const waitForWebServer = async (url: string, maxAttempts = 30): Promise<boolean> => {
|
|
||||||
for (let i = 0; i < maxAttempts; i++) {
|
|
||||||
try {
|
|
||||||
const res = await fetch(`${url}/api/health`);
|
|
||||||
if (res.ok) return true;
|
|
||||||
} catch {
|
|
||||||
// Server not ready yet
|
|
||||||
}
|
|
||||||
await Bun.sleep(100);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const webPort = process.env.WEB_PORT || "3000";
|
|
||||||
const webReady = await waitForWebServer(`http://localhost:${webPort}`);
|
|
||||||
if (webReady) {
|
|
||||||
console.log(`✅ Web server ready at http://localhost:${webPort}`);
|
|
||||||
} else {
|
|
||||||
console.warn("⚠️ Web server did not become ready in time, continuing anyway...");
|
|
||||||
}
|
|
||||||
|
|
||||||
// login with the token from .env
|
// login with the token from .env
|
||||||
if (!env.DISCORD_BOT_TOKEN) {
|
if (!env.DISCORD_BOT_TOKEN) {
|
||||||
@@ -73,12 +30,18 @@ if (!env.DISCORD_BOT_TOKEN) {
|
|||||||
AuroraClient.login(env.DISCORD_BOT_TOKEN);
|
AuroraClient.login(env.DISCORD_BOT_TOKEN);
|
||||||
|
|
||||||
// Handle graceful shutdown
|
// Handle graceful shutdown
|
||||||
const shutdownHandler = () => {
|
const shutdownHandler = async () => {
|
||||||
if (shuttingDown) return;
|
if (shuttingDown) return;
|
||||||
shuttingDown = true;
|
shuttingDown = true;
|
||||||
console.log("🛑 Shutdown signal received. Stopping web server...");
|
console.log("🛑 Shutdown signal received. Stopping services...");
|
||||||
webServer.kill();
|
|
||||||
|
// Stop web server
|
||||||
|
await webServer.stop();
|
||||||
|
|
||||||
|
// Stop bot
|
||||||
AuroraClient.shutdown();
|
AuroraClient.shutdown();
|
||||||
|
|
||||||
|
process.exit(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on("SIGINT", shutdownHandler);
|
process.on("SIGINT", shutdownHandler);
|
||||||
|
|||||||
Reference in New Issue
Block a user