1313                       "An API key can be generated at " 
1414                       "https://runpod.io/console/user/settings" )
1515
16- 
16+ def  is_completed (status :str )-> bool :
17+     """Returns true if status is one of the possible final states for a serverless request.""" 
18+     return  status  in  ["COMPLETED" , "FAILED" , "TIMED_OUT" , "CANCELLED" ]
1719# ---------------------------------------------------------------------------- # 
1820#                                    Client                                    # 
1921# ---------------------------------------------------------------------------- # 
@@ -107,7 +109,7 @@ def _fetch_job(self):
107109        status_url  =  f"{ self .endpoint_id } { self .job_id }  
108110        job_state  =  self .rp_client .get (endpoint = status_url )
109111
110-         if  job_state ["status" ]  in  [ "COMPLETED" ,  "FAILED" ,  "TIMEOUT" ] :
112+         if  is_completed ( job_state ["status" ]) :
111113            self .job_status  =  job_state ["status" ]
112114            self .job_output  =  job_state .get ("output" , None )
113115
@@ -128,7 +130,7 @@ def output(self, timeout: int = 0) -> Any:
128130            timeout: The number of seconds to wait for the server to send data before giving up. 
129131        """ 
130132        if  timeout  >  0 :
131-             while  self .status ()  not   in  [ "COMPLETED" ,  "FAILED" ,  "TIMEOUT" ] :
133+             while  not   is_completed ( self .status ()) :
132134                time .sleep (1 )
133135                timeout  -=  1 
134136                if  timeout  <=  0 :
@@ -139,6 +141,17 @@ def output(self, timeout: int = 0) -> Any:
139141
140142        return  self ._fetch_job ().get ("output" , None )
141143
144+     def  cancel (self , timeout : int  =  3 ) ->  Any :
145+         """ 
146+         Cancels the job and returns the result of the cancellation request. 
147+ 
148+         Args: 
149+             timeout: The number of seconds to wait for the server to respond before giving up. 
150+         """ 
151+         return  self .rp_client .post (f"{ self .endpoint_id } { self .job_id }  ,
152+                                    data = None ,timeout = timeout )
153+ 
154+ 
142155
143156# ---------------------------------------------------------------------------- # 
144157#                                   Endpoint                                   # 
@@ -195,3 +208,20 @@ def run_sync(self, request_input: Dict[str, Any], timeout: int = 86400) -> Dict[
195208            return  job_request .get ("output" , None )
196209
197210        return  Job (self .endpoint_id , job_request ["id" ], self .rp_client ).output (timeout = timeout )
211+ 
212+     def  health (self ,timeout : int  =  3 ) ->  Dict [str , Any ]:
213+         """ 
214+         Check the health of the endpoint (number/state of workers, number/state of requests). 
215+ 
216+         Args: 
217+             timeout: The number of seconds to wait for the server to respond before giving up. 
218+         """ 
219+         return  self .rp_client .get (f"{ self .endpoint_id }  ,timeout = timeout )
220+     def  purge_queue (self ,timeout : int  =  3 ) ->  Dict [str , Any ]:
221+         """ 
222+         Purges the endpoint's job queue and returns the result of the purge request. 
223+ 
224+         Args: 
225+             timeout: The number of seconds to wait for the server to respond before giving up. 
226+         """ 
227+         return  self .rp_client .post (f"{ self .endpoint_id }  ,data = None ,timeout = timeout )
0 commit comments