|
| 1 | +''' |
| 2 | +############################################################################## |
| 3 | +# Copyright 2023 IBM Corp. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +############################################################################## |
| 17 | +
|
| 18 | +Created on Dec 9, 2024 |
| 19 | +
|
| 20 | +@author: HWASSMAN |
| 21 | +''' |
| 22 | + |
| 23 | +import cherrypy |
| 24 | +import time |
| 25 | +from bridgeLogger import getBridgeLogger |
| 26 | +from utils import synchronized |
| 27 | +from metaclasses import Singleton |
| 28 | +from metadata import MetadataHandler |
| 29 | +from messages import MSG |
| 30 | +from threading import Thread, Lock |
| 31 | + |
| 32 | +LOCK = Lock() |
| 33 | + |
| 34 | + |
| 35 | +class TopoRefreshManager(object, metaclass=Singleton): |
| 36 | + running = False |
| 37 | + thread = None |
| 38 | + refresh_delay_secs = 30 |
| 39 | + |
| 40 | + def __init__(self, call_func_on_change=None, *args, **kwargs): |
| 41 | + self._cached_stamp = {} |
| 42 | + self.logger = getBridgeLogger() |
| 43 | + self.update_required = False |
| 44 | + self.new_keys = set() |
| 45 | + self.call_func_on_change = call_func_on_change |
| 46 | + self.args = args |
| 47 | + self.kwargs = kwargs |
| 48 | + |
| 49 | + def start_monitor(self): |
| 50 | + """ Function to start monitor in a thread""" |
| 51 | + self.running = True |
| 52 | + if not self.thread: |
| 53 | + self.thread = Thread(name='TopoRefreshManager', target=self.monitor) |
| 54 | + self.thread.start() |
| 55 | + cherrypy.engine.log('Started custom thread %r.' % self.thread.name) |
| 56 | + self.logger.debug(MSG['StartMonitoringTopoChanges']) |
| 57 | + |
| 58 | + @synchronized(LOCK) |
| 59 | + def update_local_cache(self, key): |
| 60 | + """ Function to cache unknown key locally""" |
| 61 | + cache_size = len(self.new_keys) |
| 62 | + self.new_keys = self.new_keys.union(key) |
| 63 | + updated_size = len(self.new_keys) |
| 64 | + if updated_size > cache_size: |
| 65 | + self.logger.trace(MSG['NewKeyDetected'].format('|'.join(key))) |
| 66 | + self.update_required = True |
| 67 | + else: |
| 68 | + self.logger.trace(MSG['NewKeyAlreadyReported'].format('|'.join(key))) |
| 69 | + |
| 70 | + @synchronized(LOCK) |
| 71 | + def clear_local_cache(self): |
| 72 | + """ Function to clear local cache after topo refresh""" |
| 73 | + self.new_keys.clear() |
| 74 | + self.update_required = False |
| 75 | + |
| 76 | + def monitor(self): |
| 77 | + """ Function to keep watching in a loop """ |
| 78 | + while self.running: |
| 79 | + try: |
| 80 | + time.sleep(self.refresh_delay_secs) |
| 81 | + if self.update_required: |
| 82 | + md = MetadataHandler() |
| 83 | + elapsed_time = time.time() - md.getUpdateTime |
| 84 | + self.logger.trace(MSG['ElapsedTimeSinceLastUpdate'].format(elapsed_time)) |
| 85 | + if elapsed_time > 300 or self.new_keys > 100: |
| 86 | + self.logger.info(MSG['RequestMetaUpdate']) |
| 87 | + if self.call_func_on_change is not None: |
| 88 | + self.call_func_on_change(*self.args, **self.kwargs) |
| 89 | + self.clear_local_cache() |
| 90 | + except KeyboardInterrupt: |
| 91 | + self.logger.details(MSG['StopMonitoringTopoChanges']) |
| 92 | + break |
| 93 | + except Exception as e: |
| 94 | + self.logger.warning( |
| 95 | + MSG['UnexpecterError'].format(type(e).__name__)) |
| 96 | + pass |
| 97 | + |
| 98 | + def stop_monitor(self): |
| 99 | + """ Function to break monitoring """ |
| 100 | + try: |
| 101 | + self.running = False |
| 102 | + if self.thread: |
| 103 | + self.thread.join() |
| 104 | + cherrypy.engine.log('Stopped custom thread %r.' % self.thread.name) |
| 105 | + self.thread = None |
| 106 | + except KeyboardInterrupt: |
| 107 | + print(f"Recived KeyboardInterrupt during stopping the thread {self.thread.name}") |
0 commit comments