Skip to content

Commit 253bc7d

Browse files
authored
Merge pull request #196 from Runnect/fix/log-based-healthcheck
fix: 배포 헬스체크를 로그 기반 완료 신호 감지 방식으로 변경
2 parents c84c39c + b2a2de2 commit 253bc7d

1 file changed

Lines changed: 35 additions & 12 deletions

File tree

‎scripts/deploy.sh‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,51 @@ else
3030
echo "> Grafana OTel agent 미설정, 모니터링 없이 실행"
3131
fi
3232

33+
LOG_FILE=/home/ec2-user/app/nohup.out
34+
START_LINE=$(wc -l < "$LOG_FILE" 2>/dev/null || echo 0)
35+
3336
nohup java $JAVA_AGENT_OPTS -jar \
3437
-Duser.timezone=Asia/Seoul \
3538
$JAR_PATH \
36-
>> /home/ec2-user/app/nohup.out 2>&1 &
39+
>> "$LOG_FILE" 2>&1 &
40+
APP_PID=$!
41+
echo "> 프로세스 시작됨 (PID: $APP_PID)"
3742

38-
echo "> 15초 후 헬스체크 시작"
39-
sleep 15
43+
echo "> 앱 준비 신호 대기 중 (최대 240초, 프로세스 생존 여부로 실패 판단)"
44+
MAX_WAIT=240
45+
ELAPSED=0
46+
READY=false
4047

41-
for i in {1..10}; do
42-
RESPONSE=$(curl -s http://localhost:8080/actuator/health || true)
43-
if echo "$RESPONSE" | grep -q '"status":"UP"'; then
44-
echo "> 헬스체크 성공"
45-
break
46-
fi
47-
echo "> 헬스체크 실패($i/10): $RESPONSE"
48-
if [ $i -eq 10 ]; then
48+
while [ $ELAPSED -lt $MAX_WAIT ]; do
49+
if ! kill -0 $APP_PID 2>/dev/null; then
50+
echo "> 프로세스가 예기치 않게 종료됨 (크래시)"
51+
echo "> 최근 로그:"
52+
tail -n 40 "$LOG_FILE"
4953
echo "> 배포 실패"
5054
exit 1
5155
fi
52-
sleep 10
56+
57+
if tail -n +"$((START_LINE + 1))" "$LOG_FILE" | grep -q "Started ServerApplication"; then
58+
echo "> 앱 준비 완료 신호 감지 (${ELAPSED}초 소요)"
59+
READY=true
60+
break
61+
fi
62+
63+
sleep 2
64+
ELAPSED=$((ELAPSED + 2))
5365
done
5466

67+
if [ "$READY" != "true" ]; then
68+
echo "> ${MAX_WAIT}초 내에 준비 신호 없음 — 안전장치 발동"
69+
echo "> 최근 로그:"
70+
tail -n 40 "$LOG_FILE"
71+
echo "> 배포 실패"
72+
exit 1
73+
fi
74+
75+
RESPONSE=$(curl -s --max-time 10 http://localhost:8080/actuator/health || true)
76+
echo "> 헬스체크 확인: $RESPONSE"
77+
5578
echo "> Nginx 시작"
5679
sudo systemctl start nginx || true
5780
sudo systemctl enable nginx || true

0 commit comments

Comments
 (0)