@@ -999,23 +999,25 @@ def get_statuses(
999999 except (ApiException , HTTPError ) as e :
10001000 raise PolyaxonClientException ("Api error: %s" % e ) from e
10011001
1002- def _wait_for_condition (self , statuses : Optional [List [str ]] = None ):
1003- statuses = to_list (statuses , check_none = True )
1002+ def _should_stop_waiting (self , last_status , statuses ) -> bool :
1003+ if statuses :
1004+ return last_status in statuses
1005+ return LifeCycle .is_done (last_status )
10041006
1005- def condition ():
1006- if statuses :
1007- return last_status in statuses
1008- return LifeCycle .is_done (last_status )
1007+ def _is_retryable_status_error (self , error : ApiException ) -> bool :
1008+ return error .status in {500 , 502 , 503 , 504 }
10091009
1010+ def _wait_for_condition (self , statuses : Optional [List [str ]] = None ):
1011+ statuses = to_list (statuses , check_none = True )
10101012 last_status = None
1011- while not condition ( ):
1013+ while not self . _should_stop_waiting ( last_status , statuses ):
10121014 if last_status :
10131015 time .sleep (settings .CLIENT_CONFIG .watch_interval )
10141016 try :
10151017 last_status , _conditions = self .get_statuses (last_status )
10161018 yield last_status , _conditions
10171019 except ApiException as e :
1018- if e . status in { 500 , 502 , 503 , 504 } :
1020+ if self . _is_retryable_status_error ( e ) :
10191021 yield last_status , []
10201022 else :
10211023 raise e
@@ -3435,25 +3437,82 @@ async def get_statuses(
34353437 except ApiException as e :
34363438 raise PolyaxonClientException ("Api error: %s" % e ) from e
34373439
3438- @async_client_handler (check_no_op = True , check_offline = True )
3439- async def wait_for_condition (self , * args , ** kwargs ):
3440- self ._raise_sync_only ("wait_for_condition" )
3440+ async def _wait_for_condition (self , statuses : Optional [List [str ]] = None ):
3441+ statuses = to_list (statuses , check_none = True )
3442+ last_status = None
3443+ while not self ._should_stop_waiting (last_status , statuses ):
3444+ if last_status :
3445+ await asyncio .sleep (settings .CLIENT_CONFIG .watch_interval )
3446+ try :
3447+ last_status , conditions = await self .get_statuses (last_status )
3448+ yield last_status , conditions
3449+ except ApiException as e :
3450+ if self ._is_retryable_status_error (e ):
3451+ yield last_status , []
3452+ else :
3453+ raise e
34413454
34423455 @async_client_handler (check_no_op = True , check_offline = True )
3443- async def watch_statuses (self , * args , ** kwargs ):
3444- self ._raise_sync_only ("watch_statuses" )
3456+ async def wait_for_condition (
3457+ self ,
3458+ statuses : Optional [List [str ]] = None ,
3459+ print_status : bool = False ,
3460+ live_update : Any = None ,
3461+ ):
3462+ async for status , _conditions in self ._wait_for_condition (statuses ):
3463+ self ._run_data .status = status # type: ignore
3464+ if print_status :
3465+ print ("Last received status: {}\n " .format (status ))
3466+ if live_update :
3467+ latest_status = Printer .add_status_color (
3468+ {"status" : status }, status_key = "status"
3469+ )
3470+ live_update .update (status = "{}\n " .format (latest_status ["status" ]))
3471+
3472+ async def watch_statuses (self , statuses : Optional [List [str ]] = None ):
3473+ if self ._no_op or self ._is_offline :
3474+ return
3475+ async for status , conditions in self ._wait_for_condition (statuses ):
3476+ self ._run_data .status = status # type: ignore
3477+ yield status , conditions
34453478
34463479 @async_client_handler (check_no_op = True , check_offline = True )
3447- async def get_logs (self , * args , ** kwargs ):
3448- self ._raise_sync_only ("get_logs" )
3480+ async def get_logs (self , last_file = None , last_time = None ) -> "V1Logs" :
3481+ if not self .settings :
3482+ await self .refresh_data ()
3483+ await self ._use_agent_host ()
3484+ params = get_logs_params (
3485+ last_file = last_file ,
3486+ last_time = last_time ,
3487+ connection = self .artifacts_store ,
3488+ kind = self .run_data .kind ,
3489+ )
3490+ return await self .client .runs_v1 .get_run_logs (
3491+ self .namespace ,
3492+ self .owner ,
3493+ self .project ,
3494+ self .run_uuid ,
3495+ ** params ,
3496+ )
34493497
34503498 @async_client_handler (check_no_op = True , check_offline = True )
34513499 async def watch_logs (self , * args , ** kwargs ):
34523500 self ._raise_sync_only ("watch_logs" )
34533501
34543502 @async_client_handler (check_no_op = True , check_offline = True )
3455- async def inspect (self , * args , ** kwargs ):
3456- self ._raise_sync_only ("inspect" )
3503+ async def inspect (self ):
3504+ if not self .settings :
3505+ await self .refresh_data ()
3506+ await self ._use_agent_host ()
3507+ params = get_streams_params (connection = self .artifacts_store , status = self .status )
3508+ return await self .client .runs_v1 .inspect_run (
3509+ self .namespace ,
3510+ self .owner ,
3511+ self .project ,
3512+ self .run_uuid ,
3513+ self .run_data .kind ,
3514+ ** params ,
3515+ )
34573516
34583517 @async_client_handler (check_no_op = True , check_offline = True )
34593518 async def shell (self , * args , ** kwargs ):
0 commit comments