Allow sending USR signals to pod
This commit is contained in:
parent
7d461546aa
commit
00bcefccca
|
@ -1,4 +1,4 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (.venv)" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (podlaunch)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
28
main.py
28
main.py
|
@ -5,7 +5,8 @@ import sys
|
||||||
import threading
|
import threading
|
||||||
import traceback
|
import traceback
|
||||||
from datetime import datetime
|
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 click
|
||||||
import sh
|
import sh
|
||||||
|
@ -37,6 +38,8 @@ class PodKeeper:
|
||||||
self.checking = threading.Event()
|
self.checking = threading.Event()
|
||||||
self.waiter = threading.Event()
|
self.waiter = threading.Event()
|
||||||
self.last_check = datetime.utcnow()
|
self.last_check = datetime.utcnow()
|
||||||
|
self.passing_signal = threading.Event()
|
||||||
|
self.pass_signal_nums = SimpleQueue()
|
||||||
|
|
||||||
def destroy(self, signum, stackframe):
|
def destroy(self, signum, stackframe):
|
||||||
print("Destroy signal", signum, file=sys.stderr, flush=True)
|
print("Destroy signal", signum, file=sys.stderr, flush=True)
|
||||||
|
@ -52,6 +55,11 @@ class PodKeeper:
|
||||||
self.checking.set()
|
self.checking.set()
|
||||||
self.waiter.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):
|
def run(self):
|
||||||
os.chdir(self.podhome)
|
os.chdir(self.podhome)
|
||||||
if self.stop_previous and podman.pod.exists(self.podname, _ok_code=[0, 1]).exit_code == 0:
|
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.wait()
|
||||||
self.waiter.clear()
|
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():
|
if self.checking.is_set():
|
||||||
self.checking.clear()
|
self.checking.clear()
|
||||||
self.check_pod()
|
self.check_pod()
|
||||||
|
|
||||||
if self.reloading.is_set():
|
if self.reloading.is_set():
|
||||||
self.reloading.clear()
|
self.reloading.clear()
|
||||||
self.reload_pod()
|
self.signal_pod(SIGHUP)
|
||||||
|
|
||||||
if 'NOTIFY_SOCKET' in os.environ:
|
if 'NOTIFY_SOCKET' in os.environ:
|
||||||
sdnotify("--status=Stopping pod")
|
sdnotify("--status=Stopping pod")
|
||||||
finally:
|
finally:
|
||||||
self.stop_pod()
|
self.stop_pod()
|
||||||
|
|
||||||
def reload_pod(self):
|
def signal_pod(self, signum):
|
||||||
print("Reloading pod", self.podname, file=sys.stderr, flush=True)
|
print(f"Sending signal '{strsignal(signum)}' to pod {self.podname}", file=sys.stderr, flush=True)
|
||||||
try:
|
try:
|
||||||
podman.pod.kill("--signal", "HUP", self.podname)
|
podman.pod.kill("--signal", str(signum), self.podname)
|
||||||
except sh.ErrorReturnCode:
|
except sh.ErrorReturnCode:
|
||||||
print("Error reloading pod", file=sys.stderr, flush=True)
|
print("Error signaling pod", file=sys.stderr, flush=True)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
def check_pod(self):
|
def check_pod(self):
|
||||||
|
@ -131,6 +145,8 @@ def main(network, stop_previous, identifier):
|
||||||
signal(SIGTERM, keeper.destroy)
|
signal(SIGTERM, keeper.destroy)
|
||||||
signal(SIGHUP, keeper.reload)
|
signal(SIGHUP, keeper.reload)
|
||||||
signal(SIGALRM, keeper.check)
|
signal(SIGALRM, keeper.check)
|
||||||
|
signal(SIGUSR1, keeper.passthrough)
|
||||||
|
signal(SIGUSR2, keeper.passthrough)
|
||||||
setitimer(ITIMER_REAL, 4.0, 10.0)
|
setitimer(ITIMER_REAL, 4.0, 10.0)
|
||||||
|
|
||||||
keeper.run()
|
keeper.run()
|
||||||
|
|
Loading…
Reference in a new issue