@@ -216,10 +216,29 @@ def submit(self, rtlil_path, *, dry_run=False, wait=False):
216216
217217 # Poll the status API until the build is completed or failed
218218 timeout = 10.0
219+ fail_count = 0
220+
221+ def poll_build_status ():
222+ nonlocal timeout
223+ nonlocal fail_count
224+ logger .info ("Polling build status..." )
225+ status_resp = requests .get (
226+ build_status_url ,
227+ auth = (None , chipflow_api_key ),
228+ timeout = timeout
229+ )
230+ if status_resp .status_code != 200 :
231+ fail_count += 1
232+ logger .error (f"Failed to fetch build status { fail_count } times: { status_resp .text } " )
233+ if fail_count > 5 :
234+ logger .error (f"Failed to fetch build status { fail_count } times. Exiting." )
235+ raise ChipFlowError ("Error while checking build status." )
236+ status_data = status_resp .json ()
237+ return status_data .get ("status" )
219238
220239 def stream_until_fail_or_done ():
221240 nonlocal timeout
222- fail_count = 0
241+ nonlocal fail_count
223242 print_log_warning = False
224243 while fail_count < (2 * 60 // timeout ):
225244 try :
@@ -244,47 +263,52 @@ def stream_until_fail_or_done():
244263 except requests .RequestException as e :
245264 if type (e ) is requests .exceptions .ConnectionError and e .response is None :
246265 fail_count += 1
266+ logger .warn (f"Issue while streaming logs: { type (e )} :{ e } response={ e .response } . Trying again." )
247267 continue #try again
248268 logger .error (f"Error while streaming logs: { type (e )} :{ e } response={ e .response } " )
249- return "failed"
250- status_data = status_resp .json ()
251- build_status = status_data .get ("status" )
269+ return False
252270 if print_log_warning :
253271 logger .warning ("Log streaming may have been interrupted. Some logs may be missing." )
254272 logger .warning (f"Check { build_url } " )
255273
256- return build_status
274+ return True
257275
258276
259277 if not wait :
260278 exit (0 )
261279
262280 fail_count = 0
281+ status = "waiting"
263282 while True :
264283 logger .info ("Polling build status..." )
265284 try :
266- status_resp = requests .get (
267- build_status_url ,
268- auth = (None , chipflow_api_key ),
269- timeout = timeout
270- )
271- if status_resp .status_code != 200 :
272- fail_count += 1
273- logger .error (f"Failed to fetch build status { fail_count } times: { status_resp .text } " )
274- if fail_count > 5 :
275- logger .error (f"Failed to fetch build status { fail_count } times. Exiting." )
276- raise ChipFlowError ("Error while checking build status." )
285+ status = poll_build_status ()
277286 except requests .Timeout :
278- continue #go round again
287+ continue #go round again
288+ except requests .RequestException as e :
289+ if type (e ) is requests .exceptions .ConnectionError and e .response is None :
290+ fail_count += 1
291+ logger .warn (f"Issue while polling build: { type (e )} :{ e } response={ e .response } . Trying again." )
292+ continue #try again
293+ logger .error (f"Network error while polling build: { type (e )} :{ e } response={ e .response } " )
294+ exit (1 )
295+ except Exception as e :
296+ logger .error (f"Unexpected error while polling build: { type (e )} :{ e } " )
297+
298+
299+ if not stream_until_fail_or_done ():
300+ fail_count += 1
301+ logger .warn ("Issue while streaming logs. Trying again." )
279302
280- build_status = stream_until_fail_or_done ()
281- if build_status == "completed" :
303+ # Check status
304+ status = poll_build_status ()
305+ if status == "completed" :
282306 print ("Build completed successfully!" )
283307 exit (0 )
284- elif build_status == "failed" :
308+ elif status == "failed" :
285309 print ("Build failed." )
286310 exit (1 )
287- elif build_status == "running" :
311+ elif status == "running" :
288312 print ("Build running." )
289313 # Wait before polling again
290314 time .sleep (0.5 ) # Wait before polling again
0 commit comments