Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully shutdown in response to SIGTERM #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions duologsync/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def main():
args = arg_parser.parse_args()

# Handle shutting down the program via Ctrl-C
signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGINT, signal_handler)
# Handle shutting down the program via SIGTERM
signal.signal(signal.SIGTERM, signal_handler)

# Create a config Dictionary from a YAML file located at args.ConfigPath
config = Config.create_config(args.ConfigPath)
Expand Down Expand Up @@ -82,16 +84,20 @@ def main():
print(f"DuoLogSync: shutdown successfully. Check "
f"{Config.get_log_filepath()} for program logs")

def sigint_handler(signal_number, stack_frame):
def signal_handler(signal_number, stack_frame):
"""
Handler for SIGINT (Ctrl-C) to gracefully shutdown DuoLogSync
Handler for signals to gracefully shutdown DuoLogSync
"""

shutdown_reason = f"received signal {signal_number} (Ctrl-C)"
if signal_number == signal.SIGINT:
shutdown_reason = f"received signal {signal_number} (Ctrl-C)"
else:
shutdown_reason = f"received signal {signal.strsignal(signal_number)}"

Program.initiate_shutdown(shutdown_reason)

if stack_frame:
Program.log(f"DuoLogSync: stack frame from Ctrl-C is {stack_frame}",
Program.log(f"DuoLogSync: stack frame from signal is {stack_frame}",
logging.INFO)

def create_tasks(server_to_writer):
Expand Down