Skip to content

Commit

Permalink
refactor(robot-server): Single-source filenames (#15985)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxColoring authored Aug 13, 2024
1 parent 94f76ff commit f73aa80
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 44 deletions.
6 changes: 2 additions & 4 deletions robot-server/robot_server/data_files/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""FastAPI dependencies for data files endpoints."""
from pathlib import Path
from asyncio import Lock as AsyncLock
from typing import Final
from anyio import Path as AsyncPath

from fastapi import Depends
Expand All @@ -17,13 +16,12 @@
get_active_persistence_directory,
get_sql_engine,
)
from robot_server.persistence.file_and_directory_names import DATA_FILES_DIRECTORY
from robot_server.deletion_planner import DataFileDeletionPlanner
from .data_files_store import DataFilesStore
from .file_auto_deleter import DataFileAutoDeleter


_DATA_FILES_SUBDIRECTORY: Final = "data_files"

_data_files_directory_init_lock = AsyncLock()
_data_files_directory_accessor = AppStateAccessor[Path]("data_files_directory")

Expand All @@ -39,7 +37,7 @@ async def get_data_files_directory(
async with _data_files_directory_init_lock:
data_files_dir = _data_files_directory_accessor.get_from(app_state)
if data_files_dir is None:
data_files_dir = persistent_directory / _DATA_FILES_SUBDIRECTORY
data_files_dir = persistent_directory / DATA_FILES_DIRECTORY
await AsyncPath(data_files_dir).mkdir(exist_ok=True)
_data_files_directory_accessor.set_on(app_state, data_files_dir)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
get_active_persistence_directory,
get_active_persistence_directory_failsafe,
)
from robot_server.persistence.file_and_directory_names import DECK_CONFIGURATION_FILE
from robot_server.service.notifications import (
DeckConfigurationPublisher,
get_deck_configuration_publisher,
)


# This needs to be kept in sync with opentrons.execute, which reads this file.
_DECK_CONFIGURATION_FILE_NAME = "deck_configuration.json"


_accessor = AppStateAccessor[DeckConfigurationStore]("deck_configuration_store")


Expand All @@ -43,7 +40,7 @@ async def get_deck_configuration_store(
"""Return the server's singleton `DeckConfigurationStore`."""
deck_configuration_store = _accessor.get_from(app_state)
if deck_configuration_store is None:
path = persistence_directory / _DECK_CONFIGURATION_FILE_NAME
path = persistence_directory / DECK_CONFIGURATION_FILE
# If this initialization becomes async, we will need to protect it with a lock,
# to protect against the bug described in https://github.com/Opentrons/opentrons/pull/11927.
deck_configuration_store = DeckConfigurationStore(
Expand Down Expand Up @@ -77,7 +74,7 @@ async def get_deck_configuration_store_failsafe(
return None
deck_configuration_store = _accessor.get_from(app_state)
if deck_configuration_store is None:
path = persistence_directory / _DECK_CONFIGURATION_FILE_NAME
path = persistence_directory / DECK_CONFIGURATION_FILE
# If this initialization becomes async, we will need to protect it with a lock,
# to protect against the bug described in https://github.com/Opentrons/opentrons/pull/11927.
deck_configuration_store = DeckConfigurationStore(
Expand Down
4 changes: 0 additions & 4 deletions robot-server/robot_server/persistence/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
"""Support for persisting data across device reboots."""

from ._files_and_directories import LATEST_VERSION_DIRECTORY

__all__ = ["LATEST_VERSION_DIRECTORY"]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from ..tables import schema_2, schema_3
from .._folder_migrator import Migration
from .._files_and_directories import (
from ..file_and_directory_names import (
DECK_CONFIGURATION_FILE,
PROTOCOLS_DIRECTORY,
DB_FILE,
Expand Down
5 changes: 2 additions & 3 deletions robot-server/robot_server/persistence/_migrations/v3_to_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import sqlalchemy

from ..database import sql_engine_ctx
from ..file_and_directory_names import DB_FILE
from ..tables import schema_4
from .._folder_migrator import Migration

_DB_FILE = "robot_server.db"


class Migration3to4(Migration): # noqa: D101
def migrate(self, source_dir: Path, dest_dir: Path) -> None:
Expand All @@ -29,7 +28,7 @@ def migrate(self, source_dir: Path, dest_dir: Path) -> None:
shutil.copytree(src=item, dst=dest_dir / item.name)
else:
shutil.copy(src=item, dst=dest_dir / item.name)
dest_db_file = dest_dir / _DB_FILE
dest_db_file = dest_dir / DB_FILE

# Append the new column to existing analyses in v4 database
with ExitStack() as exit_stack:
Expand Down
5 changes: 2 additions & 3 deletions robot-server/robot_server/persistence/_migrations/v4_to_v5.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import sqlalchemy

from ..database import sql_engine_ctx
from ..file_and_directory_names import DB_FILE
from ..tables import schema_5
from .._folder_migrator import Migration

_DB_FILE = "robot_server.db"


class Migration4to5(Migration): # noqa: D101
def migrate(self, source_dir: Path, dest_dir: Path) -> None:
Expand All @@ -29,7 +28,7 @@ def migrate(self, source_dir: Path, dest_dir: Path) -> None:
shutil.copytree(src=item, dst=dest_dir / item.name)
else:
shutil.copy(src=item, dst=dest_dir / item.name)
dest_db_file = dest_dir / _DB_FILE
dest_db_file = dest_dir / DB_FILE

# Append the new column to existing protocols in v4 database
with ExitStack() as exit_stack:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .._folder_migrator import Migration

from ._util import copy_rows_unmodified, copy_if_exists, copytree_if_exists
from .._files_and_directories import (
from ..file_and_directory_names import (
DECK_CONFIGURATION_FILE,
PROTOCOLS_DIRECTORY,
DATA_FILES_DIRECTORY,
Expand Down
7 changes: 2 additions & 5 deletions robot-server/robot_server/persistence/fastapi_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sqlalchemy.engine import Engine as SQLEngine
from anyio import to_thread
from fastapi import Depends, status
from typing_extensions import Final

from server_utils.fastapi_utils.app_state import (
AppState,
Expand All @@ -20,16 +19,14 @@
from robot_server.errors.error_responses import ErrorDetails

from .database import create_sql_engine
from .file_and_directory_names import DB_FILE
from .persistence_directory import (
PersistenceResetter,
prepare_active_subdirectory,
prepare_root,
)


_DATABASE_FILE: Final = "robot_server.db"


_log = logging.getLogger(__name__)


Expand Down Expand Up @@ -105,7 +102,7 @@ async def init_sql_engine() -> SQLEngine:
prepared_subdirectory = await subdirectory_prep_task

sql_engine = await to_thread.run_sync(
create_sql_engine, prepared_subdirectory / _DATABASE_FILE
create_sql_engine, prepared_subdirectory / DB_FILE
)
return sql_engine

Expand Down
16 changes: 16 additions & 0 deletions robot-server/robot_server/persistence/file_and_directory_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""The names of files and directories in persistent storage.
These are just the trailing name parts, not the full paths. A server launch-time setting
specifies the root persistence directory. And our migration system maintains copies of
these files in isolated versioned subdirectories, e.g. v1/deck_configuration.json,
v2/deck_configuration.json, etc.
"""

from typing import Final

LATEST_VERSION_DIRECTORY: Final = "6"

DECK_CONFIGURATION_FILE: Final = "deck_configuration.json"
PROTOCOLS_DIRECTORY: Final = "protocols"
DATA_FILES_DIRECTORY: Final = "data_files"
DB_FILE: Final = "robot_server.db"
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from ._folder_migrator import MigrationOrchestrator
from ._migrations import up_to_3, v3_to_v4, v4_to_v5, v5_to_v6
from . import LATEST_VERSION_DIRECTORY
from .file_and_directory_names import LATEST_VERSION_DIRECTORY

_TEMP_PERSISTENCE_DIR_PREFIX: Final = "opentrons-robot-server-"
_RESET_MARKER_FILE_NAME: Final = "_TO_BE_DELETED_ON_REBOOT"
Expand Down
9 changes: 2 additions & 7 deletions robot-server/robot_server/protocols/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from asyncio import Lock as AsyncLock
from pathlib import Path
from typing_extensions import Final
import logging

from anyio import Path as AsyncPath
from fastapi import Depends
Expand All @@ -24,6 +22,7 @@
get_sql_engine,
get_active_persistence_directory,
)
from robot_server.persistence.file_and_directory_names import PROTOCOLS_DIRECTORY
from robot_server.settings import get_settings
from .analyses_manager import AnalysesManager

Expand All @@ -34,10 +33,6 @@
from .analysis_store import AnalysisStore


_PROTOCOL_FILES_SUBDIRECTORY: Final = "protocols"

_log = logging.getLogger(__name__)

_protocol_store_init_lock = AsyncLock()
_protocol_store_accessor = AppStateAccessor[ProtocolStore]("protocol_store")

Expand Down Expand Up @@ -71,7 +66,7 @@ async def get_protocol_directory(
async with _protocol_directory_init_lock:
protocol_directory = _protocol_directory_accessor.get_from(app_state)
if protocol_directory is None:
protocol_directory = persistence_directory / _PROTOCOL_FILES_SUBDIRECTORY
protocol_directory = persistence_directory / PROTOCOLS_DIRECTORY
await AsyncPath(protocol_directory).mkdir(exist_ok=True)
_protocol_directory_accessor.set_on(app_state, protocol_directory)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from robot_server.persistence import LATEST_VERSION_DIRECTORY
from robot_server.persistence.file_and_directory_names import LATEST_VERSION_DIRECTORY

from tests.integration.dev_server import DevServer
from tests.integration.robot_client import RobotClient
Expand Down

0 comments on commit f73aa80

Please sign in to comment.