Graceful termination using signals
To gracefully terminate a program, you should do the following:
- No longer accept new requests
- Finish any requests that are accepted but not completed
- Allow a certain amount of time for any long-running processes to finish, and terminate them if they cannot be completed in the given time
Graceful termination is especially important in cloud-based service development because most cloud services are ephemeral and they get replaced by new instances often. This recipe shows how it can be done.
How to do it...
- Handle interrupt and termination signals. An interrupt signal (
SIGINT
) is usually initiated by the user (for instance, by pressing Ctrl + C), and a termination signal (SIGTERM
) is usually initiated by the host operating system, or for a containerized environment, the container orchestration system. - Disable acceptance of any new requests.
- Wait for existing requests to complete with a timeout
- Terminate the...