Skip to content

Commit

Permalink
enh: containerized service (needs auth)
Browse files Browse the repository at this point in the history
  • Loading branch information
maffettone committed May 16, 2023
1 parent 8babff3 commit e97a202
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docker/clustering_service/Dockerfile
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
2 changes: 2 additions & 0 deletions docker/clustering_service/build_and_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docker build -t kmeans_service:latest .
docker-compose up
8 changes: 8 additions & 0 deletions docker/clustering_service/docker-compose.yml
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
80 changes: 80 additions & 0 deletions docker/clustering_service/kmeans_service.py
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")

0 comments on commit e97a202

Please sign in to comment.