una-auxme / paf

Praktikum Autonomes Fahren - PAF
MIT License
9 stars 0 forks source link

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

Open ll7 opened 1 week ago

ll7 commented 1 week ago
          _:bulb: Codebase verification_

_:hammer_andwrench: 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:

🔗 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: ```diff 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: ```shell #!/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: ```shell #!/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: ```shell #!/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 https://github.com/una-auxme/paf/pull/478#discussion_r1835425809_