-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (63 loc) · 2.24 KB
/
Copy pathmain.py
File metadata and controls
78 lines (63 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.routes import router as api_router
from app.core.bridge import bridge_db_to_graph
from app.core.config import settings
from app.core.mongo_db import init_db
from motor.motor_asyncio import AsyncIOMotorClient
async def run_simple_pipeline():
"""
[Test Launcher] MongoDB에서 최신 로그를 뽑아 Bridge를 통해 Graph로 전달합니다.
"""
print("[Pipeline] Initializing Manual Entry Mode...")
await asyncio.sleep(1)
# 1. DB 연결 수립
try:
client = AsyncIOMotorClient(settings.MONGODB_URL)
collection = client[settings.MONGO_DB_NAME]["raw_logs"]
await client.admin.command('ping')
print(f"[DB] Connected: {collection.full_name}")
except Exception as e:
print(f"[DB] Connection Failed: {e}")
return
# 2. 최신 로그 1건 즉시 조회 및 노드 주입
try:
print("[Search] Fetching the latest log from DB...")
last_log = await collection.find_one(sort=[("_id", -1)])
if last_log:
print(f"[Found] User: {last_log.get('user_id')}")
await bridge_db_to_graph(last_log)
else:
print("[Empty] No logs found in 'raw_logs' collection.")
except Exception as e:
print(f"[Pipeline Error] {e}")
print("\n [Pipeline] Execution complete.")
@asynccontextmanager
async def lifespan(app: FastAPI):
# 1. Startup: MongoDB 연결 및 초기화
await init_db()
# 2. 파이프라인 실행 (감시 시작)
pipeline_task = asyncio.create_task(run_simple_pipeline())
yield
# Shutdown: 서버 종료 시 감시 태스크 종료
pipeline_task.cancel()
try:
await pipeline_task
except asyncio.CancelledError:
print("Pipeline task cancelled safely.")
app = FastAPI(lifespan=lifespan, title="Amorepacific CRM Backend")
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
# Routes
app.include_router(api_router, prefix="/api/v1")
@app.get("/")
def health_check():
return {"status": "ok", "service": "crm-backend"}