feat(hooks): emit gateway shutdown lifecycle events

This commit is contained in:
Egor Dementyev
2026-04-11 21:08:01 +02:00
committed by Val Alexander
parent a3144b6bfd
commit 59c70595f0

View File

@@ -5,6 +5,7 @@ import { disposeAllSessionMcpRuntimes } from "../agents/pi-bundle-mcp-tools.js";
import type { CanvasHostHandler, CanvasHostServer } from "../canvas-host/server.js";
import { type ChannelId, listChannelPlugins } from "../channels/plugins/index.js";
import { stopGmailWatcher } from "../hooks/gmail-watcher.js";
import { createInternalHookEvent, triggerInternalHook } from "../hooks/internal-hooks.js";
import type { HeartbeatRunner } from "../infra/heartbeat-runner.js";
import { createSubsystemLogger } from "../logging/subsystem.js";
import type { PluginServicesHandle } from "../plugins/services.js";
@@ -113,6 +114,27 @@ export function createGatewayCloseHandler(params: {
typeof opts?.restartExpectedMs === "number" && Number.isFinite(opts.restartExpectedMs)
? Math.max(0, Math.floor(opts.restartExpectedMs))
: null;
try {
const shutdownEvent = createInternalHookEvent("gateway", "shutdown", "gateway:shutdown", {
reason,
restartExpectedMs,
});
await triggerInternalHook(shutdownEvent);
if (restartExpectedMs !== null) {
const preRestartEvent = createInternalHookEvent(
"gateway",
"pre-restart",
"gateway:pre-restart",
{
reason,
restartExpectedMs,
},
);
await triggerInternalHook(preRestartEvent);
}
} catch {
// Best-effort only; shutdown should proceed even if hooks fail.
}
if (params.bonjourStop) {
try {
await params.bonjourStop();