From 4ee3d468c7289a66b3b3ab02f0f146acb429a17c Mon Sep 17 00:00:00 2001 From: Konrad Sztyber Date: Wed, 23 Feb 2022 16:34:28 +0100 Subject: [PATCH] sma: handle SIGINT and SIGTERM signals If the app is killed with either one of these signals, it'll shutdown gracefully and exit with zero. Signed-off-by: Konrad Sztyber Change-Id: I630f9e34c3ccb382c8e1b53d0f589f3ec4d1483b Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/11727 Community-CI: Broadcom CI Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Tomasz Zawadzki --- python/spdk/sma/sma.py | 6 ++++-- scripts/sma.py | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/python/spdk/sma/sma.py b/python/spdk/sma/sma.py index a268bb0a4..6bce442c0 100644 --- a/python/spdk/sma/sma.py +++ b/python/spdk/sma/sma.py @@ -24,9 +24,11 @@ class StorageManagementAgent(pb2_grpc.StorageManagementAgentServicer): def register_device(self, device_manager): self._devices[device_manager.protocol] = device_manager - def run(self): + def start(self): self._server.start() - self._server.wait_for_termination() + + def stop(self): + self._server.stop(None) def _find_device_by_name(self, name): return self._devices.get(name) diff --git a/scripts/sma.py b/scripts/sma.py index cfaa7a302..679d720dd 100755 --- a/scripts/sma.py +++ b/scripts/sma.py @@ -4,7 +4,9 @@ from argparse import ArgumentParser import importlib import logging import os +import signal import sys +import threading import yaml sys.path.append(os.path.dirname(__file__) + '/../python') @@ -72,6 +74,20 @@ def load_plugins(plugins, client): return devices +def run(agent): + event = threading.Event() + + def signal_handler(signum, frame): + event.set() + + for signum in [signal.SIGTERM, signal.SIGINT]: + signal.signal(signum, signal_handler) + + agent.start() + event.wait() + agent.stop() + + if __name__ == '__main__': logging.basicConfig(level=os.environ.get('SMA_LOGLEVEL', 'WARNING').upper()) @@ -85,5 +101,4 @@ if __name__ == '__main__': devices += load_plugins(filter(None, os.environ.get('SMA_PLUGINS', '').split(':')), client) register_devices(agent, devices, config) - - agent.run() + run(agent)