#!/bin/bash

VENV_PATH="/home/work/projects/YoLo/src/server/venv"
PROJECT_DIR="/home/work/projects/YoLo/src/server"
TRAIN_SCRIPT="train.py"
LOCK_FILE="/tmp/train_model.lock"
LOG_FILE="$PROJECT_DIR/train.log"

{
    echo "🚀 $(date) - Start running train.py"

    # Chuyển vào thư mục làm việc
    cd "$PROJECT_DIR" || { echo "❌ Failed to cd into $PROJECT_DIR"; exit 1; }

    echo "📌 Current directory: $(pwd)"
    
    # Kích hoạt môi trường ảo
    source "$VENV_PATH/bin/activate"

    # Chạy script training
    python3 "$TRAIN_SCRIPT"

    echo "✅ $(date) - Training completed!"
} 2>&1 | flock -n "$LOCK_FILE" tee -a "$LOG_FILE"
