Module Message_templates.Shutdown

Structured shutdown protocol for graceful cleanup

This module provides a mechanism for registering cleanup handlers and executing them during application shutdown with configurable strategies including timeout protection.

Example:

  let shutdown = Shutdown.create () in

  (* Register cleanup handlers *)
  Shutdown.register shutdown (fun () -> flush_pending_logs ());

  (* Graceful shutdown with timeout *)
  Shutdown.execute shutdown (Shutdown.Graceful 5.0)
type shutdown_strategy =
  1. | Immediate
    (*

    Execute all handlers synchronously, no waiting

    *)
  2. | Flush_pending
    (*

    Execute handlers concurrently, wait for all

    *)
  3. | Graceful of float
    (*

    Execute with timeout in seconds

    *)

Shutdown strategy determines how handlers are executed

type t

Shutdown controller (opaque)

val create : unit -> t

Create a new shutdown controller

  • returns

    A new shutdown controller

val register : t -> (unit -> unit) -> unit

Register a cleanup handler

Handlers are executed in reverse registration order (LIFO).

  • parameter t

    The shutdown controller

  • parameter handler

    Function to call during shutdown

val execute : t -> shutdown_strategy -> unit

Execute shutdown with specified strategy

  • parameter t

    The shutdown controller

  • parameter strategy

    How to execute handlers

  • raises Failure

    if shutdown already executed

val is_shutdown : t -> bool

Check if shutdown has been executed

  • parameter t

    The shutdown controller

  • returns

    true if shutdown has already executed

val reset : t -> unit

Reset the shutdown controller (for testing)

Clears all handlers and resets shutdown state.

  • parameter t

    The shutdown controller