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

fix topic publishing #195

Merged
merged 1 commit into from
May 15, 2023
Merged
Changes from all 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
37 changes: 27 additions & 10 deletions src/blueapi/service/handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
from typing import Optional
from typing import Mapping, Optional

from blueapi.config import ApplicationConfig
from blueapi.core import BlueskyContext
from blueapi.core.event import EventStream
from blueapi.messaging import StompMessagingTemplate
from blueapi.messaging.base import MessagingTemplate
from blueapi.worker.reworker import RunEngineWorker
Expand All @@ -29,19 +30,35 @@ def __init__(self, config: Optional[ApplicationConfig] = None) -> None:
def start(self) -> None:
self.worker.start()

self.worker.data_events.subscribe(
lambda event, corr_id: self.message_bus.send(
"public.worker.event.data", event, None, corr_id
)
)
self.worker.progress_events.subscribe(
lambda event, corr_id: self.message_bus.send(
"public.worker.event.progress", event, None, corr_id
)
self._publish_event_streams(
{
self.worker.worker_events: self.message_bus.destinations.topic(
"public.worker.event"
),
self.worker.progress_events: self.message_bus.destinations.topic(
"public.worker.event.progress"
),
self.worker.data_events: self.message_bus.destinations.topic(
"public.worker.event.data"
),
}
)

self.message_bus.connect()

def _publish_event_streams(
self, streams_to_destinations: Mapping[EventStream, str]
) -> None:
for stream, destination in streams_to_destinations.items():
self._publish_event_stream(stream, destination)

def _publish_event_stream(self, stream: EventStream, destination: str) -> None:
stream.subscribe(
lambda event, correlation_id: self.message_bus.send(
destination, event, None, correlation_id
)
)

def stop(self) -> None:
self.worker.stop()
self.message_bus.disconnect()
Expand Down