let shutdownHandler: (() => Promise<void>) | null = null;
let shuttingDown = false;

export function registerGracefulShutdown(handler: () => Promise<void>) {
  shutdownHandler = handler;
}

export async function triggerGracefulShutdown() {
  if (shuttingDown) return;
  if (!shutdownHandler) throw new Error('未注册停止回调');
  shuttingDown = true;
  await shutdownHandler();
}
