Skip to content

Commit 12f5b2b

Browse files
committed
[chg] Use redis lock to avoid conflicting cache access and simultaneous check runs
1 parent f9de437 commit 12f5b2b

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

opsiconfd/check/cache.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
from redis.exceptions import ConnectionError
1414

1515
from opsiconfd.logging import logger
16-
from opsiconfd.redis import delete_recursively
16+
from opsiconfd.redis import delete_recursively, redis_lock
1717

1818

1919
def check_cache_clear(cache_id: str = "all") -> Any:
20-
try:
21-
logger.debug("Clearing check cache: %s", cache_id)
22-
redis_key = "opsiconfd:checkcache"
23-
if cache_id != "all":
24-
redis_key = f"{redis_key}:{cache_id}"
25-
delete_recursively(redis_key)
26-
except ConnectionError as err:
27-
logger.warning("Could not clear check cache. No Connection to Redis.")
28-
logger.debug(err)
20+
with redis_lock("check-cache", acquire_timeout=60.0, lock_timeout=300.0):
21+
try:
22+
logger.debug("Clearing check cache: %s", cache_id)
23+
redis_key = "opsiconfd:checkcache"
24+
if cache_id != "all":
25+
redis_key = f"{redis_key}:{cache_id}"
26+
delete_recursively(redis_key)
27+
except ConnectionError as err:
28+
logger.warning("Could not clear check cache. No Connection to Redis.")
29+
logger.debug(err)
2930

3031

3132
def clear_check_cache(

opsiconfd/check/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def console_health_check() -> int:
115115
check_version = "1000"
116116
else:
117117
check_version = config.upgrade_check
118+
118119
if config.clear_cache:
119120
check_cache_clear("all")
120121

opsiconfd/check/main.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111

1212
from opsiconfd.check.common import CheckResult, check_manager
1313
from opsiconfd.logging import logger
14+
from opsiconfd.redis import redis_lock
1415

1516

1617
def health_check(clear_cache: bool = False) -> Iterator[CheckResult]:
1718
from opsiconfd.check.register import register_checks
1819

19-
register_checks()
20-
if not check_manager.check_ids:
21-
logger.error("No valid checks selected. Please check your configuration.")
22-
return
23-
for check in check_manager:
24-
yield check.run(clear_cache)
20+
with redis_lock("check-cache", acquire_timeout=60.0, lock_timeout=300.0):
21+
register_checks()
22+
if not check_manager.check_ids:
23+
logger.error("No valid checks selected. Please check your configuration.")
24+
return
25+
for check in check_manager:
26+
yield check.run(clear_cache)

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)