@@ -217,6 +217,22 @@ def submit(self, rtlil_path, *, dry_run=False, wait=False):
217217 # Poll the status API until the build is completed or failed
218218 timeout = 10.0
219219
220+ def poll_build_status ():
221+ logger .info ("Polling build status..." )
222+ status_resp = requests .get (
223+ build_status_url ,
224+ auth = (None , chipflow_api_key ),
225+ timeout = timeout
226+ )
227+ if status_resp .status_code != 200 :
228+ fail_count += 1
229+ logger .error (f"Failed to fetch build status { fail_count } times: { status_resp .text } " )
230+ if fail_count > 5 :
231+ logger .error (f"Failed to fetch build status { fail_count } times. Exiting." )
232+ raise ChipFlowError ("Error while checking build status." )
233+ status_data = status_resp .json ()
234+ return status_data .get ("status" )
235+
220236 def stream_until_fail_or_done ():
221237 nonlocal timeout
222238 fail_count = 0
@@ -244,47 +260,49 @@ def stream_until_fail_or_done():
244260 except requests .RequestException as e :
245261 if type (e ) is requests .exceptions .ConnectionError and e .response is None :
246262 fail_count += 1
263+ logger .warn (f"Issue while streaming logs: { type (e )} :{ e } response={ e .response } . Trying again." )
247264 continue #try again
248265 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" )
266+ return False
252267 if print_log_warning :
253268 logger .warning ("Log streaming may have been interrupted. Some logs may be missing." )
254269 logger .warning (f"Check { build_url } " )
255270
256- return build_status
271+ return True
257272
258273
259274 if not wait :
260275 exit (0 )
261276
262277 fail_count = 0
278+ status = "waiting"
263279 while True :
264280 logger .info ("Polling build status..." )
265281 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." )
282+ status = poll_build_status ()
277283 except requests .Timeout :
278- continue #go round again
279-
280- build_status = stream_until_fail_or_done ()
281- if build_status == "completed" :
284+ continue #go round again
285+ except requests .RequestException as e :
286+ if type (e ) is requests .exceptions .ConnectionError and e .response is None :
287+ fail_count += 1
288+ logger .warn (f"Issue while polling build: { type (e )} :{ e } response={ e .response } . Trying again." )
289+ continue #try again
290+ logger .error (f"Error while polling build: { type (e )} :{ e } response={ e .response } " )
291+ return False
292+
293+ if not stream_until_fail_or_done ():
294+ fail_count += 1
295+ logger .warn (f"Issue while streaming logs. Trying again." )
296+
297+ # Check status
298+ status = poll_build_status ()
299+ if status == "completed" :
282300 print ("Build completed successfully!" )
283301 exit (0 )
284- elif build_status == "failed" :
302+ elif status == "failed" :
285303 print ("Build failed." )
286304 exit (1 )
287- elif build_status == "running" :
305+ elif status == "running" :
288306 print ("Build running." )
289307 # Wait before polling again
290308 time .sleep (0.5 ) # Wait before polling again
0 commit comments