API and notifier shutdown

You add coordinated SIGTERM shutdown to the API and notifier, stopping each process cleanly before it exits. Both must exit within 10 seconds, and the API must reject an empty or shorter-than-32-byte JWT_SECRET.

Ticket

Add coordinated shutdown to the API in cmd/api and the notifier in cmd/worker. The API reads its listening address from HTTP_ADDR, serves GET /healthz, and exits cleanly when it receives SIGTERM. The notifier must also stop its consumer loops and exit cleanly after SIGTERM.

Deployments need to replace either process without leaving the old API listener or worker running. Week 4 uses this API for checkout and order history. Week 5 builds on the order events this backend publishes.

Acceptance criteria

  • The API listens on the address configured through HTTP_ADDR. GET /healthz returns HTTP 200. After SIGTERM, the API exits with status 0 within 10 seconds, and another process can bind to its listening address.
  • After SIGTERM, the worker exits with status 0 within 10 seconds.
  • The API refuses to start when JWT_SECRET is empty or contains fewer than 32 bytes. In both cases, it exits with a non-zero status within 10 seconds instead of continuing to run.

Constraints

  • Both binaries must exit within 10 seconds of receiving SIGTERM.
  • JWT_SECRET must contain at least 32 bytes for the API to start.

Design direction

Give each binary one owner for its process lifecycle. Create a root context that responds to SIGTERM using operating system signals, then pass it to the long-lived components that need to stop.

In the API, coordinate the HTTP server and the relay. Stop accepting new requests and drain requests already in progress. Wait for the relay to finish before closing the Kafka producer. The relay publishes rows from the transactional outbox, so closing its producer while it is still running can interrupt its work.

In the worker, run both consumer loops with a shared cancellation signal. Wait for each consumer group to finish its cleanup before closing the shared Kafka client and database. Keep shutdown within the process deadline.

Validate JWT_SECRET before starting the API listener. Read the listener address from HTTP_ADDR. Consider the trade-off between waiting for in-progress work and keeping shutdown bounded. Also consider what happens when a goroutine is still using a resource that another part of the process has closed.

Any package or file layout that meets the contract passes.

How to check your work

Run these commands:

bash
cd backend && make up
cd backend && make test W=W3B8

Read the first failing assertion as the contract you broke. It identifies whether the API health check, shutdown status, address release, worker exit, or JWT secret validation needs attention.

Finished Exercise 3.19?

API and worker shutdown

2 items

  • 3.18Signal-driven shutdownnot completed
  • 3.19API and notifier shutdownnot completed

Order service capacity

Up next · Module 7 · 1 items · about 50 min

Start module 7

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. API listens on the address configured through HTTP_ADDR.
  2. API returns HTTP 200 from /healthz.
  3. API exits with status 0 after SIGTERM and releases its address.
  4. Worker exits with status 0 after SIGTERM.
  5. API exits non-zero when JWT_SECRET is empty or shorter than 32 bytes.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=W3B8

Tests

  1. The API serves a health check, exits successfully after SIGTERM and releases its listening address.

    GET/healthz
    Response 200 OK

    Input Start a second API instance on an available address supplied through HTTP_ADDR, request GET /healthz, then send SIGTERM.

    Expected The health check returns HTTP 200. The API exits with status 0 within 10 seconds, and nothing is listening on its address afterward.

    TestW3B8_TheAPIExitsCleanlyWithinTenSecondsOfSIGTERM in backend/e2e/w3_events_test.go