From 964e6d1816ef28f559def043f9aad66c4e48a8c4 Mon Sep 17 00:00:00 2001 From: Shane Date: Wed, 14 May 2025 10:29:07 +1000 Subject: [PATCH] update --- manage_scripts.sh | 86 +++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/manage_scripts.sh b/manage_scripts.sh index ba0c748..d5c11e4 100755 --- a/manage_scripts.sh +++ b/manage_scripts.sh @@ -9,8 +9,7 @@ mkdir -p "$LOG_DIR" "$LOCK_DIR" || { echo "Error: Failed to create $LOG_DIR or $ # Handle stop command if [ "$1" == "stop" ]; then log "Received stop command, stopping all scripts..." - stop_scripts # Stop foodie_automator_*.py scripts (includes lock file removal) - # Stop foodie_engagement_generator.py + stop_scripts if pkill -TERM -f "$VENV_PYTHON.*foodie_engagement_generator"; then log "Sent TERM to foodie_engagement_generator.py" sleep 2 @@ -20,7 +19,6 @@ if [ "$1" == "stop" ]; then fi rm -f "$LOCK_DIR/foodie_engagement_generator.lock" || log "Failed to remove foodie_engagement_generator.lock" log "Stopped foodie_engagement_generator.py" - # Stop foodie_weekly_thread.py if pkill -TERM -f "$VENV_PYTHON.*foodie_weekly_thread"; then log "Sent TERM to foodie_weekly_thread.py" sleep 2 @@ -40,13 +38,16 @@ if [ "$1" == "start" ]; then cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; } source "$BASE_DIR/venv/bin/activate" || { log "Failed to activate virtual environment"; exit 1; } if [ -f "$BASE_DIR/.env" ]; then - export $(grep -v '^#' "$BASE_DIR/.env" | xargs) + while IFS='=' read -r key value; do + if [[ ! -z "$key" && ! "$key" =~ ^# ]]; then + export "$key=$value" + fi + done < <(grep -v '^#' "$BASE_DIR/.env") log ".env variables loaded" else log "Error: .env file not found at $BASE_DIR/.env" exit 1 fi - # Start foodie_automator scripts with random 20-30 minute delays for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automator_google.py; do if [ -f "$script" ]; then sleep_time=$(run_script "$script") @@ -58,7 +59,6 @@ if [ "$1" == "start" ]; then log "Script $script not found" fi done - # Start foodie_engagement_generator.py if [ -f "foodie_engagement_generator.py" ]; then if ! check_running "foodie_engagement_generator"; then log "Running foodie_engagement_generator.py..." @@ -68,20 +68,30 @@ if [ "$1" == "start" ]; then else log "Script foodie_engagement_generator.py not found" fi - # Start foodie_weekly_thread.py (commented out to prevent execution) - #if [ -f "foodie_weekly_thread.py" ]; then - # if ! check_running "foodie_weekly_thread"; then - # log "Running foodie_weekly_thread.py..." - # "$VENV_PYTHON" "foodie_weekly_thread.py" >> "$BASE_DIR/logs/foodie_weekly_thread.log" 2>&1 - # log "foodie_weekly_thread.py completed" - # fi - #else - # log "Script foodie_weekly_thread.py not found" - #fi log "All scripts started. Ensure cron jobs are enabled for automatic scheduling (crontab -l)." exit 0 fi +# Handle update command +if [ "$1" == "update" ]; then + if [ -z "$2" ]; then + log "Error: Package name required. Usage: $0 update [version]" + exit 1 + fi + log "Updating package $2..." + if [ -f "$BASE_DIR/update_package.sh" ]; then + "$BASE_DIR/update_package.sh" "$2" "${3:-latest}" || { + log "Error: Failed to update package $2" + exit 1 + } + log "Package $2 updated successfully" + exit 0 + else + log "Error: update_package.sh not found" + exit 1 + fi +fi + # Directory to monitor BASE_DIR="/home/shane/foodie_automator" CHECKSUM_FILE="$BASE_DIR/.file_checksum" @@ -131,9 +141,7 @@ run_script() { return 1 fi log "Running $script..." - # Run script and capture output "$VENV_PYTHON" "$script" >> "$script_log" 2>&1 - # Extract sleep_time from the last log line or generate random 20-30 minutes sleep_time=$(tail -n 1 "$script_log" | grep -oP 'sleep_time: \K[0-9]+' || echo $((RANDOM % 601 + 1200))) log "$script completed, sleep_time: $sleep_time seconds" echo "$sleep_time" @@ -170,15 +178,29 @@ update_dependencies() { fi source "$BASE_DIR/venv/bin/activate" "$VENV_PYTHON" -m pip install --upgrade pip - if [ -f "requirements.txt" ]; then + if [ -f "requirements.lock" ]; then + "$VENV_PYTHON" -m pip install -r requirements.lock || { + log "Failed to install requirements.lock, falling back to requirements.txt" + if [ -f "requirements.txt" ]; then + "$VENV_PYTHON" -m pip install -r requirements.txt || { + log "Failed to install requirements.txt, attempting core dependencies" + "$VENV_PYTHON" -m pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager tweepy python-dotenv flickr-api filelock requests-oauthlib + log "Fallback: Installed core dependencies" + } + else + log "Error: requirements.txt not found, installing core dependencies" + "$VENV_PYTHON" -m pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager tweepy python-dotenv flickr-api filelock requests-oauthlib + fi + } + elif [ -f "requirements.txt" ]; then "$VENV_PYTHON" -m pip install -r requirements.txt || { - log "Failed to install requirements.txt, attempting fallback dependencies" - "$VENV_PYTHON" -m pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager + log "Failed to install requirements.txt, attempting core dependencies" + "$VENV_PYTHON" -m pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager tweepy python-dotenv flickr-api filelock requests-oauthlib log "Fallback: Installed core dependencies" } else - log "Error: requirements.txt not found, installing core dependencies" - "$VENV_PYTHON" -m pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager + log "Error: Neither requirements.lock nor requirements.txt found, installing core dependencies" + "$VENV_PYTHON" -m pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager tweepy python-dotenv flickr-api filelock requests-oauthlib fi log "Dependencies updated." } @@ -186,40 +208,33 @@ update_dependencies() { # Main logic log "Checking for file changes..." CURRENT_CHECKSUM=$(calculate_checksum) - if [ -f "$CHECKSUM_FILE" ]; then PREVIOUS_CHECKSUM=$(cat "$CHECKSUM_FILE") else PREVIOUS_CHECKSUM="" fi - if [ "$CURRENT_CHECKSUM" != "$PREVIOUS_CHECKSUM" ]; then log "File changes detected. Previous checksum: $PREVIOUS_CHECKSUM, Current checksum: $CURRENT_CHECKSUM" - - # Stop scripts if running if pgrep -f "$VENV_PYTHON.*foodie_automator" > /dev/null; then stop_scripts fi - - # Update dependencies update_dependencies - - # Update checksum echo "$CURRENT_CHECKSUM" > "$CHECKSUM_FILE" log "Checksum updated." fi - -# Run scripts sequentially if not running cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; } source "$BASE_DIR/venv/bin/activate" if [ -f "$BASE_DIR/.env" ]; then - export $(grep -v '^#' "$BASE_DIR/.env" | xargs) + while IFS='=' read -r key value; do + if [[ ! -z "$key" && ! "$key" =~ ^# ]]; then + export "$key=$value" + fi + done < <(grep -v '^#' "$BASE_DIR/.env") log ".env variables loaded" else log "Error: .env file not found at $BASE_DIR/.env" exit 1 fi - for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automator_google.py; do if [ -f "$script" ]; then sleep_time=$(run_script "$script") @@ -231,6 +246,5 @@ for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automato log "Script $script not found" fi done - log "All scripts processed." exit 0 \ No newline at end of file