Skip to content

Commit b05a5e1

Browse files
committed
initial fix
Signed-off-by: hwassman <[email protected]>
1 parent d765036 commit b05a5e1

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

source/queryHandler/SensorConfig.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
zimonFile = '/opt/IBM/zimon'
3333
collectorsFile = '/opt/IBM/zimon/ZIMonCollector.cfg'
3434

35+
def get_config_paths():
36+
files_to_watch = []
37+
if os.path.isfile(mmsdrfsFile):
38+
files_to_watch.append(mmsdrfsFile)
39+
else:
40+
files_to_watch.append(zimonFile)
41+
return files_to_watch
42+
3543

3644
def readSensorsConfigFromMMSDRFS(logger=None):
3745
'''

source/watcher.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020
@author: HWASSMAN
2121
'''
2222

23+
import cherrypy
2324
import os
2425
import time
2526
from bridgeLogger import getBridgeLogger
2627
from messages import MSG
28+
from threading import Thread
2729

2830

2931
class ConfigWatcher(object):
3032
running = False
33+
thread = None
3134
refresh_delay_secs = 30
3235

3336
def __init__(self, watch_paths, call_func_on_change=None, *args, **kwargs):
@@ -39,7 +42,16 @@ def __init__(self, watch_paths, call_func_on_change=None, *args, **kwargs):
3942
self.args = args
4043
self.kwargs = kwargs
4144

42-
def update_files_list(self):
45+
def start_watch(self):
46+
""" Function to start watch in a thread"""
47+
self.running = True
48+
if not self.thread:
49+
self.thread = Thread(name='ConfigWatchThread', target=self.watch)
50+
self.thread.start()
51+
cherrypy.engine.log('Started custom thread %r.' % self.thread.name)
52+
self.logger.debug(MSG['StartWatchingFiles'].format(self.paths))
53+
54+
def _update_files_list(self):
4355
oldfiles = self.filenames.copy()
4456
for path in self.paths:
4557
if os.path.isfile(path):
@@ -54,7 +66,7 @@ def update_files_list(self):
5466
for file in self.filenames.difference(oldfiles):
5567
self.logger.debug(MSG['FileAddedToWatch'].format(file))
5668

57-
def look(self):
69+
def _look(self):
5870
""" Function to check if a file timestamp has changed"""
5971
for filename in self.filenames:
6072
stamp = os.stat(filename).st_mtime
@@ -69,14 +81,12 @@ def look(self):
6981

7082
def watch(self):
7183
""" Function to keep watching in a loop """
72-
self.running = True
73-
self.logger.debug(MSG['StartWatchingFiles'].format(self.paths))
7484
while self.running:
7585
try:
7686
# Look for changes
7787
time.sleep(self.refresh_delay_secs)
78-
self.update_files_list()
79-
self.look()
88+
self._update_files_list()
89+
self._look()
8090
except KeyboardInterrupt:
8191
self.logger.details(MSG['StopWatchingFiles'].format(self.paths))
8292
break
@@ -91,4 +101,11 @@ def watch(self):
91101

92102
def stop_watch(self):
93103
""" Function to break watching """
94-
self.running = False
104+
try:
105+
self.running = False
106+
if self.thread:
107+
self.thread.join()
108+
cherrypy.engine.log('Stopped custom thread %r.' % self.thread.name)
109+
self.thread = None
110+
except KeyboardInterrupt:
111+
print(f"Recived KeyboardInterrupt during stopping the thread {self.thread.name}")

source/zimonGrafanaIntf.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from collections import defaultdict
4242
from timeit import default_timer as timer
4343
from time import sleep
44-
from threading import Thread
4544

4645

4746
class MetadataHandler(metaclass=Singleton):
@@ -177,6 +176,7 @@ def __getSuggest(self, params):
177176
return resp
178177

179178
def __getLookup(self, params):
179+
self.logger.debug(f'Lookup for {params} called')
180180

181181
if params.get('m'):
182182
try:
@@ -616,17 +616,6 @@ def refresh_metadata(refresh_all=False):
616616
md.update(refresh_all)
617617

618618

619-
def watch_config():
620-
files_to_watch = []
621-
if os.path.isfile(SensorConfig.mmsdrfsFile):
622-
files_to_watch.append(SensorConfig.mmsdrfsFile)
623-
else:
624-
files_to_watch.append(SensorConfig.zimonFile)
625-
626-
watcher = ConfigWatcher(files_to_watch, refresh_metadata, refresh_all=True)
627-
watcher.watch()
628-
629-
630619
def main(argv):
631620
# parse input arguments
632621
args, msg = getSettings(argv)
@@ -714,11 +703,13 @@ def main(argv):
714703
logger.info("%s", MSG['sysStart'].format(sys.version, cherrypy.__version__))
715704

716705
try:
706+
files_to_watch = SensorConfig.get_config_paths()
707+
watcher = ConfigWatcher(files_to_watch, refresh_metadata, refresh_all=True)
708+
cherrypy.engine.subscribe('start', watcher.start_watch)
709+
cherrypy.engine.subscribe('stop', watcher.stop_watch)
717710
cherrypy.engine.start()
711+
cherrypy.engine.log('test')
718712
logger.info("server started")
719-
t = Thread(name='ConfigWatchThread', target=watch_config)
720-
t.start()
721-
# t.join()
722713
with open("/proc/{}/stat".format(os.getpid())) as f:
723714
data = f.read()
724715
foreground_pid_of_group = data.rsplit(" ", 45)[1]
@@ -736,7 +727,7 @@ def main(argv):
736727
ph = None
737728
gh = None
738729

739-
logger.warn("server stopped")
730+
logger.warning("server stopped")
740731

741732

742733
if __name__ == '__main__':

tests/test_configWatcher.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ def test_case01():
2121
cw = ConfigWatcher([dummyFile])
2222
assert len(cw.paths) == 1
2323
assert len(cw.filenames) == 0
24-
t = Thread(name='ConfigWatchThread', target=cw.watch)
25-
t.start()
24+
cw.start_watch()
2625
time.sleep(3)
2726
cw.stop_watch()
28-
t.join()
2927
assert len(cw.paths) == 1
3028
assert len(cw.filenames) == 0
3129

@@ -35,10 +33,8 @@ def test_case02():
3533
cw = ConfigWatcher([path])
3634
assert len(cw.paths) > 0
3735
assert len(cw.filenames) == 0
38-
t = Thread(name='ConfigWatchThread', target=cw.watch)
39-
t.start()
36+
cw.start_watch()
4037
time.sleep(3)
4138
cw.stop_watch()
42-
t.join()
4339
assert len(cw.paths) > 0
4440
assert len(cw.filenames) > 1

0 commit comments

Comments
 (0)