diff --git a/.github/workflows/be-rolling-deploy.yml b/.github/workflows/be-rolling-deploy.yml index ea6c9c6f..ea776827 100644 --- a/.github/workflows/be-rolling-deploy.yml +++ b/.github/workflows/be-rolling-deploy.yml @@ -14,6 +14,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 + - name: Prepare Deploy + run: | + cd ~/deploy && ./prepare-deploy.sh + - name: Run Prod1 instance deploy script run: | cd ~/deploy && ./deploy.sh @@ -25,16 +29,24 @@ jobs: steps: - name: Wait for Prod1 instance to be ready - run: sleep 30 + run: sleep 25 - name: Health check for Prod1 instance run: | - RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8080/health) - if [ $RESPONSE -ne 200 ]; then - echo "Prod1 instance deployment failed." - exit 1 - fi - echo "Prod1 instance is healthy." + ATTEMPTS=0 + MAX_ATTEMPTS=3 + until [ $ATTEMPTS -ge $MAX_ATTEMPTS ]; do + RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8080/health) + if [ $RESPONSE -eq 200 ]; then + echo "Prod1 instance is healthy." + exit 0 + fi + echo "Health check failed, attempt $((ATTEMPTS+1))/$MAX_ATTEMPTS. Retrying in 5 seconds..." + ATTEMPTS=$((ATTEMPTS+1)) + sleep 5 + done + echo "Prod1 instance deployment failed after $MAX_ATTEMPTS attempts." + exit 1 deploy-prod2: name: Deploy to Prod2 Instance @@ -45,6 +57,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 + - name: Prepare Deploy + run: | + cd ~/deploy && ./prepare-deploy.sh + - name: Run Prod2 instance deploy script run: | cd ~/deploy && ./deploy.sh @@ -56,13 +72,21 @@ jobs: steps: - name: Wait for Prod2 instance to be ready - run: sleep 30 + run: sleep 25 - name: Health check for Prod2 instance run: | - RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8080/health) - if [ $RESPONSE -ne 200 ]; then - echo "Prod2 instance deployment failed." - exit 1 - fi - echo "Prod2 instance is healthy." + ATTEMPTS=0 + MAX_ATTEMPTS=3 + until [ $ATTEMPTS -ge $MAX_ATTEMPTS ]; do + RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null http://localhost:8080/health) + if [ $RESPONSE -eq 200 ]; then + echo "Prod2 instance is healthy." + exit 0 + fi + echo "Health check failed, attempt $((ATTEMPTS+1))/$MAX_ATTEMPTS. Retrying in 5 seconds..." + ATTEMPTS=$((ATTEMPTS+1)) + sleep 5 + done + echo "Prod2 instance deployment failed after $MAX_ATTEMPTS attempts." + exit 1 diff --git a/backend/src/main/java/mouda/backend/common/HealthCheckController.java b/backend/src/main/java/mouda/backend/common/HealthCheckController.java index 3a800fbd..513e8c8e 100644 --- a/backend/src/main/java/mouda/backend/common/HealthCheckController.java +++ b/backend/src/main/java/mouda/backend/common/HealthCheckController.java @@ -1,14 +1,39 @@ package mouda.backend.common; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; +import jakarta.servlet.http.HttpServletRequest; + @RestController public class HealthCheckController { + private static final String HOST_IPV4 = "127.0.0.1"; + private static final String HOST_IPV6 = "0:0:0:0:0:0:0:1"; + private static final String HOST_NAME = "localhost"; + + private final AtomicBoolean isTerminating = new AtomicBoolean(false); + @GetMapping("/health") public ResponseEntity checkHealth() { + if (isTerminating.get()) { + return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); + } return ResponseEntity.ok().build(); } + + @PostMapping("/termination") + public ResponseEntity terminate(HttpServletRequest request) { + String remoteHost = request.getRemoteHost(); + if (HOST_IPV6.equals(remoteHost) || HOST_IPV4.equals(remoteHost) || HOST_NAME.equals(remoteHost)) { + isTerminating.set(true); + return ResponseEntity.ok().build(); + } + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } } diff --git a/backend/src/main/resources/application-prod.yml b/backend/src/main/resources/application-prod.yml index 8ac4cbe4..fd731d49 100644 --- a/backend/src/main/resources/application-prod.yml +++ b/backend/src/main/resources/application-prod.yml @@ -64,3 +64,4 @@ server: tomcat: mbeanregistry: enabled: true + shutdown: graceful