-
Notifications
You must be signed in to change notification settings - Fork 2
Add managed redis config value tool and use it for scan interlock settings #959
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9917d29
feat: add managed redis config value tool
d-perl f034e2a
feat: use config value for scan restart
d-perl dacfcbc
feat: use enum for trigger setting
d-perl 78ad883
test: add tests for managed config value
d-perl 4ba3515
fix: tidy up
d-perl 9b2f772
feat: add option to wait for set in redis to complete, use by default
d-perl f932afe
fix: improve naming, remove dead code
d-perl fda0c80
fix(actor_hli): cleanup of the redis connections on shutdown
wyzula-jan 6c36ebb
fix: remove minimum time from scan interlock
d-perl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| from threading import Event | ||
| from typing import Any, Callable, Generic, TypeVar | ||
| from weakref import ReferenceType | ||
|
|
||
| from louie.saferef import BoundMethodWeakref, safe_ref | ||
|
|
||
| from bec_lib.endpoints import EndpointInfo, MessageOp | ||
| from bec_lib.logger import bec_logger | ||
| from bec_lib.messages import ManagedConfigMessage | ||
| from bec_lib.redis_connector import RedisConnector | ||
|
|
||
| logger = bec_logger.logger | ||
| ValueT = TypeVar("ValueT") | ||
|
|
||
|
|
||
| class RedisConfigValue(property, Generic[ValueT]): | ||
| def __init__( | ||
| self, | ||
| connector: RedisConnector, | ||
| endpoint: EndpointInfo[type[ManagedConfigMessage[ValueT]]], | ||
| wait_for_writes: bool = True, | ||
| ) -> None: | ||
| """A config value bound to a value in Redis, which uses the ManagedConfigMessage and an associated endpoint, | ||
| and which can be subscribed to.""" | ||
|
|
||
| if endpoint.message_op != MessageOp.STREAM or not issubclass( | ||
| endpoint.message_type, ManagedConfigMessage | ||
| ): | ||
| raise TypeError( | ||
| "RedisConfigManager needs a STREAM endpoint with a message type which is a subclass of ManagedConfigMessage" | ||
| ) | ||
|
|
||
| self._ep = endpoint | ||
| self._connector = connector | ||
|
|
||
| self._writing_wait_event = Event() | ||
| self._writing_wait_event.set() | ||
| self._wait_for_writes = wait_for_writes | ||
|
|
||
| self._cbs: set[ReferenceType[Callable[[ValueT]]] | BoundMethodWeakref] = set() | ||
|
|
||
| self._config = self._fetch() | ||
| self._connector.register(self._ep, cb=self._update_cb) | ||
|
|
||
| def __del__(self): | ||
| if hasattr(self, "_connector"): | ||
| self.unregister_all() | ||
|
|
||
| def __bool__(self): | ||
| raise ValueError(f"Maybe you meant to check {self}.value?") | ||
|
|
||
| def _fetch(self) -> ManagedConfigMessage[ValueT]: | ||
| existing = self._connector.xread(self._ep, from_start=True) | ||
| if existing is None or existing == []: | ||
| logger.warning( | ||
| f"No value found in redis for managed config var {self._ep.endpoint}, resetting to default." | ||
| ) | ||
| config = self._ep.message_type() # type: ignore # concrete classes must have a default | ||
| self._write(config, False) | ||
| return config | ||
| return existing[-1]["config"] | ||
|
|
||
| def _write(self, updated: ManagedConfigMessage[ValueT], wait): | ||
| if wait: | ||
| self._writing_wait_event.clear() | ||
| self._connector.xadd(self._ep, {"config": updated}, max_size=1) | ||
| self._writing_wait_event.wait(timeout=2) | ||
| if not self._writing_wait_event.is_set(): | ||
| logger.error( | ||
| f"Timed out waiting for config variable {self._ep.endpoint} to return from Redis" | ||
| ) | ||
|
|
||
| def _update_cb(self, msg_dict: dict): | ||
| try: | ||
| self._config = msg_dict["config"] | ||
| for cb_ref in list(self._cbs): | ||
| if cb := cb_ref(): | ||
| try: | ||
| cb(self._config.value) | ||
| except Exception as e: | ||
| logger.error(f"Exception in managed config value callback {cb}: {e}") | ||
| else: | ||
| self._cbs.discard(cb_ref) | ||
| finally: | ||
| self._writing_wait_event.set() | ||
|
|
||
| @property | ||
| def value(self) -> ValueT: | ||
| return self._config.value | ||
|
|
||
| @value.setter | ||
| def value(self, value: ValueT): | ||
| self._write(self._ep.message_type(value=value), self._wait_for_writes) | ||
|
|
||
| def subscribe(self, cb: Callable[[ValueT], Any]): | ||
| self._cbs.add(safe_ref(cb)) | ||
|
|
||
| def unsubscribe(self, cb: Callable[[ValueT], Any]): | ||
| self._cbs.discard(safe_ref(cb)) | ||
|
|
||
| def unregister_all(self): | ||
| self._connector.unregister(self._ep, cb=self._update_cb) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.