With the following code we can make sure docker containers which run Python scripts shut down more graceful and delete their KB before exiting. This ensures that the rest of the network immediately gets a notification that the KB is gone and therefore update their state. Otherwise, you need to wait about 1 minute before the whole network is aware that a particular KB is gone.
import signal
...
"""
Make sure that this Python program handles SIGTERM by raising a
KeyboardInterrupt, to end the program.
"""
def handle_sigterm(*args):
raise KeyboardInterrupt()
signal.signal(signal.SIGTERM, handle_sigterm)
...
try:
kb.start_handle_loop()
except KeyboardInterrupt:
log.info("Gracefull shutdown requested...")
finally:
kb.unregister()