Skip to content
Snippets Groups Projects
Commit a53977d5 authored by Nicolas Dandrimont's avatar Nicolas Dandrimont
Browse files

cli: Register signal handlers for SIGTERM/SIGINT

When run under a systemd service, the default stop command is to kill the
process; By default, Python doesn't trap sigterm and terminates the process
immediately, so even finally blocks don't run.

This traps SIGTERM and SIGINT (^C) signals and raises an exception, allowing the
process to exit in an orderly fashion.
parent d0e2f59a
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
import logging
import logging.config
import signal
import click
import pkg_resources
......@@ -47,6 +48,12 @@ class AliasedGroup(click.Group):
self.format_commands(ctx, formatter)
def clean_exit_on_signal(signal, frame):
"""Raise a SystemExit exception to let command-line clients wind themselves
down on exit"""
raise SystemExit(0)
@click.group(
context_settings=CONTEXT_SETTINGS, cls=AliasedGroup,
option_notes='''\
......@@ -67,6 +74,9 @@ documented at https://docs.python.org/3/library/logging.config.html.
def swh(ctx, log_level, log_config):
"""Command line interface for Software Heritage.
"""
signal.signal(signal.SIGTERM, clean_exit_on_signal)
signal.signal(signal.SIGINT, clean_exit_on_signal)
if log_level is None and log_config is None:
log_level = 'INFO'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment