Stop new work first
On a shutdown signal, mark the instance as not ready so traffic can drain, then stop accepting new requests or jobs. The exact order depends on the hosting platform’s signal and routing behavior; confirm what the platform actually sends.
Set a maximum drain period shorter than the platform’s forced termination window. If work may exceed it, persist progress or design the operation to resume safely rather than assuming the process will remain alive.
Close resources and verify behavior
After active work drains or reaches its deadline, close database pools, message consumers, and other resources using their supported shutdown methods. Ensure repeated signals and partial initialization are handled without hanging indefinitely.
Test a deployment or controlled restart under representative traffic. Measure interrupted requests, unfinished jobs, and shutdown duration, then tune readiness and timeout settings to match the observed platform behavior.