8
8
"""
9
9
10
10
import asyncio
11
+ from datetime import UTC , datetime , timedelta
11
12
import logging
12
13
from os import makedirs
13
14
import re
@@ -62,9 +63,6 @@ async def fetch_dcl_certificates(
62
63
) -> int :
63
64
"""Fetch DCL PAA Certificates."""
64
65
LOGGER .info ("Fetching the latest PAA root certificates from DCL." )
65
- if not PAA_ROOT_CERTS_DIR .is_dir ():
66
- loop = asyncio .get_running_loop ()
67
- await loop .run_in_executor (None , makedirs , PAA_ROOT_CERTS_DIR )
68
66
fetch_count : int = 0
69
67
base_urls = set ()
70
68
# determine which url's need to be queried.
@@ -152,11 +150,30 @@ async def fetch_git_certificates() -> int:
152
150
return fetch_count
153
151
154
152
153
+ async def _get_certificate_age () -> datetime :
154
+ """Get last time PAA Certificates have been fetched."""
155
+ loop = asyncio .get_running_loop ()
156
+ stat = await loop .run_in_executor (None , PAA_ROOT_CERTS_DIR .stat )
157
+ return datetime .fromtimestamp (stat .st_mtime , tz = UTC )
158
+
159
+
155
160
async def fetch_certificates (
156
161
fetch_test_certificates : bool = True ,
157
162
fetch_production_certificates : bool = True ,
158
163
) -> int :
159
164
"""Fetch PAA Certificates."""
165
+ loop = asyncio .get_running_loop ()
166
+
167
+ if not PAA_ROOT_CERTS_DIR .is_dir ():
168
+ await loop .run_in_executor (None , makedirs , PAA_ROOT_CERTS_DIR )
169
+ else :
170
+ stat = await loop .run_in_executor (None , PAA_ROOT_CERTS_DIR .stat )
171
+ last_fetch = datetime .fromtimestamp (stat .st_mtime , tz = UTC )
172
+ if last_fetch > datetime .now (tz = UTC ) - timedelta (days = 1 ):
173
+ LOGGER .info (
174
+ "Skip fetching certificates (already fetched within the last 24h)."
175
+ )
176
+ return 0
160
177
161
178
fetch_count = await fetch_dcl_certificates (
162
179
fetch_test_certificates = fetch_test_certificates ,
@@ -166,4 +183,6 @@ async def fetch_certificates(
166
183
if fetch_test_certificates :
167
184
fetch_count += await fetch_git_certificates ()
168
185
186
+ await loop .run_in_executor (None , PAA_ROOT_CERTS_DIR .touch )
187
+
169
188
return fetch_count
0 commit comments