From ddeb1f692ec4fdcf5856d4509af85dfbf5969aaf Mon Sep 17 00:00:00 2001 From: bkfox Date: Wed, 11 Jan 2017 09:39:55 +0100 Subject: [PATCH] change technic to detect running liquidsoap instance --- aircox/controllers.py | 26 +++++++++----------------- requirements.txt | 1 + 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/aircox/controllers.py b/aircox/controllers.py index fa67d47..bbda4f3 100755 --- a/aircox/controllers.py +++ b/aircox/controllers.py @@ -141,21 +141,17 @@ class Streamer: """ Check if there is a process that has not been killed """ - # TODO: this method does not work in case the working - # directory has been erased then regenerated - if not os.path.exists(self.path + ".pid"): + if not os.path.exists(self.socket_path): return - with open(self.path + ".pid") as file: - pid = file.read() - os.kill(int(pid), signal.SIGKILL) - - def __prevent_zombie(self): - """ - Write process pid - """ - with open(self.path + ".pid", "w") as file: - file.write(str(self.process.pid)) + import psutil + conns = [ + conn for conn in psutil.net_connections(kind='unix') + if conn.laddr == self.socket_path + ] + for conn in conns: + if conn.pid is not None: + os.kill(conn.pid, signal.SIGKILL) def process_run(self): """ @@ -174,7 +170,6 @@ class Streamer: self.__check_for_zombie() self.process = subprocess.Popen(args, stderr=subprocess.STDOUT) - self.__prevent_zombie() atexit.register(lambda: self.process_terminate()) def process_terminate(self): @@ -186,9 +181,6 @@ class Streamer: self.process.kill() self.process = None - # the zombie has been killed - os.remove(self.path + ".pid") - def process_wait(self): """ Wait for the process to terminate if there is a process diff --git a/requirements.txt b/requirements.txt index 50e7ff0..5a3d699 100755 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ Django>=1.10.3 django-taggit>=0.18.3 mutagen==1.35.1 watchdog>=0.8.3 +psutil>=5.0.1 dateutils>=0.6.6 bleach>=1.4.3 django-htmlmin>=0.10.0