Shut down gracefully on sigterm (#8)

This commit is contained in:
Jonathan Zernik 2020-03-19 18:10:46 -07:00 committed by GitHub
parent fc2f783bab
commit 88fb0e2ac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,5 +1,7 @@
import argparse
import logging
import signal
import sys
import threading
import time
@ -53,6 +55,11 @@ def start_rpc_server(node):
return server, thread
def sigterm_handler(_signo, _stack_frame):
# Raises SystemExit(0):
sys.exit(0)
def parse_args():
parser = argparse.ArgumentParser(
description="squeaknode runs a node using squeak protocol. ",
@ -117,10 +124,15 @@ def run_client(config):
# start rpc server
rpc_server, rpc_server_thread = start_rpc_server(node)
while True:
time.sleep(10)
signal.signal(signal.SIGTERM, sigterm_handler)
close_db(db)
print("Starting client...")
try:
while True:
time.sleep(1)
finally:
print("Shutting down...")
close_db(db)
if __name__ == '__main__':