일을 하기전에 무슨일을 해야할지 정리를 해보자
해야할 일
우선 실험 환경을 구성했다. 우리가 회사 내부적으로 두 개의 서버를 운영하고 있기에 각 서버에서 logic이 수행되고 있다고 가정하고 일정 시간 busy하게 server를 가동할 수 있는 image를 만들어서 docker compose에 서로 아무런 관계를 갖지 않도록 하여 두 개 가동했다.
from fastapi import FastAPI
from datetime import datetime, timezone, timedelta
import random
import time
import asyncio
app = FastAPI()
task = None
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/check")
async def health_check():
if task is None or task.done():
return True
else:
return False
@app.get("/busy")
async def start_busy():
global task
random_integer = random.randint(5, 10)
task = asyncio.create_task(make_busy(random_integer))
return random_integer
async def make_busy(ran_int: int):
start_time = time.time()
start = datetime.now(timezone.utc) + timedelta(hours=9)
while time.time() - start_time < ran_int:
await asyncio.sleep(1)
finish = datetime.now(timezone.utc) + timedelta(hours=9)
print(f'finish: start_time: {start} / finish_time: {finish} / run_time: {ran_int}')
version: '3'
services:
busy-server-1:
user: user
build:
context: .
dockerfile: ./Dockerfile
container_name: busy-server-1
volumes:
- ./workspace:/opt/workspace
working_dir: /opt/workspace
entrypoint: uvicorn app:app --host 0.0.0.0 --port 8001 --reload
environment:
- PYTHONUNBUFFERED=1
ports:
- "8001:8001"
busy-server-2:
user: user
build:
context: .
dockerfile: ./Dockerfile
container_name: busy-server-2
volumes:
- ./workspace:/opt/workspace
working_dir: /opt/workspace
entrypoint: uvicorn app:app --host 0.0.0.0 --port 8002 --reload
environment:
- PYTHONUNBUFFERED=1
ports:
- "8002:8002"