66
77import logging
88import os
9- import threading
109from abc import ABC
1110from threading import Lock
1211from typing import Optional
1312
1413from ..exceptions import JobAttachmentsError
15- from .._utils import _retry
1614
1715CONFIG_ROOT = ".deadline"
1816COMPONENT_NAME = "job_attachments"
@@ -28,9 +26,6 @@ class CacheDB(ABC):
2826 close the connection to the cache database.
2927 """
3028
31- # Number of retry attempts for SQLite operational errors (e.g., database locks)
32- RETRY_ATTEMPTS = 3
33-
3429 def __init__ (
3530 self , cache_name : str , table_name : str , create_query : str , cache_dir : Optional [str ] = None
3631 ) -> None :
@@ -39,8 +34,6 @@ def __init__(
3934 self .cache_name : str = cache_name
4035 self .table_name : str = table_name
4136 self .create_query : str = create_query
42- self .local = threading .local ()
43- self .local_connections : set = set ()
4437
4538 try :
4639 # SQLite is included in Python installers, but might not exist if building python from source.
@@ -67,85 +60,29 @@ def __enter__(self):
6760 if self .enabled :
6861 import sqlite3
6962
70- @_retry (
71- ExceptionToCheck = sqlite3 .OperationalError ,
72- tries = self .RETRY_ATTEMPTS ,
73- delay = (0.5 , 1.5 ), # Jitter between 0.5 and 1.5 seconds
74- backoff = 1.0 ,
75- logger = logger .warning ,
76- )
77- def _connect_to_db ():
78- try :
79- connection = sqlite3 .connect (self .cache_dir , check_same_thread = False )
80- try :
81- # Test the connection by trying to query the table
82- connection .execute (f"SELECT * FROM { self .table_name } " )
83- except Exception :
84- # DB file doesn't have our table, so we need to create it
85- logger .info (
86- f"No cache entries for the current library version were found. Creating a new cache for { self .cache_name } "
87- )
88- connection .execute (self .create_query )
89- return connection
90- except sqlite3 .OperationalError as oe :
91- logger .info ("Error connecting to database, retrying." )
92- raise oe
93-
9463 try :
95- self .db_connection = _connect_to_db ()
64+ self .db_connection : sqlite3 .Connection = sqlite3 .connect (
65+ self .cache_dir , check_same_thread = False
66+ )
9667 except sqlite3 .OperationalError as oe :
9768 raise JobAttachmentsError (
98- f"Could not access cache file after { self . RETRY_ATTEMPTS } retry attempts: { self .cache_dir } "
69+ f"Could not access cache file in { self .cache_dir } "
9970 ) from oe
71+
72+ try :
73+ self .db_connection .execute (f"SELECT * FROM { self .table_name } " )
74+ except Exception :
75+ # DB file doesn't have our table, so we need to create it
76+ logger .info (
77+ f"No cache entries for the current library version were found. Creating a new cache for { self .cache_name } "
78+ )
79+ self .db_connection .execute (self .create_query )
10080 return self
10181
10282 def __exit__ (self , exc_type , exc_value , exc_traceback ):
10383 """Called when exiting the context manager."""
104-
10584 if self .enabled :
106- import sqlite3
107-
10885 self .db_connection .close ()
109- for conn in self .local_connections :
110- try :
111- conn .close ()
112- except sqlite3 .Error as e :
113- logger .warning (f"SQLite connection failed to close with error { e } " )
114-
115- self .local_connections .clear ()
116-
117- def get_local_connection (self ):
118- """Create and/or returns a thread local connection to the SQLite database."""
119- if not self .enabled :
120- return None
121- import sqlite3
122-
123- if not hasattr (self .local , "connection" ):
124-
125- @_retry (
126- ExceptionToCheck = sqlite3 .OperationalError ,
127- tries = self .RETRY_ATTEMPTS ,
128- delay = (0.5 , 1.5 ), # Jitter between 0.5 and 1.5 seconds
129- backoff = 1.0 ,
130- logger = logger .warning ,
131- )
132- def _create_local_connection ():
133- try :
134- connection = sqlite3 .connect (self .cache_dir , check_same_thread = False )
135- return connection
136- except sqlite3 .OperationalError as oe :
137- logger .info ("Error connecting to database, retrying." )
138- raise oe
139-
140- try :
141- self .local .connection = _create_local_connection ()
142- self .local_connections .add (self .local .connection )
143- except sqlite3 .OperationalError as oe :
144- raise JobAttachmentsError (
145- f"Could not create connection to cache after { self .RETRY_ATTEMPTS } retry attempts: { self .cache_dir } "
146- ) from oe
147-
148- return self .local .connection
14986
15087 @classmethod
15188 def get_default_cache_db_file_dir (cls ) -> Optional [str ]:
@@ -162,23 +99,12 @@ def remove_cache(self) -> None:
16299 """
163100 Removes the underlying cache contents from the file system.
164101 """
165-
166102 if self .enabled :
167- import sqlite3
168-
169103 self .db_connection .close ()
170- conn_list = list (self .local_connections )
171- for conn in conn_list :
172- try :
173- conn .close ()
174- self .local_connections .remove (conn )
175- except sqlite3 .Error as e :
176- logger .warning (f"SQLite connection failed to close with error { e } " )
177104
178105 logger .debug (f"The cache { self .cache_dir } will be removed" )
179106 try :
180107 os .remove (self .cache_dir )
181108 except Exception as e :
182109 logger .error (f"Error occurred while removing the cache file { self .cache_dir } : { e } " )
183-
184110 raise e
0 commit comments