Skip to content

Commit

Permalink
feat: 헬스체크 로직 추가 #48
Browse files Browse the repository at this point in the history
  • Loading branch information
mungsil committed Oct 27, 2024
1 parent 06b01a5 commit 1457c82
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions scripts/develop-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,63 @@ fi

DOCKER_APP_NAME=spring

# blue가 실행 중인지 확인
EXIST_BLUE=$(docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml ps -q | xargs docker inspect -f '{{.State.Running}}')
# blue 컨테이너 실행 여부 확인
BLUE_CONTAINER_RUNNING=$(docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml ps -q | xargs docker inspect -f '{{.State.Running}}')

# green이 실행 중이면 blue를 띄움
if [ "$EXIST_BLUE" != "true" ]; then
echo "blue up"
# 컨테이너 스위칭
if [ "$BLUE_CONTAINER_RUNNING" != "true" ]; then
echo "[blue up]"
docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml up -d --build

sleep 30
ACTIVE_ENV="blue"
STANDBY_ENV="green"
ACTIVE_PORT="8081"

docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml down
docker image prune -af # 사용하지 않는 이미지 삭제

# blue가 실행 중이면 green을 띄움
else
echo "green up"
echo "[green up]"
docker-compose -p ${DOCKER_APP_NAME}-green -f docker-compose.green.yml up -d --build

sleep 30
ACTIVE_ENV="green"
STANDBY_ENV="blue"
ACTIVE_PORT="8082"

fi


# 헬스체크
MAX_RETRIES=30
RETRY_INTERVAL=2
HEALTH_CHECK_URL="http://localhost:$ACTIVE_PORT/actuator/health"
ACTIVE_HEALTH_CHECK_PASSED=false

echo "Waiting for application to be ready..."
for i in $(seq 1 $MAX_RETRIES); do
if curl -s "$HEALTH_CHECK_URL" | grep -q "UP"; then
echo "[Application is ready]"
ACTIVE_HEALTH_CHECK_PASSED=true
break

fi
echo "Waiting... ($i/$MAX_RETRIES)"
sleep $RETRY_INTERVAL

done

# 새로운 컨테이너가 제대로 떴는지 확인
if [ "$ACTIVE_HEALTH_CHECK_PASSED" = "true" ]; then
# 이전 컨테이너 종료
docker-compose -p ${DOCKER_APP_NAME}-${STANDBY_ENV} -f docker-compose.${STANDBY_ENV}.yml down
docker image prune -af
echo "[Down] $STANDBY_ENV Down"

else
echo "Health check failed after $MAX_RETRIES attempts"
# 새 컨테이너 종료
docker-compose -p ${DOCKER_APP_NAME}-${ACTIVE_ENV} -f docker-compose.${ACTIVE_ENV}.yml down
# 디스코드 알림
sudo ./discord_${ACTIVE_ENV}.sh

fi



docker-compose -p ${DOCKER_APP_NAME}-blue -f docker-compose.blue.yml down
docker image prune -af
fi

0 comments on commit 1457c82

Please sign in to comment.