-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
76 lines (61 loc) · 2.33 KB
/
main.py
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
from apscheduler.schedulers.background import BackgroundScheduler
from datetime import datetime
from fastapi import FastAPI, Depends
from pytz import timezone
from database import get_db, SessionLocal
from domain.challenge import challenge_router
from domain.challenge.additional import additional_router
from domain.challenge.challenge_crud import start_challenge_server, end_challenge_server
from domain.challenge.diary import diary_router
from domain.challenge.item import item_router
from domain.desc import desc_router
from domain.user import user_router, friend_router
from models import ChallengeStatusType, ChallengeMaster
app = FastAPI()
# origins = [
# "http://127.0.0.1:5173",
# ]
#
# app.add_middleware(
# CORSMiddleware,
# allow_origins=origins,
# allow_credentials=True,
# allow_methods=["*"],
# allow_headers=["*"],
# )
app.include_router(user_router.router)
app.include_router(friend_router.router)
app.include_router(challenge_router.router)
# app.include_router(daily_router.router)
app.include_router(diary_router.router)
# app.include_router(weekly_router.router)
app.include_router(additional_router.router)
app.include_router(item_router.router)
app.include_router(desc_router.router)
scheduler = BackgroundScheduler()
scheduler.configure(timezone=timezone('Asia/Seoul'))
def check_and_start_challenges():
db = SessionLocal() # 직접 세션 생성
try:
current_date = datetime.utcnow()
challenges_to_start = db.query(ChallengeMaster).filter(
ChallengeMaster.START_DT <= current_date,
ChallengeMaster.CHALLENGE_STATUS == ChallengeStatusType.PENDING,
ChallengeMaster.DELETE_YN == False,
).all()
for challenge in challenges_to_start:
start_challenge_server(db, challenge)
challenges_to_end = db.query(ChallengeMaster).filter(
ChallengeMaster.END_DT <= current_date,
ChallengeMaster.CHALLENGE_STATUS == ChallengeStatusType.PROGRESS,
ChallengeMaster.DELETE_YN == False,
).all()
for challenge in challenges_to_end:
end_challenge_server(db, challenge)
db.commit()
finally:
db.close() # 세션 닫기
# 스케줄러에 작업 추가
scheduler.add_job(check_and_start_challenges, 'cron', hour=4, minute=00)
# 스케줄러 시작
scheduler.start()