Module: check_mk
Branch: master
Commit: 36d373c4b27b2d4ce629024211bcb4fc43975403
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=36d373c4b27b2d…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Jun 5 13:33:37 2018 +0200
Fixed a few import cycles by using a naive observer pattern.
This brings the number of cycles down to 15 (from 31).
Change-Id: Icaef89dd03d56c9dece501171a452b93e6c16fef
---
cmk_base/checks.py | 3 +++
cmk_base/cleanup.py | 26 ++++++++++++++++----------
cmk_base/core.py | 3 +++
cmk_base/item_state.py | 4 ++++
cmk_base/snmp.py | 3 +++
5 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/cmk_base/checks.py b/cmk_base/checks.py
index 11f9075..028cc2c 100644
--- a/cmk_base/checks.py
+++ b/cmk_base/checks.py
@@ -43,6 +43,7 @@ import cmk_base.rulesets as rulesets
import cmk_base.config as config
import cmk_base.console as console
import cmk_base.check_api as check_api
+import cmk_base.cleanup
# TODO: Cleanup access to check_info[] -> replace it by different function calls
# like for example check_exists(...)
@@ -884,3 +885,5 @@ def filter_by_management_board(hostname, found_check_plugin_names,
final_collection.update(host_only)
return final_collection
+
+cmk_base.cleanup.register_cleanup(lambda: set_hostname("unknown"))
diff --git a/cmk_base/cleanup.py b/cmk_base/cleanup.py
index ba1392e..3bf805e 100644
--- a/cmk_base/cleanup.py
+++ b/cmk_base/cleanup.py
@@ -24,18 +24,24 @@
# to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA.
-"""Hacky module to avoid cyclic imports. This should
die..."""
+"""Hacky module to avoid cyclic imports, using a naive observer pattern.
+This should die..."""
+
+
+cleanup_functions = set()
+
+
+def register_cleanup(func):
+ cleanup_functions.add(func)
+
+
+def unregister_cleanup(func):
+ cleanup_functions.remove(func)
+
# Reset some global variable to their original value. This is needed in
# keepalive mode. We could in fact do some positive caching in keepalive mode,
# e.g. the counters of the hosts could be saved in memory.
def cleanup_globals():
- # THIS IS HORRIBLE! We can't move the imports to the global scope because of
cycles...
- import cmk_base.checks
- cmk_base.checks.set_hostname("unknown")
- import cmk_base.item_state
- cmk_base.item_state.cleanup_item_states()
- import cmk_base.core
- cmk_base.core.cleanup_timeperiod_caches()
- import cmk_base.snmp
- cmk_base.snmp.cleanup_host_caches()
+ for func in cleanup_functions:
+ func()
diff --git a/cmk_base/core.py b/cmk_base/core.py
index e471f8e..c5d6ce9 100644
--- a/cmk_base/core.py
+++ b/cmk_base/core.py
@@ -44,6 +44,7 @@ import cmk_base.core_config as core_config
import cmk_base.core_nagios as core_nagios
from cmk_base.exceptions import MKTimeout
from cmk_base import config_cache
+import cmk_base.cleanup
try:
import cmk_base.cee.core_cmc as core_cmc
@@ -232,3 +233,5 @@ def update_timeperiods_cache():
def cleanup_timeperiod_caches():
config_cache.get_dict("timeperiods_cache").clear()
+
+cmk_base.cleanup.register_cleanup(cleanup_timeperiod_caches)
diff --git a/cmk_base/item_state.py b/cmk_base/item_state.py
index d610bed..e09c190 100644
--- a/cmk_base/item_state.py
+++ b/cmk_base/item_state.py
@@ -46,6 +46,7 @@ import traceback
import cmk.paths
import cmk.store
from cmk.exceptions import MKGeneralException
+import cmk_base.cleanup
# Constants for counters
SKIP = None
@@ -365,3 +366,6 @@ def get_average(itemname, this_time, this_val, backlog_minutes,
initialize_zero
set_item_state(itemname, (this_time, new_val))
return new_val
+
+
+cmk_base.cleanup.register_cleanup(cleanup_item_states)
diff --git a/cmk_base/snmp.py b/cmk_base/snmp.py
index 506b130..cdcaa32 100644
--- a/cmk_base/snmp.py
+++ b/cmk_base/snmp.py
@@ -858,3 +858,6 @@ def do_snmpget(*args):
value = get_single_oid(access_data, oid)
console.output("%s (%s): %r\n" % (hostname, ipaddress, value))
cmk_base.cleanup.cleanup_globals()
+
+
+cmk_base.cleanup.register_cleanup(cleanup_host_caches)