feat: v9.1 업그레이드 — CS 기록 메뉴 + 반품 목록 검색 + 구글 연동 안정화

원본(CS 통합 프로그램 v9.1, 2026.09.03)을 서버판에 병합. 서버용 개조는 유지:
SSO(AuthGuard·SessionMiddleware), /corm 서브경로(root_path·APP_BASE_PATH),
X-Service-Token, /health/db, 환경변수 DB 설정, cafe24/naver 키 환경변수.

- CS 기록 메뉴(routers/cs_records.py, cs_records.js/css): 달력별 기록·완료·복사·
  다음 업무일 이월, 상시 메모(폴더·색), 일정, 엑셀 내려받기/업로드.
  표는 itemcode_db 에 자동 생성(CREATE TABLE IF NOT EXISTS). DB 설정은 원본의
  하드코딩 대신 CS_RECORD_DB_* → POSTGRES_*/ITEMCODE_DB 환경변수.
- New CS 누락·파손·불량·오배송 발주 → CS 기록 자동 저장.
- 반품 신청/입고 목록: 최근 3개월 기본, 전체 보기, 원송장번호 전체 검색.
- google_runtime.py: 구글 호출 시간 제한·전용 작업 슬롯, 설정 시트 스냅샷 캐시
  (예전 10분 인메모리 캐시와 /api/config/refresh 대체), 설정 저장 한 번에 원자적으로.
