1111from prometrix import PrometheusNotFound
1212from rich .console import Console
1313from slack_sdk import WebClient
14+ from tenacity import retry , stop_after_attempt , wait_exponential , retry_if_exception_type
1415import requests
1516import json
1617import traceback
@@ -373,6 +374,22 @@ def publish_input_error(url: str, scan_id: str, start_time: str, error: str):
373374def publish_error (error : str ):
374375 _send_scan_payload (settings .publish_scan_url , settings .scan_id , settings .start_time , error , is_error = True )
375376
377+ @retry (
378+ stop = stop_after_attempt (3 ),
379+ wait = wait_exponential (multiplier = 1 , min = 1 , max = 8 ),
380+ retry = retry_if_exception_type (requests .exceptions .RequestException ),
381+ reraise = True
382+ )
383+ def _post_scan_request (url : str , headers : dict , payload : dict , scan_id : str , is_error : bool ):
384+ logger_msg = "Sending error scan" if is_error else "Sending scan"
385+ logger .info (f"{ logger_msg } for scan_id={ scan_id } to url={ url } " )
386+
387+ response = requests .post (url , headers = headers , json = payload )
388+ logger .info (f"scan_id={ scan_id } | Status code: { response .status_code } " )
389+ logger .info (f"scan_id={ scan_id } | Response body: { response .text } " )
390+ return response
391+
392+
376393def _send_scan_payload (
377394 url : str ,
378395 scan_id : str ,
@@ -402,32 +419,9 @@ def _send_scan_payload(
402419 }
403420 }
404421
405- retries = 3
406- backoff = 1
407-
408- for attempt in range (1 , retries + 1 ):
409- try :
410- logger_msg = "Sending error scan" if is_error else "Sending scan"
411- logger .info (f"{ logger_msg } for scan_id={ scan_id } to url={ url } (attempt { attempt } )" )
412-
413- response = requests .post (
414- url ,
415- headers = headers ,
416- json = action_request # Let requests handle encoding
417- )
418-
419- logger .info (f"scan_id={ scan_id } | Status code: { response .status_code } " )
420- logger .info (f"scan_id={ scan_id } | Response body: { response .text } " )
421- break # Success, exit retry loop
422-
423- except requests .exceptions .RequestException as e :
424- logger .warning (f"scan_id={ scan_id } | Attempt { attempt } failed with RequestException: { e } " , exc_info = True )
425- except Exception as e :
426- logger .error (f"scan_id={ scan_id } | Attempt { attempt } failed with unexpected exception: { e } " , exc_info = True )
427-
428- if attempt < retries :
429- logger .info (f"scan_id={ scan_id } | Retrying in { backoff } seconds..." )
430- time .sleep (backoff )
431- backoff *= 2 # Exponential backoff
432- else :
433- logger .error (f"scan_id={ scan_id } | All retry attempts failed." )
422+ try :
423+ _post_scan_request (url , headers , action_request , scan_id , is_error )
424+ except requests .exceptions .RequestException as e :
425+ logger .error (f"scan_id={ scan_id } | All retry attempts failed due to RequestException: { e } " , exc_info = True )
426+ except Exception as e :
427+ logger .error (f"scan_id={ scan_id } | Unexpected error after retries: { e } " , exc_info = True )
0 commit comments