import { gracefulShutdown, isShutdownInProgress } from "../../services/shutdown.js";
import { logger } from "../../utils/logger.js";
let exitFn = () => process.exit(0);
/**
 * Override the exit function (for testing).
 */
export function setExitFn(fn) {
    exitFn = fn;
}
export async function stopRoutes(app) {
    app.post("/stop", async (_request, reply) => {
        if (isShutdownInProgress()) {
            return reply.status(200).send({ status: "stopping" });
        }
        logger.info("Received /stop request");
        // Schedule shutdown after response is sent
        setImmediate(() => {
            void gracefulShutdown().then(() => {
                exitFn();
            });
        });
        return reply.status(200).send({ status: "stopping" });
    });
}
//# sourceMappingURL=stop.js.map