refactor(agents): split local agent bootstrap from controller startup
This commit is contained in:
parent
11e9fbcc44
commit
3a9f7c96d2
3 changed files with 30 additions and 17 deletions
|
|
@ -9,7 +9,7 @@ vi.mock("node:child_process", async () => {
|
||||||
return { spawn: spawnMock };
|
return { spawn: spawnMock };
|
||||||
});
|
});
|
||||||
|
|
||||||
let spawnLocalAgent: (typeof import("../agents-manager"))["spawnLocalAgent"];
|
let startLocalAgent: (typeof import("../agents-manager"))["startLocalAgent"];
|
||||||
let stopLocalAgent: (typeof import("../agents-manager"))["stopLocalAgent"];
|
let stopLocalAgent: (typeof import("../agents-manager"))["stopLocalAgent"];
|
||||||
|
|
||||||
const processWithAgentRuntime = process as ProcessWithAgentRuntime;
|
const processWithAgentRuntime = process as ProcessWithAgentRuntime;
|
||||||
|
|
@ -50,7 +50,7 @@ const createFakeChild = () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
vi.resetModules();
|
vi.resetModules();
|
||||||
setAgentRuntime();
|
setAgentRuntime();
|
||||||
({ spawnLocalAgent, stopLocalAgent } = await import("../agents-manager"));
|
({ startLocalAgent, stopLocalAgent } = await import("../agents-manager"));
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
|
|
@ -68,7 +68,7 @@ test("respawns the local agent after an unexpected exit", async () => {
|
||||||
const secondChild = createFakeChild();
|
const secondChild = createFakeChild();
|
||||||
spawnMock.mockReturnValueOnce(firstChild).mockReturnValueOnce(secondChild);
|
spawnMock.mockReturnValueOnce(firstChild).mockReturnValueOnce(secondChild);
|
||||||
|
|
||||||
await spawnLocalAgent();
|
await startLocalAgent();
|
||||||
|
|
||||||
firstChild.exitCode = 1;
|
firstChild.exitCode = 1;
|
||||||
firstChild.emit("exit", 1, null);
|
firstChild.emit("exit", 1, null);
|
||||||
|
|
@ -84,7 +84,7 @@ test("does not respawn the local agent after an intentional stop", async () => {
|
||||||
const child = createFakeChild();
|
const child = createFakeChild();
|
||||||
spawnMock.mockReturnValue(child);
|
spawnMock.mockReturnValue(child);
|
||||||
|
|
||||||
await spawnLocalAgent();
|
await startLocalAgent();
|
||||||
await stopLocalAgent();
|
await stopLocalAgent();
|
||||||
|
|
||||||
await vi.advanceTimersByTimeAsync(1_000);
|
await vi.advanceTimersByTimeAsync(1_000);
|
||||||
|
|
|
||||||
|
|
@ -147,7 +147,7 @@ const backupEventHandlers: AgentBackupEventHandlers = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const startAgentRuntime = async () => {
|
export const startAgentController = async () => {
|
||||||
const runtime = getAgentRuntimeState();
|
const runtime = getAgentRuntimeState();
|
||||||
|
|
||||||
if (runtime.agentManager) {
|
if (runtime.agentManager) {
|
||||||
|
|
@ -163,6 +163,13 @@ export const startAgentRuntime = async () => {
|
||||||
runtime.agentManager = nextAgentManager;
|
runtime.agentManager = nextAgentManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const stopAgentController = async () => {
|
||||||
|
const runtime = getAgentRuntimeState();
|
||||||
|
const agentManagerRuntime = runtime.agentManager;
|
||||||
|
runtime.agentManager = null;
|
||||||
|
await agentManagerRuntime?.stop();
|
||||||
|
};
|
||||||
|
|
||||||
export const agentManager = {
|
export const agentManager = {
|
||||||
runBackup: async (agentId: string, request: AgentRunBackupRequest) => {
|
runBackup: async (agentId: string, request: AgentRunBackupRequest) => {
|
||||||
const runtime = getAgentManagerRuntime();
|
const runtime = getAgentManagerRuntime();
|
||||||
|
|
@ -213,7 +220,7 @@ export const agentManager = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const spawnLocalAgent = async () => {
|
export const startLocalAgent = async () => {
|
||||||
await spawnLocalAgentProcess(getAgentRuntimeState());
|
await spawnLocalAgentProcess(getAgentRuntimeState());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -221,8 +228,3 @@ export const spawnLocalAgent = async () => {
|
||||||
export const stopLocalAgent = async () => {
|
export const stopLocalAgent = async () => {
|
||||||
await stopLocalAgentProcess(getAgentRuntimeState());
|
await stopLocalAgentProcess(getAgentRuntimeState());
|
||||||
};
|
};
|
||||||
|
|
||||||
export const stopAgentRuntime = async () => {
|
|
||||||
await getAgentManagerRuntime()?.stop();
|
|
||||||
await stopLocalAgent();
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { runDbMigrations } from "../../db/db";
|
import { runDbMigrations } from "../../db/db";
|
||||||
import { spawnLocalAgent, startAgentRuntime, stopAgentRuntime } from "../agents/agents-manager";
|
import { config } from "../../core/config";
|
||||||
|
import { startAgentController, startLocalAgent, stopAgentController, stopLocalAgent } from "../agents/agents-manager";
|
||||||
import { runMigrations } from "./migrations";
|
import { runMigrations } from "./migrations";
|
||||||
import { startup } from "./startup";
|
import { startup } from "./startup";
|
||||||
|
|
||||||
|
|
@ -8,11 +9,20 @@ let bootstrapPromise: Promise<void> | undefined;
|
||||||
const runBootstrap = async () => {
|
const runBootstrap = async () => {
|
||||||
await runDbMigrations();
|
await runDbMigrations();
|
||||||
await runMigrations();
|
await runMigrations();
|
||||||
if (process.env.ENABLE_LOCAL_AGENT === "true") {
|
|
||||||
await startAgentRuntime();
|
try {
|
||||||
await spawnLocalAgent();
|
await startAgentController();
|
||||||
|
|
||||||
|
if (config.flags.enableLocalAgent) {
|
||||||
|
await startLocalAgent();
|
||||||
|
}
|
||||||
|
|
||||||
|
await startup();
|
||||||
|
} catch (error) {
|
||||||
|
await stopLocalAgent();
|
||||||
|
await stopAgentController();
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
await startup();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const bootstrapApplication = async () => {
|
export const bootstrapApplication = async () => {
|
||||||
|
|
@ -30,7 +40,8 @@ export const bootstrapApplication = async () => {
|
||||||
|
|
||||||
export const stopApplicationRuntime = async () => {
|
export const stopApplicationRuntime = async () => {
|
||||||
try {
|
try {
|
||||||
await stopAgentRuntime();
|
await stopLocalAgent();
|
||||||
|
await stopAgentController();
|
||||||
} finally {
|
} finally {
|
||||||
bootstrapPromise = undefined;
|
bootstrapPromise = undefined;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue