Soumik Bose commited on
Commit
6d4468d
·
1 Parent(s): e5baa9c
Files changed (1) hide show
  1. aiven_keep_alive_service.py +44 -20
aiven_keep_alive_service.py CHANGED
@@ -11,8 +11,10 @@ from pydantic import BaseModel
11
  # Load environment variables
12
  load_dotenv()
13
 
14
- sql_uri = os.getenv("AIVEN_MYSQL_URI")
15
- postgres_uri = os.getenv("AIVEN_POSTGRES_URI")
 
 
16
 
17
  # Configure logging (console only)
18
  logging.basicConfig(
@@ -35,11 +37,21 @@ class AivenPingResponse(BaseModel):
35
  class AivenAllPingResponse(BaseModel):
36
  aiven_services: List[AivenPingResponse]
37
 
 
 
 
 
 
 
 
 
38
  # ---------- MySQL ----------
39
- def ping_mysql() -> AivenPingResponse:
40
  now = datetime.utcnow().isoformat()
 
 
41
  try:
42
- parsed_uri = urlparse(sql_uri)
43
  host = parsed_uri.hostname
44
  port = parsed_uri.port or 3306
45
  user = parsed_uri.username
@@ -47,15 +59,18 @@ def ping_mysql() -> AivenPingResponse:
47
  database = parsed_uri.path.lstrip('/') or 'defaultdb'
48
 
49
  query_params = parse_qs(parsed_uri.query)
 
50
  ssl_mode = query_params.get('ssl-mode', ['REQUIRED'])[0]
 
51
 
 
52
  connection = pymysql.connect(
53
  host=host,
54
  port=port,
55
  user=user,
56
  password=password,
57
  database=database,
58
- ssl={'mode': ssl_mode} if ssl_mode else None
59
  )
60
  with connection.cursor() as cursor:
61
  cursor.execute("SELECT 1")
@@ -73,26 +88,28 @@ def ping_mysql() -> AivenPingResponse:
73
  )
74
  except Exception as e:
75
  error_msg = str(e)
76
- logger.error(f"MySQL ping failed: {error_msg}")
77
  return AivenPingResponse(
78
  service_name="mysql_service",
79
  success=False,
80
  error=error_msg,
81
  time=now,
82
- host=None,
83
  db_type="mysql"
84
  )
85
 
86
 
87
  # ---------- PostgreSQL ----------
88
- def ping_postgres() -> AivenPingResponse:
89
  now = datetime.utcnow().isoformat()
 
 
90
  try:
91
- parsed_uri = urlparse(postgres_uri)
92
  host = parsed_uri.hostname
93
  db_name = parsed_uri.path.lstrip('/')
94
 
95
- connection = psycopg2.connect(postgres_uri)
96
  cursor = connection.cursor()
97
  cursor.execute("SELECT 1")
98
  cursor.fetchone()
@@ -110,28 +127,35 @@ def ping_postgres() -> AivenPingResponse:
110
  )
111
  except Exception as e:
112
  error_msg = str(e)
113
- logger.error(f"PostgreSQL ping failed: {error_msg}")
114
  return AivenPingResponse(
115
  service_name="postgres_service",
116
  success=False,
117
  error=error_msg,
118
  time=now,
119
- host=None,
120
  db_type="postgres"
121
  )
122
 
123
 
124
  # ---------- Run Aiven Pings ----------
125
- def ping_aiven_projects() -> list[AivenPingResponse]:
126
  logger.info("Starting Aiven service health check")
127
  results = []
128
- if sql_uri:
129
- results.append(ping_mysql())
130
- if postgres_uri:
131
- results.append(ping_postgres())
132
- logger.info("Completed Aiven service health check")
133
- final_result = AivenAllPingResponse(aiven_services=results)
134
- return final_result
135
 
 
 
 
 
 
 
 
136
 
 
 
 
137
 
 
 
 
 
 
11
  # Load environment variables
12
  load_dotenv()
13
 
