@@ -787,6 +787,32 @@ async def terminate_model(self, model_uid: str) -> JSONResponse:
787787 raise HTTPException (status_code = 500 , detail = str (e ))
788788 return JSONResponse (content = None )
789789
790+ async def terminate_model_replica (
791+ self , model_uid : str , replica_id : int
792+ ) -> JSONResponse :
793+ try :
794+ assert self ._app is not None
795+ remaining_replicas = await (
796+ await self ._get_supervisor_ref ()
797+ ).terminate_model_replica (model_uid , replica_id )
798+ if remaining_replicas == 0 :
799+ self ._app .router .routes = [
800+ route
801+ for route in self ._app .router .routes
802+ if not (
803+ hasattr (route , "path" )
804+ and isinstance (route .path , str )
805+ and route .path == "/" + model_uid
806+ )
807+ ]
808+ return JSONResponse (content = {"remaining_replicas" : remaining_replicas })
809+ except ValueError as ve :
810+ logger .error (str (ve ), exc_info = True )
811+ raise HTTPException (status_code = 400 , detail = str (ve ))
812+ except Exception as e :
813+ logger .error (e , exc_info = True )
814+ raise HTTPException (status_code = 500 , detail = str (e ))
815+
790816 async def _get_model_last_error (self , replica_model_uid : bytes , e : Exception ):
791817 if not isinstance (e , xo .ServerClosed ):
792818 return e
0 commit comments