Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new docker compose command instruction based on the refactor suggestion #498

Open
ll7 opened this issue Nov 13, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@ll7
Copy link
Member

ll7 commented Nov 13, 2024

          _:bulb: Codebase verification_

🛠️ Refactor suggestion

Consider using service dependencies instead of sleep

Based on the analysis of the codebase, I'm modifying my previous recommendation. The docker-compose.cicd.yaml shows that the agent service properly depends on carla-simulator and roscore services. Instead of using a complex retry mechanism, we should leverage Docker Compose's built-in dependency handling:

     command: |-
-      bash -c "sleep 10 && sudo chown -R ${USER_UID}:${USER_GID} ../ && \
-      sudo chmod -R a+w ../ && sudo mkdir -p $${XDG_RUNTIME_DIR} && sudo chmod 0700 $${XDG_RUNTIME_DIR} && sudo chown -R ${USER_UID}:${USER_GID} $${XDG_RUNTIME_DIR} && \
-      (rqt_console &) && disown -a && \
-      python3 /opt/leaderboard/leaderboard/leaderboard_evaluator.py --debug=0 --routes=$${ROUTE} --agent=/workspace/code/agent/src/agent/agent.py --host=$${CARLA_SIM_HOST} --track=MAP"
+      bash -c '
+        # Setup error handling
+        set -e
+        
+        # Setup permissions
+        echo "Setting up permissions..."
+        sudo chown -R ${USER_UID}:${USER_GID} ../ || exit 1
+        sudo chmod -R a+w ../ || exit 1
+        
+        # Setup runtime directory
+        echo "Setting up runtime directory..."
+        sudo mkdir -p $${XDG_RUNTIME_DIR} || exit 1
+        sudo chmod 0700 $${XDG_RUNTIME_DIR} || exit 1
+        sudo chown -R ${USER_UID}:${USER_GID} $${XDG_RUNTIME_DIR} || exit 1
+        
+        # Start rqt_console in background
+        echo "Starting rqt_console..."
+        rqt_console &
+        disown -a
+        
+        # Start the evaluator
+        echo "Starting leaderboard evaluator..."
+        exec python3 /opt/leaderboard/leaderboard/leaderboard_evaluator.py \
+          --debug=0 \
+          --routes=$${ROUTE} \
+          --agent=/workspace/code/agent/src/agent/agent.py \
+          --host=$${CARLA_SIM_HOST} \
+          --track=MAP
+      '

Add these service dependencies to ensure proper startup order:

    depends_on:
      - carla-simulator
      - roscore

This approach:

  • Removes the arbitrary sleep 10 and relies on Docker's service orchestration
  • Maintains error handling for critical file operations
  • Improves readability and logging
  • Preserves the original functionality in a more robust way
🔗 Analysis chain

Enhance command robustness and readability.

The current command structure has several potential issues:

  1. The sleep 10 is a brittle way to handle timing dependencies
  2. No error handling for critical operations (chmod, chown, mkdir)
  3. Multiple sudo operations without checking for failures

Consider refactoring to this more robust approach:

     command: |-
-      bash -c "sleep 10 && sudo chown -R ${USER_UID}:${USER_GID} ../ && \
-      sudo chmod -R a+w ../ && sudo mkdir -p $${XDG_RUNTIME_DIR} && sudo chmod 0700 $${XDG_RUNTIME_DIR} && sudo chown -R ${USER_UID}:${USER_GID} $${XDG_RUNTIME_DIR} && \
-      (rqt_console &) && disown -a && \
-      python3 /opt/leaderboard/leaderboard/leaderboard_evaluator.py --debug=0 --routes=$${ROUTE} --agent=/workspace/code/agent/src/agent/agent.py --host=$${CARLA_SIM_HOST} --track=MAP"
+      bash -c '
+        # Setup error handling
+        set -e
+        
+        # Function to retry operations with timeout
+        wait_for_service() {
+          local retries=30
+          local wait_time=2
+          while [ $retries -gt 0 ]; do
+            if [ -d "../" ]; then
+              return 0
+            fi
+            retries=$((retries-1))
+            sleep $wait_time
+          done
+          return 1
+        }
+        
+        # Wait for services to be ready
+        echo "Waiting for services..."
+        wait_for_service || { echo "Service startup timeout"; exit 1; }
+        
+        # Setup permissions
+        echo "Setting up permissions..."
+        sudo chown -R ${USER_UID}:${USER_GID} ../ || { echo "Failed to set ownership"; exit 1; }
+        sudo chmod -R a+w ../ || { echo "Failed to set permissions"; exit 1; }
+        
+        # Setup runtime directory
+        echo "Setting up runtime directory..."
+        sudo mkdir -p $${XDG_RUNTIME_DIR} || { echo "Failed to create runtime dir"; exit 1; }
+        sudo chmod 0700 $${XDG_RUNTIME_DIR} || { echo "Failed to set runtime dir permissions"; exit 1; }
+        sudo chown -R ${USER_UID}:${USER_GID} $${XDG_RUNTIME_DIR} || { echo "Failed to set runtime dir ownership"; exit 1; }
+        
+        # Start rqt_console in background
+        echo "Starting rqt_console..."
+        rqt_console &
+        disown -a
+        
+        # Start the evaluator
+        echo "Starting leaderboard evaluator..."
+        exec python3 /opt/leaderboard/leaderboard/leaderboard_evaluator.py \
+          --debug=0 \
+          --routes=$${ROUTE} \
+          --agent=/workspace/code/agent/src/agent/agent.py \
+          --host=$${CARLA_SIM_HOST} \
+          --track=MAP
+      '

This refactored version:

  • Replaces static sleep with dynamic service checking
  • Adds proper error handling and logging
  • Improves readability with structured sections
  • Uses exec to replace shell with the Python process

Let's verify the service dependencies:


Let's search for any service dependencies and timing-related patterns in the build directory to better understand the startup requirements.


Let's examine the service dependencies and health check configurations to better understand the startup requirements and validate our recommendation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for other services that might need to be ready before agent starts
rg -l "depends_on.*agent" build/

Length of output: 128


Script:

#!/bin/bash
# Check for service dependencies in docker-compose files
rg -l "depends_on" build/

# Check for any references to timing or startup order
rg "sleep|wait" build/

Length of output: 1258


Script:

#!/bin/bash
# Check the content of docker-compose.cicd.yaml to understand dependencies
cat build/docker-compose.cicd.yaml

# Check if there are any health checks defined
rg "healthcheck" build/

Length of output: 842

Originally posted by @coderabbitai[bot] in #478 (comment)

@ll7 ll7 added the enhancement New feature or request label Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

1 participant