From 00bcefcccaca1a3ebc2ee5410993ca02507b5270 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Wed, 2 Dec 2020 11:15:56 +0100 Subject: [PATCH] Allow sending USR signals to pod --- .idea/misc.xml | 2 +- main.py | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 479ee61..608768e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/main.py b/main.py index 9f4bf93..385643e 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,8 @@ import sys import threading import traceback from datetime import datetime -from signal import signal, SIGHUP, SIGINT, SIGTERM, setitimer, SIGALRM, ITIMER_REAL +from queue import SimpleQueue +from signal import signal, SIGHUP, SIGINT, SIGTERM, setitimer, SIGALRM, ITIMER_REAL, SIGUSR1, SIGUSR2, strsignal import click import sh @@ -37,6 +38,8 @@ class PodKeeper: self.checking = threading.Event() self.waiter = threading.Event() self.last_check = datetime.utcnow() + self.passing_signal = threading.Event() + self.pass_signal_nums = SimpleQueue() def destroy(self, signum, stackframe): print("Destroy signal", signum, file=sys.stderr, flush=True) @@ -52,6 +55,11 @@ class PodKeeper: self.checking.set() self.waiter.set() + def passthrough(self, signum, stackframe): + self.pass_signal_nums.put(item=signum, block=True, timeout=3) + self.passing_signal.set() + self.waiter.set() + def run(self): os.chdir(self.podhome) if self.stop_previous and podman.pod.exists(self.podname, _ok_code=[0, 1]).exit_code == 0: @@ -68,25 +76,31 @@ class PodKeeper: self.waiter.wait() self.waiter.clear() + if self.passing_signal.is_set(): + self.passing_signal.clear() + while not self.pass_signal_nums.empty(): + signum = self.pass_signal_nums.get(block=True, timeout=2) + self.signal_pod(signum) + if self.checking.is_set(): self.checking.clear() self.check_pod() if self.reloading.is_set(): self.reloading.clear() - self.reload_pod() + self.signal_pod(SIGHUP) if 'NOTIFY_SOCKET' in os.environ: sdnotify("--status=Stopping pod") finally: self.stop_pod() - def reload_pod(self): - print("Reloading pod", self.podname, file=sys.stderr, flush=True) + def signal_pod(self, signum): + print(f"Sending signal '{strsignal(signum)}' to pod {self.podname}", file=sys.stderr, flush=True) try: - podman.pod.kill("--signal", "HUP", self.podname) + podman.pod.kill("--signal", str(signum), self.podname) except sh.ErrorReturnCode: - print("Error reloading pod", file=sys.stderr, flush=True) + print("Error signaling pod", file=sys.stderr, flush=True) traceback.print_exc() def check_pod(self): @@ -131,6 +145,8 @@ def main(network, stop_previous, identifier): signal(SIGTERM, keeper.destroy) signal(SIGHUP, keeper.reload) signal(SIGALRM, keeper.check) + signal(SIGUSR1, keeper.passthrough) + signal(SIGUSR2, keeper.passthrough) setitimer(ITIMER_REAL, 4.0, 10.0) keeper.run()