Skip to content

Commit

Permalink
Add ID to environment
Browse files Browse the repository at this point in the history
  • Loading branch information
callumforrester committed Dec 18, 2024
1 parent 2f50993 commit c9adb11
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/blueapi/service/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Iterable
from typing import Any
from uuid import UUID

from bluesky.protocols import HasName
from pydantic import Field
Expand Down Expand Up @@ -144,6 +145,10 @@ class EnvironmentResponse(BlueapiBaseModel):
State of internal environment.
"""

environment_id: UUID = Field(
description="Unique ID for the environment instance, can be used to "
"differentiate between a new environment and old that has been torn down"
)
initialized: bool = Field(description="blueapi context initialized")
error_message: str | None = Field(
default=None,
Expand Down
12 changes: 11 additions & 1 deletion src/blueapi/service/runner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
import logging
import signal
import uuid
from collections.abc import Callable
from importlib import import_module
from multiprocessing import Pool, set_start_method
Expand Down Expand Up @@ -57,6 +58,7 @@ def default_subprocess_factory():
self._subprocess = None
self._subprocess_factory = subprocess_factory or default_subprocess_factory
self._state = EnvironmentResponse(
environment_id=uuid.uuid4(),
initialized=False,
)

Expand All @@ -69,30 +71,38 @@ def reload(self):

@start_as_current_span(TRACER)
def start(self):
new_environment_id = uuid.uuid4()
try:
self._subprocess = self._subprocess_factory()
self.run(setup, self._config)
self._state = EnvironmentResponse(initialized=True)
self._state = EnvironmentResponse(
environment_id=new_environment_id,
initialized=True,
)
except Exception as e:
self._state = EnvironmentResponse(
environment_id=new_environment_id,
initialized=False,
error_message=str(e),
)
LOGGER.exception(e)

@start_as_current_span(TRACER)
def stop(self):
existing_environment_id = self._state.environment_id
try:
self.run(teardown)
if self._subprocess is not None:
self._subprocess.close()
self._subprocess.join()
self._state = EnvironmentResponse(
environment_id=existing_environment_id,
initialized=False,
error_message=self._state.error_message,
)
except Exception as e:
self._state = EnvironmentResponse(
environment_id=existing_environment_id,
initialized=False,
error_message=str(e),
)
Expand Down

0 comments on commit c9adb11

Please sign in to comment.