14
+ # We expect these to be comma-separated strings if multiple URIs exist
15
+ # Example: "mysql://user:pass@host1/db,mysql://user:pass@host2/db"
16
+ mysql_uris_env = os.getenv("AIVEN_MYSQL_URIS", "")
17
+ postgres_uris_env = os.getenv("AIVEN_POSTGRESQL_URIS", "")
18
 
19
  # Configure logging (console only)
20
  logging.basicConfig(
 
37
  class AivenAllPingResponse(BaseModel):
38
  aiven_services: List[AivenPingResponse]
39
 
40
+ # ---------- Helper to parse URI lists ----------
41
+ def get_uris_from_env(env_str: str) -> List[str]:
42
+ """Splits a comma-separated string of URIs into a list."""
43
+ if not env_str:
44
+ return []
45
+ # Split by comma and strip whitespace
46
+ return [uri.strip() for uri in env_str.split(',') if uri.strip()]
47
+
48
  # ---------- MySQL ----------
49
+ def ping_mysql(uri: str) -> AivenPingResponse:
50
  now = datetime.utcnow().isoformat()
51
+ host = "unknown"
52
+
53
  try:
54
+ parsed_uri = urlparse(uri)
55
  host = parsed_uri.hostname
56
  port = parsed_uri.port or 3306
57
  user = parsed_uri.username
 
59
  database = parsed_uri.path.lstrip('/') or 'defaultdb'
60
 
61
  query_params = parse_qs(parsed_uri.query)
62
+ # Handle SSL mode
63
  ssl_mode = query_params.get('ssl-mode', ['REQUIRED'])[0]
64
+ ssl_config = {'mode': ssl_mode} if ssl_mode else None
65
 
66
+ # PyMySQL connection
67
  connection = pymysql.connect(
68
  host=host,
69
  port=port,
70
  user=user,
71
  password=password,
72
  database=database,
73
+ ssl=ssl_config
74
  )
75
  with connection.cursor() as cursor:
76
  cursor.execute("SELECT 1")
 
88
  )
89
  except Exception as e:
90
  error_msg = str(e)
91
+ logger.error(f"MySQL ping failed for host {host}: {error_msg}")
92
  return AivenPingResponse(
93
  service_name="mysql_service",
94
  success=False,
95
  error=error_msg,
96
  time=now,
97
+ host=host,
98
  db_type="mysql"
99
  )
100
 
101
 
102
  # ---------- PostgreSQL ----------
103
+ def ping_postgres(uri: str) -> AivenPingResponse:
104
  now = datetime.utcnow().isoformat()
105
+ host = "unknown"
106
+
107
  try:
108
+ parsed_uri = urlparse(uri)
109
  host = parsed_uri.hostname
110
  db_name = parsed_uri.path.lstrip('/')
111
 
112
+ connection = psycopg2.connect(uri)
113
  cursor = connection.cursor()
114
  cursor.execute("SELECT 1")
115
  cursor.fetchone()
 
127
  )
128
  except Exception as e:
129
  error_msg = str(e)
130
+ logger.error(f"PostgreSQL ping failed for host {host}: {error_msg}")
131
  return AivenPingResponse(
132
  service_name="postgres_service",
133
  success=False,
134
  error=error_msg,
135
  time=now,
136
+ host=host,
137
  db_type="postgres"
138
  )
139
 
140
 
141
  # ---------- Run Aiven Pings ----------
142
+ def ping_aiven_projects() -> AivenAllPingResponse:
143
  logger.info("Starting Aiven service health check")
144
  results = []
 
 
 
 
 
 
 
145
 
146
+ # Parse lists from environment variables
147
+ mysql_uris = get_uris_from_env(mysql_uris_env)
148
+ postgres_uris = get_uris_from_env(postgres_uris_env)
149
+
150
+ # Process MySQL URIs
151
+ for uri in mysql_uris:
152
+ results.append(ping_mysql(uri))
153
 
154
+ # Process PostgreSQL URIs
155
+ for uri in postgres_uris:
156
+ results.append(ping_postgres(uri))
157
 
158
+ logger.info(f"Completed health check. Checked {len(results)} services.")
159
+
160
+ final_result = AivenAllPingResponse(aiven_services=results)
161
+ return final_result