Skip to content

Commit e481341

Browse files
Refactor DatabaseFactory to lazily initialize the lock and improve file record handling in BatchService
1 parent 7815d5c commit e481341

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/backend/common/database/database_factory.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99

1010
class DatabaseFactory:
1111
_instance: Optional[DatabaseBase] = None
12-
_lock: asyncio.Lock = asyncio.Lock()
12+
_lock: Optional[asyncio.Lock] = None
1313
_logger = AppLogger("DatabaseFactory")
1414

15+
@staticmethod
16+
def _get_lock() -> asyncio.Lock:
17+
if DatabaseFactory._lock is None:
18+
DatabaseFactory._lock = asyncio.Lock()
19+
return DatabaseFactory._lock
20+
1521
@staticmethod
1622
async def get_database():
1723
if DatabaseFactory._instance is not None:
1824
return DatabaseFactory._instance
1925

20-
async with DatabaseFactory._lock:
26+
async with DatabaseFactory._get_lock():
2127
# Double-check after acquiring the lock
2228
if DatabaseFactory._instance is not None:
2329
return DatabaseFactory._instance

src/backend/common/services/batch_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ async def upload_file_to_batch(self, batch_id: str, user_id: str, file: UploadFi
288288

289289
# Create file entry
290290
file_record_obj = await self.database.add_file(batch_id, file_id, file.filename, blob_path)
291-
file_record = file_record_obj.dict() if hasattr(file_record_obj, 'dict') else file_record_obj
291+
file_record_dict = getattr(file_record_obj, "dict", None)
292+
file_record = file_record_dict() if callable(file_record_dict) else file_record_obj
292293

293294
await self.database.add_file_log(
294295
UUID(file_id),

0 commit comments

Comments
 (0)