9595from .auth import api as auth_api
9696from .control_tasks import (
9797 _CONTROL_TO_POWER_STATE ,
98- _CONTROL_TRANSITION_INITIAL ,
9998 dispatch_control ,
100- perform_control ,
99+ get_control_in_progress_status ,
101100)
102101from .resize_tasks import dispatch_resize
103102from .tasks import task_api
@@ -233,29 +232,34 @@ def _should_update_branch_status(
233232def _adjust_derived_status_for_stuck_creation (
234233 branch : Branch , current : BranchServiceStatus , derived : BranchServiceStatus
235234) -> BranchServiceStatus :
236- if derived != BranchServiceStatus .STOPPED :
235+ if derived not in {BranchServiceStatus .STOPPED , BranchServiceStatus .ERROR }:
236+ return derived
237+ if current not in {BranchServiceStatus .CREATING , BranchServiceStatus .STARTING }:
237238 return derived
238239
239240 status_timestamp = branch .status_updated_at or branch .created_datetime
240241 elapsed = datetime .now (UTC ) - status_timestamp
241242
242- if current == BranchServiceStatus .CREATING and elapsed >= _CREATING_STATUS_ERROR_GRACE_PERIOD :
243- logger .warning (
244- "Branch %s still CREATING after %s with STOPPED services; marking ERROR" ,
245- branch .id ,
246- elapsed ,
247- )
248- return BranchServiceStatus .ERROR
243+ if current == BranchServiceStatus .CREATING :
244+ if derived == BranchServiceStatus .STOPPED and elapsed >= _CREATING_STATUS_ERROR_GRACE_PERIOD :
245+ logger .warning (
246+ "Branch %s still CREATING after %s with STOPPED services; marking ERROR" ,
247+ branch .id ,
248+ elapsed ,
249+ )
250+ return BranchServiceStatus .ERROR
251+ return derived
249252
250- if current == BranchServiceStatus .STARTING and elapsed >= _STARTING_STATUS_ERROR_GRACE_PERIOD :
253+ # current == STARTING
254+ if elapsed >= _STARTING_STATUS_ERROR_GRACE_PERIOD :
251255 logger .warning (
252- "Branch %s still STARTING after %s with STOPPED services ; marking ERROR" ,
256+ "Branch %s still STARTING after %s; marking ERROR" ,
253257 branch .id ,
254258 elapsed ,
255259 )
256260 return BranchServiceStatus .ERROR
257-
258- return derived
261+ # Within grace period: suppress both STOPPED and ERROR — stay STARTING
262+ return current
259263
260264
261265async def refresh_branch_status (branch_id : Identifier ) -> BranchServiceStatus :
@@ -279,10 +283,9 @@ async def refresh_branch_status(branch_id: Identifier) -> BranchServiceStatus:
279283def _active_task_status (branch : Branch ) -> BranchServiceStatus | None :
280284 """Return the in-progress status for the running task, or None if idle."""
281285 if branch .control_task_id is not None :
282- result = perform_control .AsyncResult (str (branch .control_task_id ))
283- action = (result .kwargs or {}).get ("action" )
284- if action in _CONTROL_TRANSITION_INITIAL :
285- return _CONTROL_TRANSITION_INITIAL [action ]
286+ status = get_control_in_progress_status (branch .control_task_id )
287+ if status is not None :
288+ return status
286289 if branch .resize_task_id is not None :
287290 return BranchServiceStatus .RESIZING
288291 return None
0 commit comments