From 9f3a3b501f2fffbecbbe1705b6ff18d3193a8526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 1 Jun 2018 14:01:32 +0200 Subject: [PATCH] Initialise supervisor xmlrpc client lazily to avoid globals --- lib/tfw/components/log_monitor.py | 1 - lib/tfw/mixins/supervisor_mixin.py | 15 ++++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/tfw/components/log_monitor.py b/lib/tfw/components/log_monitor.py index 287618f..d7672d1 100644 --- a/lib/tfw/components/log_monitor.py +++ b/lib/tfw/components/log_monitor.py @@ -29,7 +29,6 @@ class LogMonitor(ObserverMixin): class SendLogWatchdogEventHandler(PatternMatchingWatchdogEventHandler, SupervisorLogMixin): def __init__(self, process_name, log_tail=0): - self.acquire_own_supervisor_instance() # This thread-localises the xmlrpc client self.process_name = process_name self.procinfo = self.supervisor.getProcessInfo(self.process_name) super().__init__([self.procinfo['stdout_logfile'], self.procinfo['stderr_logfile']]) diff --git a/lib/tfw/mixins/supervisor_mixin.py b/lib/tfw/mixins/supervisor_mixin.py index 9360395..bdd43ea 100644 --- a/lib/tfw/mixins/supervisor_mixin.py +++ b/lib/tfw/mixins/supervisor_mixin.py @@ -6,21 +6,14 @@ from xmlrpc.client import Fault as SupervisorFault from contextlib import suppress from os import remove +from tfw.decorators import LazyInitialise from tfw.config import TFWENV -def get_supervisor_instance(): - return xmlrpc.client.ServerProxy(TFWENV.SUPERVISOR_HTTP_URI).supervisor - - class SupervisorBaseMixin: - supervisor = get_supervisor_instance() - - def acquire_own_supervisor_instance(self): - """ - Give this instance non-static, local xmlrpc client - """ - self.supervisor = get_supervisor_instance() + @LazyInitialise + def supervisor(self): + return xmlrpc.client.ServerProxy(TFWENV.SUPERVISOR_HTTP_URI).supervisor class SupervisorMixin(SupervisorBaseMixin):