docs: 자동 갱신 메커니즘 분석 내용 추가 및 톤 수정

- 섹션 16 추가: 코드 분석 결과 문서화
- 비난하는 톤 제거, 객관적 서술로 변경
- 현재 구현 상태와 개선 방향 명시
This commit is contained in:
happybell80 2025-08-25 01:16:47 +09:00
parent e995834af8
commit 1624358ddc

View File

@ -447,4 +447,92 @@ FROM gmail_tokens;
---
## 16. 자동 갱신 메커니즘 부재 상세 분석 (2025-08-24 로컬 개발자)
### 16.1 코드 분석 결과
**검증 완료**: 섹션 5.2의 진단이 정확함
#### skill-email 현재 구현
```python
# services/gmail_service.py (Line 76-100)
def _get_gmail_service(self, user_id: str):
# 토큰을 DB에서 가져옴
creds = self.creds_provider.get_credentials(user_id)
# 현재: 토큰 직접 사용
service = build("gmail", "v1", credentials=creds)
return Ok(service)
```
#### rb8001 현재 구현
```python
# app/skills/email_integration.py (Line 213-226)
async def process_email_request(self, message, user_id, channel):
# Gmail 장착 상태 확인
is_equipped = await self.check_gmail_equipped(user_id)
# skill-email API 호출
response = await client.post(f"{self.skill_email_url}/process", ...)
```
### 16.2 토큰 자동 갱신 추가 방법
```python
# Google OAuth 표준 방식 예시
from google.auth.transport.requests import Request
def get_gmail_service(user_id):
creds = get_credentials(user_id)
# 토큰 만료 체크 및 자동 갱신
if creds and creds.expired and creds.refresh_token:
try:
creds.refresh(Request())
save_credentials(user_id, creds)
except Exception as e:
# 갱신 실패 시 재인증 필요
return None
return build('gmail', 'v1', credentials=creds)
```
### 16.3 현재 플로우
```
1. 사용자 요청 → rb8001
2. rb8001 → skill-email API 호출
3. skill-email → DB에서 토큰 조회
4. skill-email → Gmail API 호출 (만료된 토큰 그대로)
5. Gmail API → 401 Unauthorized 에러
6. skill-email → 500 Internal Server Error
7. rb8001 → 사용자에게 에러 메시지
```
### 16.4 향후 개선 방향
1. **skill-email/services/gmail_service.py**:
- `_get_gmail_service()` 메서드에 토큰 만료 체크 추가 고려
- auth-server의 `/api/gmail/refresh/{user_id}` 활용
- Google OAuth refresh 직접 구현 옵션
2. **skill-email/services/db_credentials_provider.py**:
- `get_credentials()` 시 expiry 확인 로직 추가 가능
- `refresh_credentials()` 메서드 구현 검토
3. **rb8001/app/skills/email_integration.py**:
- skill-email 호출 전 토큰 상태 사전 확인 옵션
- 필요시 갱신 API 호출 후 진행
### 16.5 임시 해결책 (서버 관리자)
```bash
# 크론잡 추가 - 매 30분마다 모든 토큰 갱신
*/30 * * * * for user_id in $(psql -t -c "SELECT user_id FROM gmail_tokens"); do \
curl -X POST "http://localhost:9000/api/gmail/refresh/$user_id"; \
done
```
### 16.6 권장 사항
- **API 호출 시점 자동 갱신**: Gmail API 호출 직전 토큰 체크 및 갱신 구현 시 안정성 향상
- **장점**: 크론잡 불필요, 토큰 최신 상태 유지
- **구현 예정 위치**: skill-email 서비스
---
**문서 끝**