Skip to content

Add resilient event broker startup and subscription readiness #28

Closed

Description

@pajoma

Story

As an operator deploying an agent service with NATS or Dapr eventing, I want broker startup and subscription setup to retry asynchronously so the service can become live without accepting traffic before event handling is ready.

Problem

The service startup path can fail when the event broker or Dapr sidecar is not reachable yet. In Kubernetes this can happen during normal pod startup ordering, causing the application process to exit instead of staying live and reporting not-ready until dependencies are available.

Readiness must reflect handler readiness, not just process liveness or socket connectivity.

Acceptance Criteria

  • Event broker connection and subscription setup run asynchronously after application startup.
  • /health/live can succeed while broker startup is still retrying.
  • /health/ready stays unhealthy until broker connectivity and required topic subscriptions are established.
  • NATS and Dapr clients expose consistent retry/readiness behavior.
  • Retry behavior is configurable with event_client_max_retries and event_client_retry_delay.
  • Failure after exhausted retries is logged and leaves the service not-ready.
  • Unit tests cover retry success, retry exhaustion, subscription readiness, and the case where broker connection succeeds but subscription setup fails.
  • Documentation describes broker startup resilience and Kubernetes readiness-probe implications.

Implementation Link

Implemented by PR #27.

Activity

  1. Astrasu commented on Oct 22, 2026

    @Astrasu
    Contributor

    Decision recorded for #96, which closes this issue. One acceptance criterion is met differently from how it is written here:

    Failure after exhausted retries is logged and leaves the service not-ready.

    #96 (CHANGELOG [Unreleased], Breaking 18 and 19) does this instead:

    • The connection loop gives up (event_client_max_retries exhausted), or a resume after a degradation keeps failing: logged CRITICAL, and GET /health/live answers 503 naming the agent until it consumes again -- so Kubernetes restarts the pod.
    • A fault no retry can fix (a durable that filters another subject, a stream the server refuses, a missing JetStream grant, a refused login or deliver subject on a named connection): never retried, logged CRITICAL with what to change; at startup the start fails, after startup liveness answers 503.
    • Transient faults are unchanged: a degraded agent whose fault may heal only affects readiness and never fails liveness. Everything else in this issue's criteria holds as written.

    Why: a client that had given up or was paused reported healthy, so readiness read UP while nothing was consumed -- the state this issue set out to prevent. A pod that stays not-ready for good is never restarted; liveness is what recovers it. Owner decision D23 (with D21) in docs/plans/2026-10-16-pr96-review-fixes.md on the #96 branch.

    The criterion therefore reads, as implemented: failure after exhausted retries is logged CRITICAL and fails liveness until the agent consumes again.

    Written with Claude Code on behalf of the PR author.

  2. pajoma commented on Oct 22, 2026

    @pajoma
    ContributorAuthor

    Agreed in principle. A pod that stays not-ready for good is never restarted, and an agent that can no longer recover by itself is what liveness is for. With event_client_max_retries defaulting to -1, a broker outage alone never trips it. Before #96 closes this issue on that basis, four points (checked on feature/multi-agent-namespaces):

    1. Dapr is not covered. The rewritten criterion is unqualified, but only NATS implements it. DaprClient._on_retry_done logs ERROR after exhausted retries and nothing else, and app_builder.py wires set_orphan_handler for NATSClient only, so a Dapr agent that gave up stays live. That also leaves AC 4 (consistent NATS/Dapr behaviour) unmet. Either scope the criterion to NATS and open a follow-up for Dapr, or wire the orphan handler for Dapr in feat: multi-agent grouping (0.9.0) #96.
    2. Recovery needs an orchestrator that acts on liveness. Kubernetes does. Standalone Docker Compose (restart: unless-stopped plus a healthcheck on /health/live, as in the VerA Portainer stacks) only marks the container unhealthy and never restarts it, so there an orphaned agent stays down: more visible, not recovered. Please say so in the deployment guide. Whether deployments add an autoheal sidecar, or the framework gets an opt-in "exit after N seconds orphaned", can be a follow-up.
    3. "Keeps failing" comes from the probe, not the code. _try_resume reports the agent orphaned on the first failed attempt (nats_client.py:2220). A transient failure only survives because the liveness probe's failureThreshold × periodSeconds (guide example: 3 × 20 s) outlasts the 5 s / 10 s / 20 s resume backoff. Document that coupling next to the probe example, so nobody tightens the probe into restart loops.
    4. The deployment guide contradicts the change. Its "Health probes" table says liveness never depends on a degraded agent and names only deterministic faults; the orphaned case is described only in concepts/observability.md. AC 8 asks for the Kubernetes probe implications, and the deployment guide is where operators look for them.

    Proposed wording for the criterion:

    NATS: failure after exhausted retries (event_client_max_retries ≥ 0), or a resume after a degradation that is still failing when the liveness probe's failure threshold is reached, is logged CRITICAL and fails liveness until the agent consumes again. Dapr: unchanged (logged, service not-ready) or same as NATS, depending on point 1.

    Once point 1 is settled, the criterion in the issue body should be updated to the final wording. The issue also still says "Implemented by PR #27"; with #96 closing it, #27 looks superseded.

    Written with Claude Code on behalf of the issue author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions