-
Notifications
You must be signed in to change notification settings - Fork 23
Gracefully Handle HTCondor Collector Updates #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 11 commits
0b3e6c2
3ef13a4
a7b61e9
f276b73
a1106be
c24ed5d
e361756
e2bb4f5
f0d3906
3b27beb
cb8f443
4b8c62f
e120220
10b7169
0a1fae3
639217b
93660e2
cc91fd4
7b971ed
7bd1076
973f49a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,11 @@ Alexander Haas <[email protected]> | |
| Eileen Kuehn <[email protected]> | ||
| matthias.schnepf <[email protected]> | ||
| ubdsv <[email protected]> | ||
| Max Kühn <[email protected]> | ||
| Rene Caspart <[email protected]> | ||
| Leon Schuhmacher <[email protected]> | ||
| R. Florian von Cube <[email protected]> | ||
| Benjamin Rottler <[email protected]> | ||
| Max Kühn <[email protected]> | ||
| Sebastian Wozniewski <[email protected]> | ||
| mschnepf <[email protected]> | ||
| swozniewski <[email protected]> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| from collections.abc import Mapping | ||
| from datetime import datetime | ||
| from datetime import timedelta | ||
| from functools import partial | ||
| from types import MappingProxyType | ||
|
|
||
| import asyncio | ||
| import logging | ||
|
|
@@ -11,10 +13,16 @@ | |
|
|
||
|
|
||
| class AsyncCacheMap(Mapping): | ||
| def __init__(self, update_coroutine, max_age: int = 60 * 15): | ||
| def __init__( | ||
| self, | ||
| update_coroutine, | ||
| max_age: int = 60 * 15, | ||
| update_coroutine_receives_ro_cache: bool = False, | ||
giffels marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): | ||
| self._update_coroutine = update_coroutine | ||
| self._max_age = max_age | ||
| self._last_update = datetime.fromtimestamp(0) | ||
| self._update_coroutine_receives_cache = update_coroutine_receives_ro_cache | ||
| self._data = {} | ||
| self._lock = None | ||
|
|
||
|
|
@@ -26,17 +34,27 @@ def _async_lock(self): | |
| self._lock = asyncio.Lock() | ||
| return self._lock | ||
|
|
||
| @property | ||
| def read_only_cache(self): | ||
| return MappingProxyType(self._data) | ||
|
|
||
| @property | ||
| def last_update(self) -> datetime: | ||
| return self._last_update | ||
|
|
||
| @property | ||
| def update_coroutine(self): | ||
| if self._update_coroutine_receives_cache: | ||
| return partial(self._update_coroutine, self.read_only_cache) | ||
| return self._update_coroutine | ||
|
|
||
| async def update_status(self) -> None: | ||
| current_time = datetime.now() | ||
|
|
||
| async with self._async_lock: | ||
| if (current_time - self._last_update) > timedelta(seconds=self._max_age): | ||
| try: | ||
| data = await self._update_coroutine() | ||
| data = await self.update_coroutine() | ||
| except json.decoder.JSONDecodeError as je: | ||
| logger.warning( | ||
| f"AsyncMap update_status failed: Could not decode json {je}" | ||
|
|
@@ -66,4 +84,6 @@ def __eq__(self, other): | |
| and self._last_update == other._last_update | ||
| and self._data == other._data | ||
| and self._lock == other._lock | ||
| and self._update_coroutine_receives_cache | ||
| == other._update_coroutine_receives_cache | ||
|
||
| ) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really part of the feature, but fixing Type Hints is always good when visting the code anyhow. |
Uh oh!
There was an error while loading. Please reload this page.