Skip to content

Commit

Permalink
feat: 슬랙 배치 잡 스케줄링
Browse files Browse the repository at this point in the history
  • Loading branch information
minkyu97 committed Jul 4, 2024
1 parent 4d205ec commit a1df55e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
27 changes: 26 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pydantic-settings = "^2.0.3"
httpx = "^0.25.0"
pydantic = "^2.4.2"
aiohttp = "^3.9.5"
schedule = "^1.2.2"
pytz = "^2024.1"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.2"
Expand Down
12 changes: 12 additions & 0 deletions waffledotcom/src/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import asyncio

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from waffledotcom.src.apps.router import api_router
from waffledotcom.src.batch.scheduler import schedule_tasks
from waffledotcom.src.database.connection import DBSessionFactory
from waffledotcom.src.settings import settings

Expand Down Expand Up @@ -29,6 +32,14 @@ def on_shutdown():
return on_shutdown


def _register_startup_event(app: FastAPI):
@app.on_event("startup")
def on_startup():
asyncio.create_task(schedule_tasks())

return on_startup


def create_app() -> FastAPI:
app = FastAPI(
title="waffledotcom-server",
Expand All @@ -40,4 +51,5 @@ def create_app() -> FastAPI:
_add_middlewares(app)
_add_routers(app)
_register_shutdown_event(app)
_register_startup_event(app)
return app
22 changes: 22 additions & 0 deletions waffledotcom/src/batch/scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
TODO : 추후 배치잡이 많아지거나 부하가 심하다고 판단되면 pod를 분리한다.
"""

import asyncio

from schedule import Scheduler

from waffledotcom.src.batch.slack.main import main as slack_main
from waffledotcom.src.settings import settings


async def schedule_tasks():
scheduler = Scheduler()
if settings.is_dev:
scheduler.every().day.at("00:00", "Asia/Seoul").do(slack_main)
if settings.is_prod:
scheduler.every().sunday.at("00:00", "Asia/Seoul").do(slack_main)
while True:
# 최소 주기를 60초로 설정
await asyncio.sleep(10)
scheduler.run_pending()
5 changes: 4 additions & 1 deletion waffledotcom/src/batch/slack/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ async def create_users_from_slack(

def main():
solver = DependencySolver()
asyncio.run(solver.run(create_users_from_slack))
try:
asyncio.create_task(solver.run(create_users_from_slack))
except RuntimeError:
asyncio.run(solver.run(create_users_from_slack))


if __name__ == "__main__":
Expand Down

0 comments on commit a1df55e

Please sign in to comment.