update
This commit is contained in:
+110
-133
@@ -6,6 +6,114 @@ LOG_DIR="$BASE_DIR/logs"
|
|||||||
LOCK_DIR="$BASE_DIR/locks"
|
LOCK_DIR="$BASE_DIR/locks"
|
||||||
mkdir -p "$LOG_DIR" "$LOCK_DIR" || { echo "Error: Failed to create $LOG_DIR or $LOCK_DIR"; exit 1; }
|
mkdir -p "$LOG_DIR" "$LOCK_DIR" || { echo "Error: Failed to create $LOG_DIR or $LOCK_DIR"; exit 1; }
|
||||||
|
|
||||||
|
# Log function
|
||||||
|
log() {
|
||||||
|
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
|
||||||
|
echo "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Directory to monitor
|
||||||
|
CHECKSUM_FILE="$BASE_DIR/.file_checksum"
|
||||||
|
LOG_FILE="$BASE_DIR/logs/manage_scripts.log"
|
||||||
|
VENV_PYTHON="$BASE_DIR/venv/bin/python"
|
||||||
|
LOCK_DIR="$BASE_DIR/locks"
|
||||||
|
|
||||||
|
# Calculate checksum of files (excluding logs, JSON files, and venv)
|
||||||
|
calculate_checksum() {
|
||||||
|
find "$BASE_DIR" -type f \
|
||||||
|
-not -path "$BASE_DIR/logs/*" \
|
||||||
|
-not -path "$BASE_DIR/*.json" \
|
||||||
|
-not -path "$BASE_DIR/.file_checksum" \
|
||||||
|
-not -path "$BASE_DIR/venv/*" \
|
||||||
|
-not -path "$BASE_DIR/locks/*" \
|
||||||
|
-exec sha256sum {} \; | sort | sha256sum | awk '{print $1}'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if a script is running (using lock file)
|
||||||
|
check_running() {
|
||||||
|
local script_name="$1"
|
||||||
|
local lock_file="$LOCK_DIR/${script_name}.lock"
|
||||||
|
if [ -f "$lock_file" ]; then
|
||||||
|
local pid=$(cat "$lock_file")
|
||||||
|
if ps -p "$pid" > /dev/null; then
|
||||||
|
log "$script_name is already running (PID: $pid)"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log "Stale lock file found for $script_name, removing"
|
||||||
|
rm -f "$lock_file"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Run a script and extract sleep_time
|
||||||
|
run_script() {
|
||||||
|
local script="$1"
|
||||||
|
local script_name="${script%.py}"
|
||||||
|
local script_log="$BASE_DIR/logs/${script_name}.log"
|
||||||
|
if check_running "$script_name"; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
log "Running $script..."
|
||||||
|
"$VENV_PYTHON" "$script" >> "$script_log" 2>&1 &
|
||||||
|
local pid=$!
|
||||||
|
echo "$pid" > "$LOCK_DIR/${script_name}.lock"
|
||||||
|
wait "$pid"
|
||||||
|
local exit_code=$?
|
||||||
|
if [ $exit_code -eq 0 ]; then
|
||||||
|
log "$script completed successfully"
|
||||||
|
else
|
||||||
|
log "$script failed with exit code $exit_code"
|
||||||
|
fi
|
||||||
|
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"
|
||||||
|
rm -f "$LOCK_DIR/${script_name}.lock"
|
||||||
|
echo "$sleep_time"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Stop scripts
|
||||||
|
stop_scripts() {
|
||||||
|
log "Stopping scripts..."
|
||||||
|
for script in foodie_automator_*.py; do
|
||||||
|
if [ -f "$script" ] && [ "$script" != "foodie_weekly_thread.py" ] && [ "$script" != "foodie_engagement_tweet.py" ]; then
|
||||||
|
local script_name="${script%.py}"
|
||||||
|
if pkill -TERM -f "$VENV_PYTHON.*$script_name"; then
|
||||||
|
log "Sent TERM to $script_name"
|
||||||
|
sleep 2
|
||||||
|
pkill -9 -f "$VENV_PYTHON.*$script_name" || true
|
||||||
|
else
|
||||||
|
log "No running $script_name found"
|
||||||
|
fi
|
||||||
|
rm -f "$LOCK_DIR/${script_name}.lock"
|
||||||
|
log "Removed lock file for $script_name"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
log "Scripts stopped."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Update dependencies
|
||||||
|
update_dependencies() {
|
||||||
|
log "Updating dependencies..."
|
||||||
|
cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; }
|
||||||
|
if [ ! -d "venv" ]; then
|
||||||
|
python3 -m venv venv
|
||||||
|
log "Created new virtual environment"
|
||||||
|
fi
|
||||||
|
source "$BASE_DIR/venv/bin/activate"
|
||||||
|
"$VENV_PYTHON" -m pip install --upgrade pip
|
||||||
|
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 psutil
|
||||||
|
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 psutil
|
||||||
|
fi
|
||||||
|
log "Dependencies updated."
|
||||||
|
}
|
||||||
|
|
||||||
# Handle stop command
|
# Handle stop command
|
||||||
if [ "$1" == "stop" ]; then
|
if [ "$1" == "stop" ]; then
|
||||||
log "Received stop command, stopping all scripts..."
|
log "Received stop command, stopping all scripts..."
|
||||||
@@ -57,6 +165,7 @@ if [ "$1" == "start" ]; then
|
|||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
log "Script $script not found"
|
log "Script $script not found"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
if [ -f "foodie_engagement_generator.py" ]; then
|
if [ -f "foodie_engagement_generator.py" ]; then
|
||||||
@@ -72,139 +181,6 @@ if [ "$1" == "start" ]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Handle update command
|
|
||||||
if [ "$1" == "update" ]; then
|
|
||||||
if [ -z "$2" ]; then
|
|
||||||
log "Error: Package name required. Usage: $0 update <package_name> [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"
|
|
||||||
LOG_FILE="$BASE_DIR/logs/manage_scripts.log"
|
|
||||||
VENV_PYTHON="$BASE_DIR/venv/bin/python"
|
|
||||||
LOCK_DIR="$BASE_DIR/locks"
|
|
||||||
|
|
||||||
# Log function
|
|
||||||
log() {
|
|
||||||
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Calculate checksum of files (excluding logs, JSON files, and venv)
|
|
||||||
calculate_checksum() {
|
|
||||||
find "$BASE_DIR" -type f \
|
|
||||||
-not -path "$BASE_DIR/logs/*" \
|
|
||||||
-not -path "$BASE_DIR/*.json" \
|
|
||||||
-not -path "$BASE_DIR/.file_checksum" \
|
|
||||||
-not -path "$BASE_DIR/venv/*" \
|
|
||||||
-not -path "$BASE_DIR/locks/*" \
|
|
||||||
-exec sha256sum {} \; | sort | sha256sum | awk '{print $1}'
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if a script is running (using lock file)
|
|
||||||
check_running() {
|
|
||||||
local script_name="$1"
|
|
||||||
local lock_file="$LOCK_DIR/${script_name}.lock"
|
|
||||||
if [ -f "$lock_file" ]; then
|
|
||||||
local pid=$(cat "$lock_file")
|
|
||||||
if ps -p "$pid" > /dev/null; then
|
|
||||||
log "$script_name is already running (PID: $pid)"
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
log "Stale lock file found for $script_name, removing"
|
|
||||||
rm -f "$lock_file"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Run a script and extract sleep_time
|
|
||||||
run_script() {
|
|
||||||
local script="$1"
|
|
||||||
local script_name="${script%.py}"
|
|
||||||
local script_log="$BASE_DIR/logs/${script_name}.log"
|
|
||||||
if check_running "$script_name"; then
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
log "Running $script..."
|
|
||||||
"$VENV_PYTHON" "$script" >> "$script_log" 2>&1
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Stop scripts
|
|
||||||
stop_scripts() {
|
|
||||||
log "Stopping scripts..."
|
|
||||||
for script in foodie_automator_*.py; do
|
|
||||||
if [ -f "$script" ] && [ "$script" != "foodie_weekly_thread.py" ] && [ "$script" != "foodie_engagement_tweet.py" ]; then
|
|
||||||
local script_name="${script%.py}"
|
|
||||||
pkill -TERM -f "$VENV_PYTHON.*$script_name" || true
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
sleep 10
|
|
||||||
for script in foodie_automator_*.py; do
|
|
||||||
if [ -f "$script" ] && [ "$script" != "foodie_weekly_thread.py" ] && [ "$script" != "foodie_engagement_tweet.py" ]; then
|
|
||||||
local script_name="${script%.py}"
|
|
||||||
pkill -9 -f "$VENV_PYTHON.*$script_name" || true
|
|
||||||
rm -f "$LOCK_DIR/${script_name}.lock"
|
|
||||||
log "Removed lock file for $script_name"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
log "Scripts stopped."
|
|
||||||
}
|
|
||||||
|
|
||||||
# Update dependencies
|
|
||||||
update_dependencies() {
|
|
||||||
log "Updating dependencies..."
|
|
||||||
cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; }
|
|
||||||
if [ ! -d "venv" ]; then
|
|
||||||
python3 -m venv venv
|
|
||||||
log "Created new virtual environment"
|
|
||||||
fi
|
|
||||||
source "$BASE_DIR/venv/bin/activate"
|
|
||||||
"$VENV_PYTHON" -m pip install --upgrade pip
|
|
||||||
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 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: 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."
|
|
||||||
}
|
|
||||||
|
|
||||||
# Main logic
|
# Main logic
|
||||||
log "Checking for file changes..."
|
log "Checking for file changes..."
|
||||||
CURRENT_CHECKSUM=$(calculate_checksum)
|
CURRENT_CHECKSUM=$(calculate_checksum)
|
||||||
@@ -244,6 +220,7 @@ for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automato
|
|||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
log "Script $script not found"
|
log "Script $script not found"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
log "All scripts processed."
|
log "All scripts processed."
|
||||||
|
|||||||
+2
-1
@@ -12,4 +12,5 @@ tweepy==4.15.0
|
|||||||
python-dotenv==1.1.0
|
python-dotenv==1.1.0
|
||||||
flickr-api==0.7.7
|
flickr-api==0.7.7
|
||||||
filelock==3.16.1
|
filelock==3.16.1
|
||||||
requests-oauthlib==2.0.0
|
requests-oauthlib==2.0.0
|
||||||
|
psutil==7.0.0
|
||||||
Reference in New Issue
Block a user