Files
ljwx-k8s/cache-morning-images.sh
2026-02-20 22:50:45 +08:00

205 lines
5.9 KiB
Bash
Executable File

#!/bin/bash
# Batch Image Caching Script for Morning Window
# This script caches commonly used images during the morning when EU Harbor connection is fastest
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Logging functions
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[!]${NC} $1"; }
log_step() { echo -e "${CYAN}[→]${NC} $1"; }
# Configuration
IMAGES_FILE="${IMAGES_FILE:-common-images.txt}"
CACHE_SCRIPT="./cache-image.sh"
LOG_FILE="cache-morning-images-$(date +%Y%m%d-%H%M%S).log"
# Check if cache script exists
if [[ ! -f "$CACHE_SCRIPT" ]]; then
log_error "Cache script not found: $CACHE_SCRIPT"
exit 1
fi
# Check if images file exists
if [[ ! -f "$IMAGES_FILE" ]]; then
log_error "Images file not found: $IMAGES_FILE"
exit 1
fi
# Function to display usage
show_usage() {
cat << EOF
Morning Image Caching Script
This script caches commonly used images during the morning when EU Harbor connection is fastest.
Usage:
$0 [OPTIONS]
Options:
-f, --file FILE Images file to use (default: common-images.txt)
-s, --script FILE Cache script to use (default: ./cache-image.sh)
--dry-run Show what would be cached without actually caching
-h, --help Show this help message
Examples:
# Cache images from default file
$0
# Cache images from custom file
$0 -f my-images.txt
# Dry run to see what would be cached
$0 --dry-run
EOF
}
# Parse command line arguments
DRY_RUN=false
while [[ $# -gt 0 ]]; do
case $1 in
-f|--file)
IMAGES_FILE="$2"
shift 2
;;
-s|--script)
CACHE_SCRIPT="$2"
shift 2
;;
--dry-run)
DRY_RUN=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
log_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Main function
main() {
log_info "=== Morning Image Caching Script ==="
log_info "Images file: $IMAGES_FILE"
log_info "Cache script: $CACHE_SCRIPT"
log_info "Log file: $LOG_FILE"
log_info "Dry run mode: $DRY_RUN"
echo ""
# Check if we're in the morning (6AM-10AM) for optimal EU connection
local current_hour=$(date +%H)
local hour_int=${current_hour#0} # Remove leading zero
if [[ $hour_int -ge 6 && $hour_int -lt 10 ]]; then
log_success "✅ Running during optimal morning window ($current_hour:00-$((current_hour+1)):00)"
else
log_warning "⚠️ Not in optimal morning window (6AM-10AM), but proceeding anyway"
fi
# Read images from file, skipping comments and empty lines
local images=()
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip empty lines and comments
[[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
images+=("$line")
done < "$IMAGES_FILE"
log_info "Found ${#images[@]} images to cache"
echo ""
# Show images that will be cached in dry run mode
if [[ "$DRY_RUN" == "true" ]]; then
log_warning "[DRY RUN] The following images would be cached:"
for img in "${images[@]}"; do
echo " - $img"
done
log_info "[DRY RUN] Actual caching skipped."
return 0
fi
# Start logging to file
exec > >(tee -a "$LOG_FILE")
exec 2>&1
log_info "Starting batch caching process..."
echo ""
# Cache each image
local success_count=0
local failed_count=0
local failed_images=()
for i in "${!images[@]}"; do
local image="${images[$i]}"
local current=$((i + 1))
echo ""
log_info "Progress: [$current/${#images[@]}] Processing: $image"
echo "───────────────────────────────────────────────"
if "$CACHE_SCRIPT" "$image"; then
((success_count++))
log_success "✅ Successfully cached: $image"
else
((failed_count++))
failed_images+=("$image")
log_error "❌ Failed to cache: $image"
fi
echo "───────────────────────────────────────────────"
done
# Display summary
echo ""
echo "═══════════════════════════════════════════════"
log_info "Batch Caching Summary"
echo "═══════════════════════════════════════════════"
echo " Total images: ${#images[@]}"
echo " Successful: $success_count"
echo " Failed: $failed_count"
echo " Success rate: $(( success_count * 100 / ${#images[@]} ))%"
if [[ $failed_count -gt 0 ]]; then
echo ""
log_error "Failed images:"
for img in "${failed_images[@]}"; do
echo " ✗ $img"
done
fi
echo "═══════════════════════════════════════════════"
echo ""
# Log summary to separate summary file
local summary_file="cache-summary-$(date +%Y%m%d).txt"
{
echo "Cache Summary - $(date)"
echo "Total: ${#images[@]}, Success: $success_count, Failed: $failed_count"
echo "Success Rate: $(( success_count * 100 / ${#images[@]} ))%"
if [[ $failed_count -gt 0 ]]; then
echo "Failed Images:"
printf ' - %s\n' "${failed_images[@]}"
fi
echo ""
} >> "$summary_file"
log_success "✅ Batch caching completed! Summary saved to $summary_file"
}
# Run main function
main "$@"