11import logging
22import os
3- from datetime import datetime
3+ import datetime
44from typing import List , Dict , Any
55import sentry_sdk
6+ from slack_sdk import WebClient
67
78import boto3
89from botocore .exceptions import ClientError , BotoCoreError
4344app = FastAPI ()
4445
4546
47+ def send_slack_alert (message ):
48+ slack_token = os .environ .get ("SLACK_BOT_TOKEN" , "" )
49+ slack_channel = os .environ .get ("SLACK_CHANNEL" , "" )
50+ if not slack_token or not slack_channel :
51+ logging .warning ("Slack token or channel is missing." )
52+ return
53+ client = WebClient (token = slack_token )
54+ bot_name = "SQS and DB"
55+ client .chat_postMessage (channel = slack_channel , text = message , username = bot_name )
56+
57+
58+ def is_connection_restored (last_attempt_time , max_retry_duration = 60 ):
59+ current_time = datetime .now ().timestamp ()
60+ last_attempt_timestamp = last_attempt_time .timestamp ()
61+ return (current_time - last_attempt_timestamp ) > max_retry_duration
62+
63+
4664# Dependency
4765def _get_db ():
48- db = session_maker ()()
49- try :
50- yield db
51- finally :
52- db .close ()
66+ retries = 5
67+ for attempt in range (retries ):
68+ try :
69+ db = session_maker ()()
70+ try :
71+ yield db
72+ finally :
73+ db .close ()
74+ return
75+ except SQLAlchemyError as e :
76+ logging .exception (f"Database connection failed (Attempt { attempt + 1 } ): { e } " )
77+ if is_connection_restored (datetime .now ()):
78+ break
79+ send_slack_alert ("DB connection issue detected in async-request-backend.." )
5380
5481
5582def _get_sqs_client ():
56- return boto3 .client ("sqs" )
83+ retries = 5
84+ for attempt in range (retries ):
85+ try :
86+ logging .info (f"SQS client successfully connected on attempt { attempt } ." )
87+ return boto3 .client ("sqs" )
88+ except (ClientError , BotoCoreError ) as e :
89+ logging .exception (
90+ f"SQS connection failed on attempt { attempt } . Retrying..."
91+ )
92+ if is_connection_restored (datetime .now ()):
93+ break
94+ send_slack_alert ("SQS connection issue detected in async-request-backend.." )
5795
5896
5997@app .get ("/health" , response_model = HealthCheckResponse )
@@ -64,9 +102,7 @@ def healthcheck(
64102 db_result = db .execute (text ("SELECT 1" ))
65103 db_reachable = len (db_result .all ()) == 1
66104 except SQLAlchemyError :
67- logging .exception (
68- "Health check of request-db failed" ,
69- )
105+ logging .exception ("Health check of request-db failed" )
70106 db_reachable = False
71107
72108 try :
0 commit comments