mirror of https://github.com/openclaw/openclaw.git
refactor(plugins): reuse safe gateway_stop hook runner
This commit is contained in:
parent
b45d4dc388
commit
8542ac77ae
|
|
@ -16,12 +16,24 @@ vi.mock("../../plugin-registry.js", () => ({
|
|||
|
||||
const hasHooksMock = vi.fn(() => false);
|
||||
const runGatewayStopMock = vi.fn(async () => {});
|
||||
const getGlobalHookRunnerMock = vi.fn(() => ({
|
||||
hasHooks: hasHooksMock,
|
||||
runGatewayStop: runGatewayStopMock,
|
||||
}));
|
||||
const runGlobalGatewayStopSafelyMock = vi.fn(
|
||||
async (params: {
|
||||
event: { reason?: string };
|
||||
ctx: Record<string, unknown>;
|
||||
onError?: (err: unknown) => void;
|
||||
}) => {
|
||||
if (!hasHooksMock("gateway_stop")) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await runGatewayStopMock(params.event, params.ctx);
|
||||
} catch (err) {
|
||||
params.onError?.(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
vi.mock("../../../plugins/hook-runner-global.js", () => ({
|
||||
getGlobalHookRunner: () => getGlobalHookRunnerMock(),
|
||||
runGlobalGatewayStopSafely: (...args: unknown[]) => runGlobalGatewayStopSafelyMock(...args),
|
||||
}));
|
||||
|
||||
const exitMock = vi.fn((): never => {
|
||||
|
|
@ -45,10 +57,7 @@ describe("runMessageAction", () => {
|
|||
messageCommandMock.mockReset().mockResolvedValue(undefined);
|
||||
hasHooksMock.mockReset().mockReturnValue(false);
|
||||
runGatewayStopMock.mockReset().mockResolvedValue(undefined);
|
||||
getGlobalHookRunnerMock.mockReset().mockReturnValue({
|
||||
hasHooks: hasHooksMock,
|
||||
runGatewayStop: runGatewayStopMock,
|
||||
});
|
||||
runGlobalGatewayStopSafelyMock.mockClear();
|
||||
exitMock.mockReset().mockImplementation((): never => {
|
||||
throw new Error("exit");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import type { Command } from "commander";
|
|||
import { messageCommand } from "../../../commands/message.js";
|
||||
import { danger, setVerbose } from "../../../globals.js";
|
||||
import { CHANNEL_TARGET_DESCRIPTION } from "../../../infra/outbound/channel-target.js";
|
||||
import { getGlobalHookRunner } from "../../../plugins/hook-runner-global.js";
|
||||
import { runGlobalGatewayStopSafely } from "../../../plugins/hook-runner-global.js";
|
||||
import { defaultRuntime } from "../../../runtime.js";
|
||||
import { runCommandWithRuntime } from "../../cli-utils.js";
|
||||
import { createDefaultDeps } from "../../deps.js";
|
||||
|
|
@ -24,15 +24,11 @@ function normalizeMessageOptions(opts: Record<string, unknown>): Record<string,
|
|||
}
|
||||
|
||||
async function runPluginStopHooks(): Promise<void> {
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (!hookRunner?.hasHooks("gateway_stop")) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await hookRunner.runGatewayStop({ reason: "cli message action complete" }, {});
|
||||
} catch (err) {
|
||||
defaultRuntime.error(danger(`gateway_stop hook failed: ${String(err)}`));
|
||||
}
|
||||
await runGlobalGatewayStopSafely({
|
||||
event: { reason: "cli message action complete" },
|
||||
ctx: {},
|
||||
onError: (err) => defaultRuntime.error(danger(`gateway_stop hook failed: ${String(err)}`)),
|
||||
});
|
||||
}
|
||||
|
||||
export function createMessageCliHelpers(
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ import {
|
|||
import { scheduleGatewayUpdateCheck } from "../infra/update-startup.js";
|
||||
import { startDiagnosticHeartbeat, stopDiagnosticHeartbeat } from "../logging/diagnostic.js";
|
||||
import { createSubsystemLogger, runtimeForLogger } from "../logging/subsystem.js";
|
||||
import { getGlobalHookRunner } from "../plugins/hook-runner-global.js";
|
||||
import { getGlobalHookRunner, runGlobalGatewayStopSafely } from "../plugins/hook-runner-global.js";
|
||||
import { getTotalQueueSize } from "../process/command-queue.js";
|
||||
import { runOnboardingWizard } from "../wizard/onboarding.js";
|
||||
import { createAuthRateLimiter, type AuthRateLimiter } from "./auth-rate-limit.js";
|
||||
|
|
@ -720,19 +720,11 @@ export async function startGatewayServer(
|
|||
return {
|
||||
close: async (opts) => {
|
||||
// Run gateway_stop plugin hook before shutdown
|
||||
{
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (hookRunner?.hasHooks("gateway_stop")) {
|
||||
try {
|
||||
await hookRunner.runGatewayStop(
|
||||
{ reason: opts?.reason ?? "gateway stopping" },
|
||||
{ port },
|
||||
);
|
||||
} catch (err) {
|
||||
log.warn(`gateway_stop hook failed: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
await runGlobalGatewayStopSafely({
|
||||
event: { reason: opts?.reason ?? "gateway stopping" },
|
||||
ctx: { port },
|
||||
onError: (err) => log.warn(`gateway_stop hook failed: ${String(err)}`),
|
||||
});
|
||||
if (diagnosticsEnabled) {
|
||||
stopDiagnosticHeartbeat();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import type { PluginRegistry } from "./registry.js";
|
||||
import type { PluginHookGatewayContext, PluginHookGatewayStopEvent } from "./types.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
import { createHookRunner, type HookRunner } from "./hooks.js";
|
||||
|
||||
|
|
@ -58,6 +59,26 @@ export function hasGlobalHooks(hookName: Parameters<HookRunner["hasHooks"]>[0]):
|
|||
return globalHookRunner?.hasHooks(hookName) ?? false;
|
||||
}
|
||||
|
||||
export async function runGlobalGatewayStopSafely(params: {
|
||||
event: PluginHookGatewayStopEvent;
|
||||
ctx: PluginHookGatewayContext;
|
||||
onError?: (err: unknown) => void;
|
||||
}): Promise<void> {
|
||||
const hookRunner = getGlobalHookRunner();
|
||||
if (!hookRunner?.hasHooks("gateway_stop")) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await hookRunner.runGatewayStop(params.event, params.ctx);
|
||||
} catch (err) {
|
||||
if (params.onError) {
|
||||
params.onError(err);
|
||||
return;
|
||||
}
|
||||
log.warn(`gateway_stop hook failed: ${String(err)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the global hook runner (for testing).
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue