#!/bin/bash # ParentZone Downloaders Daily Scheduler # This script runs both the config downloader and snapshot downloader # Set environment variables (cron doesn't inherit Docker ENV vars) export PYTHONPATH=/app export PYTHONUNBUFFERED=1 LOG_DIR="/app/data/logs" LOG_FILE="$LOG_DIR/scheduler_$(date +%Y%m%d).log" SNAPSHOT_CONFIG_FILE="/app/config/snapshot_config.json" ASSET_CONFIG_FILE="/app/config/parentzone_config.json" # Create log directory if it doesn't exist mkdir -p "$LOG_DIR" # Function to log messages with timestamp log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE" } # Function to run a command and log its output run_with_logging() { local command="$1" local description="$2" local temp_output="/tmp/scheduler_output_$$.txt" log_message "Starting: $description" log_message "Command: $command" # Run the command and capture both stdout and stderr to temp file eval "$command" > "$temp_output" 2>&1 local exit_code=$? # Log the full output if [ -s "$temp_output" ]; then log_message "--- Command Output Start ---" cat "$temp_output" >> "$LOG_FILE" log_message "--- Command Output End ---" fi # Cleanup temp file rm -f "$temp_output" if [ $exit_code -eq 0 ]; then log_message "SUCCESS: $description completed successfully" return 0 else log_message "ERROR: $description failed with exit code $exit_code" return 1 fi } # Main execution log_message "=== ParentZone Downloaders Daily Run Started ===" # Change to app directory first cd /app # Debug: Log environment information log_message "=== DEBUG: Environment Information ===" log_message "PATH: $PATH" log_message "PYTHONPATH: $PYTHONPATH" log_message "PWD: $(pwd)" log_message "USER: $(whoami)" log_message "which python3: $(which python3 2>&1 || echo 'NOT FOUND')" log_message "python3 --version: $(python3 --version 2>&1 || echo 'FAILED')" log_message "ls -la src/: $(ls -la src/ 2>&1 | head -5)" log_message "=== END DEBUG ===" # Check if config files exist if [ ! -f "$SNAPSHOT_CONFIG_FILE" ]; then log_message "ERROR: Snapshot configuration file $SNAPSHOT_CONFIG_FILE not found" exit 1 fi if [ ! -f "$ASSET_CONFIG_FILE" ]; then log_message "WARNING: Asset configuration file $ASSET_CONFIG_FILE not found, skipping asset downloader" SKIP_ASSET_DOWNLOADER=true else SKIP_ASSET_DOWNLOADER=false fi # Run config-based asset downloader if [ "$SKIP_ASSET_DOWNLOADER" = false ]; then run_with_logging "python3 src/config_downloader.py --config $ASSET_CONFIG_FILE" "Config Asset Downloader" asset_result=$? else log_message "SKIPPED: Config Asset Downloader (configuration file not found)" asset_result=0 fi # Run config-based snapshot downloader run_with_logging "python3 src/config_snapshot_downloader.py --config $SNAPSHOT_CONFIG_FILE" "Config Snapshot Downloader" config_result=$? # Summary log_message "=== Daily Run Summary ===" if [ "$SKIP_ASSET_DOWNLOADER" = false ]; then if [ $asset_result -eq 0 ]; then log_message "✓ Config Asset Downloader: SUCCESS" else log_message "✗ Config Asset Downloader: FAILED" fi else log_message "- Config Asset Downloader: SKIPPED" fi if [ $config_result -eq 0 ]; then log_message "✓ Snapshot Downloader: SUCCESS" else log_message "✗ Snapshot Downloader: FAILED" fi # Cleanup old log files (keep only last 30 days) find "$LOG_DIR" -name "scheduler_*.log" -mtime +30 -delete 2>/dev/null || true log_message "=== ParentZone Downloaders Daily Run Completed ===" # Exit with error if any downloader failed if [ $asset_result -ne 0 ] || [ $config_result -ne 0 ]; then exit 1 fi exit 0