Skip to content
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

Add document listener #329

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions helm/blueapi/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ spec:
env:
- name: SCRATCH_AREA
value: {{ .Values.scratch.containerPath }}
{{- if .Values.listener.enabled -}}
- name: {{ .Chart.Name }}
rosesyrett marked this conversation as resolved.
Show resolved Hide resolved
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
rosesyrett marked this conversation as resolved.
Show resolved Hide resolved
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resources should be much smaller than the resources for the blueapi main container

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just remove the resource requirements entirely and let k8s work it out. Do you think that's reasonable?

The alternative is including a listener.resources section in the values.yaml which I don't know if we should do since it's such a small process

args:
- "-c"
- "/config/config.yaml"
{{- with .Values.existingSecret }}
- "-c"
- "/config/secret.yaml"
{{- end }}
- "listen"
{{- end }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand Down
3 changes: 3 additions & 0 deletions helm/blueapi/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ affinity: {}

hostNetwork: false # May be needed for talking to arcane protocols such as EPICS

listener:
enabled: true

scratch:
hostPath: "" # example: /usr/local/blueapi-software-scratch
containerPath: /blueapi-plugins/scratch
Expand Down
14 changes: 12 additions & 2 deletions src/blueapi/cli/amq.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
super().__init__(message)


_Event = Union[WorkerEvent, ProgressEvent, DataEvent]


class AmqClient:
app: MessagingTemplate
complete: threading.Event
Expand Down Expand Up @@ -42,7 +45,8 @@
callback = BestEffortCallback()

def on_event_wrapper(
ctx: MessageContext, event: Union[WorkerEvent, ProgressEvent, DataEvent]
ctx: MessageContext,
event: _Event,
) -> None:
if isinstance(event, WorkerEvent):
if (on_event is not None) and (ctx.correlation_id == correlation_id):
Expand All @@ -55,9 +59,15 @@
elif isinstance(event, DataEvent):
callback(event.name, event.doc)

self.subscribe_to_all_events(on_event_wrapper)

Check warning on line 62 in src/blueapi/cli/amq.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/amq.py#L62

Added line #L62 was not covered by tests

def subscribe_to_all_events(
self,
on_event: Callable[[MessageContext, _Event], None],
) -> None:
self.app.subscribe(
self.app.destinations.topic("public.worker.event"),
on_event_wrapper,
on_event,
)

def wait_for_complete(self, timeout: Optional[float] = None) -> None:
Expand Down
28 changes: 27 additions & 1 deletion src/blueapi/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from blueapi import __version__
from blueapi.cli.amq import AmqClient
from blueapi.config import ApplicationConfig, ConfigLoader
from blueapi.core import DataEvent
from blueapi.messaging import MessageContext
from blueapi.messaging.stomptemplate import StompMessagingTemplate
from blueapi.service.main import start
from blueapi.service.model import WorkerTask
Expand All @@ -21,7 +23,7 @@
print_schema_as_yaml,
write_schema_as_yaml,
)
from blueapi.worker import RunPlan, WorkerEvent, WorkerState
from blueapi.worker import ProgressEvent, RunPlan, WorkerEvent, WorkerState

from .rest import BlueapiRestClient

Expand Down Expand Up @@ -127,6 +129,30 @@
pprint(client.get_devices().dict())


@controller.command(name="listen")
@check_connection
@click.pass_obj
def listen_to_events(obj: dict) -> None:
"""Listen to events output by blueapi"""
config: ApplicationConfig = obj["config"]
amq_client = AmqClient(StompMessagingTemplate.autoconfigured(config.stomp))

Check warning on line 138 in src/blueapi/cli/cli.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/cli.py#L137-L138

Added lines #L137 - L138 were not covered by tests

def on_event(

Check warning on line 140 in src/blueapi/cli/cli.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/cli.py#L140

Added line #L140 was not covered by tests
context: MessageContext,
event: Union[WorkerEvent, ProgressEvent, DataEvent],
) -> None:
converted = json.dumps(event.dict(), indent=2)
print(converted)

Check warning on line 145 in src/blueapi/cli/cli.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/cli.py#L144-L145

Added lines #L144 - L145 were not covered by tests

print(

Check warning on line 147 in src/blueapi/cli/cli.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/cli.py#L147

Added line #L147 was not covered by tests
"Subscribing to all bluesky events from "
f"{config.stomp.host}:{config.stomp.port}"
)
with amq_client:
amq_client.subscribe_to_all_events(on_event)
input("Press enter to exit")

Check warning on line 153 in src/blueapi/cli/cli.py

View check run for this annotation

Codecov / codecov/patch

src/blueapi/cli/cli.py#L151-L153

Added lines #L151 - L153 were not covered by tests


@controller.command(name="run")
@click.argument("name", type=str)
@click.argument("parameters", type=str, required=False)
Expand Down