aiteamutils 0.2.40__tar.gz → 0.2.42__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aiteamutils
3
- Version: 0.2.40
3
+ Version: 0.2.42
4
4
  Summary: AI Team Utilities
5
5
  Project-URL: Homepage, https://github.com/yourusername/aiteamutils
6
6
  Project-URL: Issues, https://github.com/yourusername/aiteamutils/issues
@@ -47,8 +47,21 @@ async def get_db() -> AsyncGenerator[AsyncSession, None]:
47
47
  CustomException: 세션 생성 실패 시
48
48
  """
49
49
  db_service = get_database_service()
50
+ if not db_service or not db_service.engine:
51
+ raise CustomException(
52
+ ErrorCode.DB_CONNECTION_ERROR,
53
+ detail="Database service or engine is not properly initialized",
54
+ source_function="get_db"
55
+ )
56
+
50
57
  try:
51
58
  async with db_service.get_session() as session:
59
+ if session is None:
60
+ raise CustomException(
61
+ ErrorCode.DB_CONNECTION_ERROR,
62
+ detail="Failed to create database session",
63
+ source_function="get_db"
64
+ )
52
65
  yield session
53
66
  except Exception as e:
54
67
  raise CustomException(
@@ -58,7 +71,7 @@ async def get_db() -> AsyncGenerator[AsyncSession, None]:
58
71
  original_error=e
59
72
  )
60
73
  finally:
61
- if 'session' in locals():
74
+ if 'session' in locals() and session is not None:
62
75
  await session.close()
63
76
 
64
77
  def get_database_session(db: AsyncSession = Depends(get_db)) -> 'DatabaseService':
@@ -69,8 +82,26 @@ def get_database_session(db: AsyncSession = Depends(get_db)) -> 'DatabaseService
69
82
 
70
83
  Returns:
71
84
  DatabaseService: DatabaseService 인스턴스
85
+
86
+ Raises:
87
+ CustomException: 데이터베이스 세션이 유효하지 않은 경우
72
88
  """
73
- return DatabaseService(session=db)
89
+ if db is None:
90
+ raise CustomException(
91
+ ErrorCode.DB_CONNECTION_ERROR,
92
+ detail="Database session is not initialized",
93
+ source_function="get_database_session"
94
+ )
95
+
96
+ db_service = DatabaseService(session=db)
97
+ if db_service.db is None:
98
+ raise CustomException(
99
+ ErrorCode.DB_CONNECTION_ERROR,
100
+ detail="Failed to initialize database session",
101
+ source_function="get_database_session"
102
+ )
103
+
104
+ return db_service
74
105
 
75
106
  class DatabaseService:
76
107
  def __init__(
@@ -685,16 +716,33 @@ class DatabaseService:
685
716
 
686
717
  Returns:
687
718
  생성된 로그 엔티티
688
- """
689
- # 공통 필드 추가 (ULID를 문자열로 변환)
690
- log_data["ulid"] = str(ULID())
691
719
 
692
- # request가 있는 경우 user-agent와 ip 정보 추가
693
- if request:
694
- log_data["user_agent"] = request.headers.get("user-agent")
695
- log_data["ip_address"] = request.headers.get("x-forwarded-for")
696
-
697
- return await self.create_entity(model, log_data)
720
+ Raises:
721
+ CustomException: 로그 생성 실패 시
722
+ """
723
+ try:
724
+ # 공통 필드 추가 (ULID를 문자열로 변환)
725
+ log_data["ulid"] = str(ULID())
726
+
727
+ # request가 있는 경우 user-agent와 ip 정보 추가
728
+ if request:
729
+ log_data["user_agent"] = request.headers.get("user-agent")
730
+ log_data["ip_address"] = request.headers.get("x-forwarded-for")
731
+
732
+ # 데이터 전처리
733
+ processed_data = self.preprocess_data(model, log_data)
734
+ entity = model(**processed_data)
735
+
736
+ # 로그 엔티티 저장
737
+ self.db.add(entity)
738
+ await self.db.flush()
739
+
740
+ return entity
741
+
742
+ except Exception as e:
743
+ logging.error(f"Failed to create log: {str(e)}")
744
+ # 로그 생성 실패는 원래 작업에 영향을 주지 않도록 함
745
+ return None
698
746
 
699
747
  async def soft_delete(
700
748
  self,
@@ -0,0 +1,2 @@
1
+ """버전 정보"""
2
+ __version__ = "0.2.42"
@@ -1,2 +0,0 @@
1
- """버전 정보"""
2
- __version__ = "0.2.40"
@@ -1,54 +0,0 @@
1
- async def startup_event() -> None:
2
- """Application startup event handler."""
3
- try:
4
- # 1. 설정 및 DB 초기화
5
- init_settings(
6
- jwt_secret=settings.JWT_SECRET,
7
- jwt_algorithm=settings.JWT_ALGORITHM,
8
- db_url=settings.CORE_DB_URL,
9
- db_echo=settings.DB_ECHO,
10
- db_pool_size=settings.DB_POOL_SIZE,
11
- db_max_overflow=settings.DB_MAX_OVERFLOW,
12
- db_pool_timeout=settings.DB_POOL_TIMEOUT,
13
- db_pool_recycle=settings.DB_POOL_RECYCLE
14
- )
15
- db_service = get_database_service()
16
-
17
- if not db_service or not db_service.engine:
18
- raise RuntimeError("데이터베이스 서비스 초기화 실패")
19
-
20
- # 2. DB 연결 테스트
21
- async with db_service.engine.begin() as conn:
22
- await conn.run_sync(lambda _: None)
23
- logging.info("데이터베이스 연결 성공")
24
-
25
- # 3. 테이블 생성
26
- async with db_service.engine.begin() as conn:
27
- await conn.run_sync(Base.metadata.create_all)
28
- logging.info("데이터베이스 테이블 생성 완료")
29
-
30
- # 4. 서비스 등록
31
- register_services()
32
- service_registry.set_initialized() # 서비스 등록 완료 표시
33
-
34
- logging.info("서비스 등록 완료")
35
-
36
- except Exception as e:
37
- logging.error(f"애플리케이션 시작 실패: {str(e)}")
38
- raise RuntimeError(f"애플리케이션 시작 실패: {str(e)}")
39
-
40
- async def shutdown_event() -> None:
41
- """Application shutdown event handler."""
42
- try:
43
- # 1. 서비스 레지스트리 초기화
44
- service_registry.clear()
45
- logging.info("서비스 레지스트리 초기화 완료")
46
-
47
- # 2. 데이터베이스 연결 종료
48
- db_service = get_database_service()
49
- if db_service and db_service.engine:
50
- await db_service.engine.dispose()
51
- logging.info("데이터베이스 연결 종료")
52
- except Exception as e:
53
- logging.error(f"애플리케이션 종료 중 오류 발생: {str(e)}")
54
- raise
File without changes
File without changes
File without changes
File without changes