/**
 * Graceful Shutdown Module
 *
 * Provides graceful shutdown handling for the INTENT module to ensure
 * in-flight requests are completed during deployment.
 *
 * Features:
 * - Track active HTTP requests
 * - Reject new requests during shutdown with 503 status
 * - Wait for in-flight requests to complete (with timeout)
 * - Coordinate BullMQ worker shutdown
 * - Close database and Redis connections
 * - Comprehensive shutdown metrics logging
 *
 * @packageDocumentation
 */
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
/**
 * Shutdown metrics collected during graceful shutdown
 */
export interface ShutdownMetrics {
    /** Total shutdown duration in milliseconds */
    totalDurationMs: number;
    /** Time spent waiting for HTTP requests */
    httpWaitDurationMs: number;
    /** Time spent waiting for queue jobs */
    jobWaitDurationMs: number;
    /** Number of HTTP requests that were in-flight when shutdown started */
    initialActiveRequests: number;
    /** Number of HTTP requests that completed gracefully */
    completedRequests: number;
    /** Number of HTTP requests that were forcibly terminated */
    forcedTerminatedRequests: number;
    /** Number of queue jobs that were active when shutdown started */
    initialActiveJobs: number;
    /** Number of queue jobs that completed gracefully */
    completedJobs: number;
    /** Number of queue jobs that were abandoned */
    abandonedJobs: number;
    /** Whether the shutdown was forced due to timeout */
    forcedShutdown: boolean;
    /** Shutdown phases and their durations */
    phases: {
        name: string;
        durationMs: number;
        success: boolean;
        error?: string;
    }[];
}
/**
 * Check if the server is currently shutting down
 */
export declare function isServerShuttingDown(): boolean;
/**
 * Get the current number of active requests
 */
export declare function getActiveRequestCount(): number;
/**
 * Get the last shutdown metrics (for monitoring/debugging)
 */
export declare function getLastShutdownMetrics(): ShutdownMetrics | null;
/**
 * Track a new request and return a cleanup function to call when complete.
 * The cleanup function should be called in onResponse hook.
 */
export declare function trackRequest(): () => void;
export interface GracefulShutdownOptions {
    /** Maximum time to wait for requests to complete in milliseconds */
    timeoutMs?: number;
    /** Skip closing database connections (for testing) */
    skipDatabase?: boolean;
    /** Skip closing Redis connections (for testing) */
    skipRedis?: boolean;
    /** Skip worker shutdown (for testing) */
    skipWorkers?: boolean;
    /** Skip scheduler shutdown (for testing) */
    skipScheduler?: boolean;
}
/**
 * Enhanced graceful shutdown options with separate timeouts
 */
export interface EnhancedShutdownOptions {
    /** Maximum time to wait for HTTP requests to complete (default: 10000ms) */
    httpTimeoutMs: number;
    /** Maximum time to wait for queue jobs to complete (default: 15000ms) */
    jobTimeoutMs: number;
    /** Force shutdown after this total time regardless of state (default: 30000ms) */
    forceAfterMs: number;
}
/**
 * Perform enhanced graceful shutdown of the server and all resources.
 *
 * Shutdown sequence:
 * 1. Stop accepting new HTTP requests
 * 2. Wait for in-flight HTTP requests to complete (with timeout)
 * 3. Pause queue workers (stop accepting new jobs)
 * 4. Wait for active jobs to complete (with timeout)
 * 5. Shutdown workers and close queue connections
 * 6. Flush audit system
 * 7. Close database connections
 * 8. Close Redis connections
 * 9. Log shutdown metrics
 *
 * @param options - Shutdown options with separate timeouts for HTTP and job completion
 */
export declare function gracefulShutdown(options: EnhancedShutdownOptions): Promise<ShutdownMetrics>;
export declare function gracefulShutdown(server: FastifyInstance, options?: GracefulShutdownOptions): Promise<void>;
/**
 * Register signal handlers for graceful shutdown.
 *
 * Handles:
 * - SIGTERM: Kubernetes/Docker graceful termination
 * - SIGINT: Ctrl+C in terminal
 * - SIGQUIT: Extended timeout shutdown for debugging
 */
export declare function registerShutdownHandlers(server: FastifyInstance, options?: GracefulShutdownOptions): void;
/**
 * Register enhanced signal handlers with separate HTTP and job timeouts.
 *
 * This is the recommended way to register shutdown handlers for production.
 *
 * @param options - Default shutdown options
 * @param extendedOptions - Options for SIGQUIT with extended timeouts
 */
export declare function registerEnhancedShutdownHandlers(options: EnhancedShutdownOptions, extendedOptions?: EnhancedShutdownOptions): void;
/**
 * Fastify hook to reject requests during shutdown.
 * Add this to server.addHook('onRequest', ...)
 *
 * When the server is shutting down:
 * - Returns 503 Service Unavailable
 * - Includes Retry-After header (RFC 7231)
 * - Provides structured error response
 */
export declare function shutdownRequestHook(request: FastifyRequest, reply: FastifyReply): Promise<void>;
/**
 * Fastify hook to clean up request tracking.
 * Add this to server.addHook('onResponse', ...)
 */
export declare function shutdownResponseHook(request: FastifyRequest): Promise<void>;
/**
 * Reset shutdown state (for testing purposes only)
 */
export declare function resetShutdownState(): void;
//# sourceMappingURL=shutdown.d.ts.map