fix(cli): run plugin gateway_stop hooks before message exit (#16580)

Merged via /review-pr -> /prepare-pr -> /merge-pr.

Prepared head SHA: 8542ac77ae183e19a0700c3bb0304ab06bb7d568
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
Gustavo Madeira Santana
2026-02-14 17:33:08 -05:00
committed by GitHub
parent 3821d74019
commit 8217d77ece
5 changed files with 121 additions and 19 deletions

View File

@@ -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).
*/