From 960313877792aca88773a987f400cfab4546a665 Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Thu, 10 Oct 2019 12:09:11 +0700 Subject: [PATCH 1/4] Added SIGINT signal handler --- pisa/pisad.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pisa/pisad.py b/pisa/pisad.py index 152d7c9..e1864c6 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -1,5 +1,6 @@ -from sys import argv +from sys import argv, exit from getopt import getopt +from signal import signal, SIGINT from pisa.logger import Logger from pisa.api import start_api @@ -7,7 +8,17 @@ from pisa.tools import can_connect_to_bitcoind, in_correct_network logger = Logger("Daemon") + +def handle_sigint(signal_received, frame): + print("Shutting down PISA...") + # TODO: add code to close the db, free any resources, etc. + + exit(0) + + if __name__ == '__main__': + signal(SIGINT, handle_sigint) + debug = False opts, _ = getopt(argv[1:], 'd', ['debug']) for opt, arg in opts: From 99f3400d638d0f938ae1939c18d74660223226f6 Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Thu, 10 Oct 2019 22:37:06 +0700 Subject: [PATCH 2/4] Added SIGTERM and SIGQUIT --- pisa/pisad.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pisa/pisad.py b/pisa/pisad.py index e1864c6..593c19b 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -1,6 +1,6 @@ from sys import argv, exit from getopt import getopt -from signal import signal, SIGINT +from signal import signal, SIGINT, SIGQUIT, SIGTERM from pisa.logger import Logger from pisa.api import start_api @@ -9,7 +9,7 @@ from pisa.tools import can_connect_to_bitcoind, in_correct_network logger = Logger("Daemon") -def handle_sigint(signal_received, frame): +def handle_signals(signal_received, frame): print("Shutting down PISA...") # TODO: add code to close the db, free any resources, etc. @@ -17,7 +17,9 @@ def handle_sigint(signal_received, frame): if __name__ == '__main__': - signal(SIGINT, handle_sigint) + signal(SIGINT, handle_signals) + signal(SIGTERM, handle_signals) + signal(SIGQUIT, handle_signals) debug = False opts, _ = getopt(argv[1:], 'd', ['debug']) From 6348319dfb3be2b9ce633d2ca332ccbb32a17a78 Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Fri, 11 Oct 2019 09:26:29 +0700 Subject: [PATCH 3/4] Using logging instead of print; added startup log message --- pisa/pisad.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pisa/pisad.py b/pisa/pisad.py index 593c19b..751aa53 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -10,13 +10,15 @@ logger = Logger("Daemon") def handle_signals(signal_received, frame): - print("Shutting down PISA...") + logger.info("Shutting down PISA") # TODO: add code to close the db, free any resources, etc. exit(0) if __name__ == '__main__': + logger.info("Starting PISA") + signal(SIGINT, handle_signals) signal(SIGTERM, handle_signals) signal(SIGQUIT, handle_signals) From 8acff789fe2985bfe0e863f8dc96a9a9e39fb69c Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Fri, 11 Oct 2019 11:27:03 +0700 Subject: [PATCH 4/4] Add reference to github issue for the Todo --- pisa/pisad.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pisa/pisad.py b/pisa/pisad.py index 751aa53..ac5bf86 100644 --- a/pisa/pisad.py +++ b/pisa/pisad.py @@ -11,7 +11,7 @@ logger = Logger("Daemon") def handle_signals(signal_received, frame): logger.info("Shutting down PISA") - # TODO: add code to close the db, free any resources, etc. + # TODO: #11-add-graceful-shutdown: add code to close the db, free any resources, etc. exit(0)