This commit is contained in:
2025-05-12 15:11:41 +10:00
parent e972714ada
commit 8c7049fa4c
5 changed files with 123 additions and 161 deletions
+42 -75
View File
@@ -40,21 +40,21 @@ check_running() {
return 1
}
# Create lock file
create_lock() {
local script_name="$1"
local lock_file="$LOCK_DIR/${script_name}.lock"
mkdir -p "$LOCK_DIR"
echo $$ > "$lock_file"
log "Created lock file for $script_name (PID: $$)"
}
# Remove lock file
remove_lock() {
local script_name="$1"
local lock_file="$LOCK_DIR/${script_name}.lock"
rm -f "$lock_file"
log "Removed lock file for $script_name"
# 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..."
# Run script and capture output
"$VENV_PYTHON" "$script" >> "$script_log" 2>&1
# Extract sleep_time from the last log line
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
@@ -71,74 +71,22 @@ stop_scripts() {
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
remove_lock "$script_name"
rm -f "$LOCK_DIR/${script_name}.lock"
log "Removed lock file for $script_name"
fi
done
log "Scripts stopped."
}
# Start scripts
start_scripts() {
log "Starting scripts..."
cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; }
# Source virtual environment
if [ -f "$BASE_DIR/venv/bin/activate" ]; then
source "$BASE_DIR/venv/bin/activate"
else
log "Error: Virtual environment not found at $BASE_DIR/venv"
exit 1
fi
# Load .env variables
if [ -f "$BASE_DIR/.env" ]; then
export $(grep -v '^#' "$BASE_DIR/.env" | xargs)
log ".env variables loaded"
else
log "Error: .env file not found at $BASE_DIR/.env"
exit 1
fi
# Find and start all foodie_automator_*.py scripts (excluding weekly/engagement)
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 ! check_running "$script_name"; then
log "Starting $script..."
create_lock "$script_name"
nohup "$VENV_PYTHON" "$script" >> "$BASE_DIR/logs/${script_name}.log" 2>&1 &
if [ $? -eq 0 ]; then
log "$script started successfully"
else
log "Failed to start $script"
remove_lock "$script_name"
fi
fi
fi
done
log "All scripts started."
}
# Update dependencies
update_dependencies() {
log "Updating dependencies..."
cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; }
# Create venv if it doesn't exist
if [ ! -d "venv" ]; then
python3 -m venv venv
log "Created new virtual environment"
fi
# Source virtual environment
if [ -f "$BASE_DIR/venv/bin/activate" ]; then
source "$BASE_DIR/venv/bin/activate"
else
log "Error: Virtual environment not found at $BASE_DIR/venv"
exit 1
fi
# Update pip and install requirements
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 || {
@@ -174,14 +122,33 @@ if [ "$CURRENT_CHECKSUM" != "$PREVIOUS_CHECKSUM" ]; then
# Update dependencies
update_dependencies
# Start scripts
start_scripts
# Update checksum
echo "$CURRENT_CHECKSUM" > "$CHECKSUM_FILE"
log "Checksum updated."
else
log "No file changes detected."
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)
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")
if [ -n "$sleep_time" ]; then
log "Sleeping for $sleep_time seconds after $script"
sleep "$sleep_time"
fi
else
log "Script $script not found"
fi
done
log "All scripts processed."
exit 0