-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh: containerized service (needs auth)
- Loading branch information
1 parent
8babff3
commit e97a202
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,22 @@ | ||
# docker build -t kmeans_service:latest . | ||
FROM fedora | ||
# Lock in a python version because of some backward compatability issues | ||
RUN dnf update -y \ | ||
&& dnf install -y \ | ||
python3.9 \ | ||
g++ \ | ||
gcc \ | ||
git \ | ||
&& dnf clean all | ||
|
||
RUN python3.9 -m ensurepip --upgrade | ||
RUN pip3 install 'bluesky-adaptive[all]' | ||
RUN pip3 install uvicorn fastapi caproto nslsii | ||
RUN pip3 install git+https://github.com/NSLS-II-PDF/pdf-agents.git@main | ||
RUN pip3 install git+https://github.com/bluesky/[email protected]#egg=databroker | ||
|
||
COPY kmeans_service.py /src/kmeans_service.py | ||
|
||
ENV BS_AGENT_STARTUP_SCRIPT_PATH=/src/kmeans_service.py | ||
|
||
CMD uvicorn bluesky_adaptive.server:app --host 127.0.0.1 --port 60610 |
This file contains 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,2 @@ | ||
docker build -t kmeans_service:latest . | ||
docker-compose up |
This file contains 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,8 @@ | ||
version: '3' | ||
services: | ||
your-service: | ||
image: kmeans_service:latest | ||
ports: | ||
- 60281:60610 | ||
volumes: | ||
- /etc/bluesky:/etc/bluesky |
This file contains 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,80 @@ | ||
from bluesky_adaptive.server import register_variable, shutdown_decorator, startup_decorator | ||
|
||
from pdf_agents.sklearn import PassiveKmeansAgent | ||
|
||
|
||
class Agent(PassiveKmeansAgent): | ||
"""Copy of Kmeans agent for PDF Beamline that exposes and on/off switch to stop listening, and | ||
a full reset method. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._running = False | ||
|
||
@property | ||
def name(self): | ||
return "KmeansAgent" | ||
|
||
@property | ||
def running(self): | ||
return self._running | ||
|
||
def activate(self): | ||
self._running = True | ||
|
||
def pause(self): | ||
self._running = False | ||
|
||
def exp_start_clean_run(self): | ||
"""Convenience method to start a fresh agent that is accumulating documents live.""" | ||
self.pause() | ||
self.close_and_restart() | ||
self.activate() | ||
|
||
def exp_report_from_historical_data(self, uids): | ||
"""Convenience method to spawn a fresh agent that writes one report.""" | ||
self.pause() | ||
self.disable_continuous_reporting() | ||
self.close_and_restart() | ||
self.tell_agent_by_uid(uids) | ||
self.report() | ||
|
||
def exp_start_run_with_data(self, uids): | ||
"""Convenience method to spawn a fresh agent with data that accumualtes documents live.""" | ||
self.pause() | ||
self.disable_continuous_reporting() | ||
self.close_and_restart() | ||
self.tell_agent_by_uid(uids) | ||
self.enable_continuous_reporting() | ||
self.activate() | ||
|
||
def trigger_condition(self, uid) -> bool: | ||
return self.running | ||
|
||
def server_registrations(self) -> None: | ||
self._register_method("close_and_restart") | ||
self._register_property("running") | ||
self._register_method("activate") | ||
self._register_method("pause") | ||
self._register_method("exp_start_clean_run") | ||
self._register_method("exp_report_from_historical_data") | ||
self._register_method("exp_start_run_with_data") | ||
return super().server_registrations() | ||
|
||
|
||
agent = Agent(k_clusters=3, report_on_tell=True, ask_on_tell=False, direct_to_queue=False) | ||
|
||
|
||
@startup_decorator | ||
def startup(): | ||
agent.start() | ||
|
||
|
||
@shutdown_decorator | ||
def shutdown_agent(): | ||
return agent.stop() | ||
|
||
|
||
register_variable("UID Cache", agent, "tell_cache") | ||
register_variable("Agent Name", agent, "instance_name") |