Skip to content

Commit

Permalink
fix some, indicate uncertain points
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-dot committed Oct 11, 2024
1 parent b3ec5bd commit d28365e
Show file tree
Hide file tree
Showing 19 changed files with 50 additions and 152 deletions.
2 changes: 1 addition & 1 deletion docs/explanations/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ of being written, loaded and run. Take the following plan.
import bluesky.plans as bp
from blueapi.core import MsgGenerator
from dls_bluesky_core.core import inject
from dodal.common import inject
from bluesky.protocols import Readable
Expand Down
107 changes: 0 additions & 107 deletions errors.txt

This file was deleted.

1 change: 0 additions & 1 deletion src/blueapi/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from blueapi.client.rest import BlueskyRemoteControlError
from blueapi.config import (
ApplicationConfig,
BasicAuthentication,
ConfigLoader,
StompConfig,
)
Expand Down
2 changes: 1 addition & 1 deletion src/blueapi/core/bluesky_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Triggerable,
WritesExternalAssets,
)
from dls_bluesky_core.core import MsgGenerator, PlanGenerator
from dodal.common import MsgGenerator, PlanGenerator
from ophyd_async.core import Device as AsyncDevice
from pydantic import BaseModel, Field

Expand Down
4 changes: 2 additions & 2 deletions src/blueapi/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def my_plan(a: int, b: str):
model = create_model(
plan.__name__,
__config__=BlueapiPlanModelConfig,
**self._type_spec_for_function(plan),
**self._type_spec_for_function(plan), # type: ignore
)
self.plans[plan.__name__] = Plan(
name=plan.__name__, model=model, description=plan.__doc__
Expand Down Expand Up @@ -284,7 +284,7 @@ def _convert_type(self, typ: type | Any) -> type:
root = get_origin(typ)
if root == UnionType:
root = Union
return root[new_types] if root else typ
return root[new_types] # type: ignore
return typ


Expand Down
7 changes: 2 additions & 5 deletions src/blueapi/core/device_lookup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from typing import Any, TypeVar
from typing import Any

from .bluesky_types import Device, is_bluesky_compatible_device

#: Device obeying Bluesky protocols
D = TypeVar("D", bound=Device)


def find_component(obj: Any, addr: list[str]) -> D | None:
def find_component(obj: Any, addr: list[str]) -> Device | None:
"""
Best effort function to locate a child device, either in a dictionary of
devices or a device with child attributes.
Expand Down
24 changes: 13 additions & 11 deletions src/blueapi/service/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from bluesky_stomp.messaging import StompClient
from bluesky_stomp.models import Broker, DestinationBase, MessageTopic

from blueapi.config import ApplicationConfig
from blueapi.config import ApplicationConfig, StompConfig
from blueapi.core.context import BlueskyContext
from blueapi.core.event import EventStream
from blueapi.service.model import DeviceModel, PlanModel, WorkerTask
Expand Down Expand Up @@ -49,12 +49,14 @@ def worker() -> TaskWorker:


@lru_cache
def messaging_template() -> StompClient | None:
stomp_config = config().stomp
def cached_stomp_client() -> StompClient | None:
stomp_config: StompConfig | None = config().stomp
if stomp_config is not None:
template = StompClient.for_broker(
client = StompClient.for_broker(
broker=Broker(
host=stomp_config.host, port=stomp_config.port, auth=stomp_config.auth
host=stomp_config.host,
port=stomp_config.port,
auth=stomp_config.auth, # type: ignore
)
)

Expand All @@ -68,8 +70,8 @@ def messaging_template() -> StompClient | None:
task_worker.data_events: event_topic,
}
)
template.connect()
return template
client.connect()
return client
else:
return None

Expand All @@ -83,16 +85,16 @@ def setup(config: ApplicationConfig) -> None:

logging.basicConfig(level=config.logging.level)
worker()
messaging_template()
cached_stomp_client()


def teardown() -> None:
worker().stop()
if (template := messaging_template()) is not None:
if (template := cached_stomp_client()) is not None:
template.disconnect()
context.cache_clear()
worker.cache_clear()
messaging_template.cache_clear()
cached_stomp_client.cache_clear()


def _publish_event_streams(
Expand All @@ -104,7 +106,7 @@ def _publish_event_streams(

def _publish_event_stream(stream: EventStream, destination: DestinationBase) -> None:
def forward_message(event: Any, correlation_id: str | None) -> None:
if (template := messaging_template()) is not None:
if (template := cached_stomp_client()) is not None:
template.send(destination, event, None, correlation_id=correlation_id)

stream.subscribe(forward_message)
Expand Down
5 changes: 3 additions & 2 deletions src/blueapi/service/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ def submit_task(
task: Task = Body(..., example=example_task),
runner: WorkerDispatcher = Depends(_runner),
):
plan_model: PlanModel | None = None
"""Submit a task to the worker."""
plan_model = runner.run(interface.get_plan, task.name)
try:
plan_model = runner.run(interface.get_plan, task.name)
task_id: str = runner.run(interface.submit_task, task)
response.headers["Location"] = f"{request.url}/{task_id}"
return TaskResponse(task_id=task_id)
Expand All @@ -161,7 +162,7 @@ def submit_task(
)
error_detail_response = f"""
Input validation failed: {formatted_errors},
suppplied params {task.params},
supplied params {task.params},
do not match the expected params: {plan_model.parameter_schema}
"""
raise HTTPException(
Expand Down
4 changes: 2 additions & 2 deletions src/blueapi/service/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def _rpc(
**kwargs: Any,
) -> T:
mod = import_module(module_name)
func: Callable[P, T] = _validate_function(
func: Callable[..., T] = _validate_function(
mod.__dict__.get(function_name, None), function_name
)
) # type: ignore
value = func(*args, **kwargs)
return _valid_return(value, expected_type)

Expand Down
15 changes: 8 additions & 7 deletions src/blueapi/startup/example_plans.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import bluesky.plan_stubs as bps
from bluesky.plans import count
from bluesky.protocols import Movable, Readable
from dls_bluesky_core.core import inject
from dls_bluesky_core.plans import count
from dls_bluesky_core.stubs import move
from dodal.common import MsgGenerator, inject

from blueapi.core import MsgGenerator
TEMP: Movable = inject("sample_temperature")
PRESS: Movable = inject("sample_pressure")


def stp_snapshot(
detectors: list[Readable],
temperature: Movable = inject("sample_temperature"),
pressure: Movable = inject("sample_pressure"),
temperature: Movable = TEMP,
pressure: Movable = PRESS,
) -> MsgGenerator:
"""
Moves devices for pressure and temperature (defaults fetched from the context)
Expand All @@ -26,5 +27,5 @@ def stp_snapshot(
Yields:
Iterator[MsgGenerator]: Bluesky messages
"""
yield from move({temperature: 0, pressure: 10**5})
yield from bps.mv({temperature: 0, pressure: 10**5})
yield from count(detectors, 1)
1 change: 0 additions & 1 deletion src/blueapi/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
__all__ = [
"handle_all_exceptions",
"load_module_all",
"ConfigLoader",
"serialize",
"BlueapiBaseModel",
"BlueapiModelConfig",
Expand Down
1 change: 0 additions & 1 deletion src/blueapi/worker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
__all__ = [
"TaskWorker",
"Task",
"Worker",
"WorkerEvent",
"WorkerState",
"StatusView",
Expand Down
Loading

0 comments on commit d28365e

Please sign in to comment.