Skip to content

Commit

Permalink
fix(robot-server): add internal flag to subsystem updates so we know …
Browse files Browse the repository at this point in the history
…if updates are started automatically or explictly.
  • Loading branch information
vegano1 committed Apr 24, 2024
1 parent 1b1ec1b commit 37463b1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion robot-server/robot_server/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ async def _do_updates(
) -> None:
update_handles = [
await update_manager.start_update_process(
str(uuid4()), SubSystem.from_hw(subsystem), utc_now()
str(uuid4()), SubSystem.from_hw(subsystem), utc_now(), internal=True,
)
for subsystem, subsystem_state in hardware.attached_subsystems.items()
if not subsystem_state.ok or subsystem_state.fw_update_needed
Expand Down
19 changes: 15 additions & 4 deletions robot-server/robot_server/subsystems/firmware_update_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ProcessDetails:
created_at: datetime
subsystem: SubSystem
update_id: str
internal: bool


@dataclass
Expand Down Expand Up @@ -131,6 +132,7 @@ class _UpdateProcess:
_created_at: datetime
_update_id: str
_complete_callback: Callable[[], Awaitable[None]]
_internal: bool

def __init__(
self,
Expand All @@ -139,6 +141,7 @@ def __init__(
created_at: datetime,
update_id: str,
complete_callback: Callable[[], Awaitable[None]],
internal: bool = True,
) -> None:
"""Build an _UpdateProcess. Should only be done by the manager."""
self._status_queue = Queue()
Expand All @@ -149,6 +152,7 @@ def __init__(
self._created_at = created_at
self._update_id = update_id
self._complete_callback = complete_callback
self._internal = internal

@property
def status_cache(self) -> UpdateProgress:
Expand Down Expand Up @@ -177,6 +181,11 @@ def update_id(self) -> str:
"""The ID of the update task."""
return self._update_id

@property
def internal(self) -> bool:
"""Whether this update was started internally or by the client."""
return self._internal

async def _update_task(self) -> None:
last_progress = 0
try:
Expand Down Expand Up @@ -252,6 +261,7 @@ def __init__(self, update_proc: _UpdateProcess) -> None:
update_proc.created_at,
SubSystem.from_hw(update_proc.subsystem),
update_proc.update_id,
update_proc.internal,
)

async def get_progress(self) -> UpdateProgress:
Expand Down Expand Up @@ -322,7 +332,7 @@ async def _get_by_id(self, update_id: str) -> _UpdateProcess:
raise UpdateIdNotFound() from e

async def _emplace(
self, update_id: str, subsystem: SubSystem, creation_time: datetime
self, update_id: str, subsystem: SubSystem, creation_time: datetime, internal: bool
) -> _UpdateProcess:
hw_subsystem = subsystem.to_hw()

Expand All @@ -346,8 +356,8 @@ async def _complete() -> None:
log.exception(f"Double pop for update on {subsystem}")

self._all_updates_by_id[update_id] = _UpdateProcess(
self._hardware_handle, hw_subsystem, creation_time, update_id, _complete
)
self._hardware_handle, hw_subsystem, creation_time, update_id, _complete,
internal)
self._running_updates_by_subsystem[hw_subsystem] = self._all_updates_by_id[
update_id
]
Expand Down Expand Up @@ -403,6 +413,7 @@ async def start_update_process(
update_id: str,
subsystem: SubSystem,
created_at: datetime,
internal: bool = False,
) -> UpdateProcessHandle:
"""Try to begin an update process, checking preconditions, and return a handle if successful.
Expand All @@ -412,6 +423,6 @@ async def start_update_process(
handle).
"""
async with self._management_lock:
process = await self._emplace(update_id, subsystem, created_at)
process = await self._emplace(update_id, subsystem, created_at, internal)
await process.provide_latest_progress()
return process.get_handle()
7 changes: 7 additions & 0 deletions robot-server/robot_server/subsystems/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ class UpdateProgressData(BaseModel):
...,
description="If the process failed, this will contain a string description of the reason.",
)
internal: bool = Field(
..., description="Whether this update was started internally or not."
)



class UpdateProgressSummary(BaseModel):
Expand All @@ -139,3 +143,6 @@ class UpdateProgressSummary(BaseModel):
updateStatus: UpdateState = Field(
..., description="Whether an update is queued, in progress or completed"
)
internal: bool = Field(
..., description="Whether this update was started internally or not."
)
6 changes: 6 additions & 0 deletions robot-server/robot_server/subsystems/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ async def get_subsystem_updates(
subsystem=handle.process_details.subsystem,
updateStatus=handle.cached_state,
createdAt=handle.process_details.created_at,
internal=handle.process_details.internal,
)
for handle in handles
]
Expand Down Expand Up @@ -242,6 +243,7 @@ async def get_subsystem_update(
updateStatus=progress.state,
updateProgress=progress.progress,
updateError=_error_str(progress.error),
internal=handle.process_details.internal,
)
)
)
Expand Down Expand Up @@ -271,6 +273,7 @@ async def get_update_processes(
subsystem=update.process_details.subsystem,
updateStatus=update.cached_state,
createdAt=update.process_details.created_at,
internal=update.process_details.internal,
)
for update in update_manager.all_update_processes()
]
Expand Down Expand Up @@ -306,6 +309,7 @@ async def get_update_process(
updateStatus=progress.state,
updateProgress=progress.progress,
updateError=_error_str(progress.error),
internal=handle.process_details.internal,
)
)
)
Expand Down Expand Up @@ -340,6 +344,7 @@ async def begin_subsystem_update(
update_process_id,
subsystem,
created_at,
internal=False,
)
except _SubsystemNotFound as e:
raise SubsystemNotPresent(
Expand Down Expand Up @@ -373,6 +378,7 @@ async def begin_subsystem_update(
updateStatus=progress.state,
updateProgress=progress.progress,
updateError=_error_str(progress.error),
internal=summary.process_details.internal,
)
),
status_code=status.HTTP_201_CREATED,
Expand Down

0 comments on commit 37463b1

Please sign in to comment.