- 밀크런: 탭을 열 때만 구글 시트 조회.
- 자동발주 메뉴는 원본처럼 막지 않고 그대로 둠, 엑셀 내려받기 링크도 /corm 경로.
- requirements: holidays 추가. tests/test_google_resilience.py(서비스 토큰으로 SSO 통과).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 16:07:53 +09:00
parent 065e81bf97
commit d2fdec5c43
18 changed files with 5608 additions and 253 deletions
+243
View File
@@ -0,0 +1,243 @@
"""Bounded Google I/O and last-known-good settings, independent of the ASGI loop."""
import asyncio
import copy
import functools
import json
import logging
import os
import tempfile
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import requests
from fastapi import HTTPException
from google.auth.transport.requests import AuthorizedSession, Request
logger = logging.getLogger(__name__)
GOOGLE_CONNECT_TIMEOUT = float(os.getenv("GOOGLE_CONNECT_TIMEOUT", "3"))
GOOGLE_READ_TIMEOUT = float(os.getenv("GOOGLE_READ_TIMEOUT", "10"))
GOOGLE_TIMEOUT = (GOOGLE_CONNECT_TIMEOUT, GOOGLE_READ_TIMEOUT)
GOOGLE_WORKERS = max(1, int(os.getenv("GOOGLE_WORKERS", "4")))
_executor = ThreadPoolExecutor(max_workers=GOOGLE_WORKERS, thread_name_prefix="google-io")
_slots = threading.BoundedSemaphore(GOOGLE_WORKERS)
_settings_write_lock = threading.RLock()
_history_lock = threading.RLock()
def _bounded_timeout(timeout):
if timeout is None:
return GOOGLE_TIMEOUT
if isinstance(timeout, tuple):
connect, read = timeout
else:
connect = read = timeout
return (
min(connect, GOOGLE_CONNECT_TIMEOUT) if connect is not None else GOOGLE_CONNECT_TIMEOUT,
min(read, GOOGLE_READ_TIMEOUT) if read is not None else GOOGLE_READ_TIMEOUT,
)
class GoogleTimeoutSession(requests.Session):
"""Also bound token refresh calls, which otherwise default to 120 seconds."""
def request(self, method, url, **kwargs):
kwargs["timeout"] = _bounded_timeout(kwargs.get("timeout"))
return super().request(method, url, **kwargs)
class GoogleAuthorizedSession(AuthorizedSession):
def request(self, method, url, **kwargs):
kwargs["timeout"] = _bounded_timeout(kwargs.get("timeout"))
return super().request(method, url, **kwargs)
def create_google_session(credentials):
return GoogleAuthorizedSession(
credentials,
auth_request=Request(session=GoogleTimeoutSession()),
refresh_timeout=GOOGLE_CONNECT_TIMEOUT + GOOGLE_READ_TIMEOUT,
)
def create_google_client(credentials):
import gspread
client = gspread.Client(auth=credentials, session=create_google_session(credentials))
client.set_timeout(GOOGLE_TIMEOUT)
return client
async def run_google_io(function, *args, **kwargs):
# No unbounded executor queue, and no consumption of FastAPI's DB worker pool.
if not _slots.acquire(blocking=False):
raise HTTPException(
status_code=503,
detail="구글 연동 작업이 진행 중입니다. 잠시 후 다시 시도해주세요.",
headers={"Retry-After": "3"},
)
try:
future = _executor.submit(functools.partial(function, *args, **kwargs))
except BaseException:
_slots.release()
raise
future.add_done_callback(lambda _: _slots.release())
# Disconnecting the browser must not free a slot while its write still runs.
return await asyncio.shield(asyncio.wrap_future(future))
def google_worker(function):
@functools.wraps(function)
async def wrapped(*args, **kwargs):
return await run_google_io(function, *args, **kwargs)
return wrapped
def _serialized(lock):
def decorate(function):
@functools.wraps(function)
def wrapped(*args, **kwargs):
with lock:
return function(*args, **kwargs)
return wrapped
return decorate
settings_update = _serialized(_settings_write_lock)
history_access = _serialized(_history_lock)
class SettingsCache:
"""Serve snapshots immediately; refresh stale data once, in a separate worker."""
def __init__(self, path, spreadsheet_id, ttl=60, retry_delay=15):
self.path = Path(path)
self.spreadsheet_id = spreadsheet_id
self.ttl = ttl
self.retry_delay = retry_delay
self._lock = threading.RLock()
self._refresh_lock = threading.Lock()
self._executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="settings-refresh")
self._pending = None
self._rows = None
self._loaded_at = float("-inf")
self._retry_at = 0.0
self._generation = 0
self._restore()
@property
def has_value(self):
with self._lock:
return self._rows is not None
@property
def stale(self):
with self._lock:
return time.monotonic() - self._loaded_at >= self.ttl
def _restore(self):
try:
payload = json.loads(self.path.read_text(encoding="utf-8"))
rows = payload["rows"]
if payload["spreadsheet_id"] != self.spreadsheet_id:
return
if not isinstance(rows, list) or not all(
isinstance(row, list) and all(isinstance(cell, str) for cell in row)
for row in rows
):
return
self._rows = rows
# A restored snapshot is usable immediately, but always revalidated.
except (OSError, ValueError, KeyError, TypeError):
pass
def _persist_locked(self):
temporary = None
try:
self.path.parent.mkdir(parents=True, exist_ok=True)
with tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", dir=self.path.parent, delete=False
) as target:
temporary = target.name
json.dump(
{"spreadsheet_id": self.spreadsheet_id, "rows": self._rows},
target, ensure_ascii=False,
)
os.replace(temporary, self.path)
except OSError as exc:
logger.warning("Settings snapshot persistence failed (%s)", type(exc).__name__)
finally:
if temporary and os.path.exists(temporary):
os.unlink(temporary)
def publish(self, rows):
with self._lock:
self._rows = copy.deepcopy(rows)
self._loaded_at = time.monotonic()
self._retry_at = 0.0
self._generation += 1
self._persist_locked()
def invalidate(self):
with self._lock:
self._loaded_at = float("-inf")
self._retry_at = 0.0
self._generation += 1
def _load(self, loader, force=False):
with self._refresh_lock:
with self._lock:
if not force:
if self._rows is not None and not self.stale:
return copy.deepcopy(self._rows)
if time.monotonic() < self._retry_at:
if self._rows is not None:
return copy.deepcopy(self._rows)
raise RuntimeError("Settings refresh is cooling down")
generation = self._generation
try:
rows = loader()
except Exception:
with self._lock:
self._retry_at = time.monotonic() + self.retry_delay
raise
with self._lock:
# An earlier read must never replace a subsequently saved setting.
if generation == self._generation:
self.publish(rows)
return copy.deepcopy(self._rows)
def _background_load(self, loader):
try:
self._load(loader)
except Exception as exc:
# Do not log URLs, credential contents or settings data.
logger.warning("Google settings refresh failed (%s); retaining snapshot", type(exc).__name__)
def refresh_in_background(self, loader):
with self._lock:
if time.monotonic() < self._retry_at:
return
if self._pending is None or self._pending.done():
self._pending = self._executor.submit(self._background_load, loader)
def read(self, loader, force=False):
if force:
# Editors must not overwrite Google using a stale cached snapshot.
return self._load(loader, force=True)
with self._lock:
if self._rows is not None:
rows = copy.deepcopy(self._rows)
if self.stale:
self.refresh_in_background(loader)
return rows
return self._load(loader)
def close(self):
self._executor.shutdown(wait=True)