84 lines
3.0 KiB
Bash
Executable File
84 lines
3.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# sms_timer_manager.sh — reconciles DB flags with systemd timers
|
|
# Runs every 1 min via cron:
|
|
# 1. timer_active → enable/disable systemd timer
|
|
# 2. run_requested → run campaign script immediately (script resets flag itself)
|
|
|
|
set -euo pipefail
|
|
|
|
DB_HOST="10.0.4.12"
|
|
DB_PORT="5432"
|
|
DB_NAME="postgres"
|
|
DB_USER="supabase_admin"
|
|
DB_PASS="4fe80bb21c7c3d17a8d8b226adf7a479"
|
|
|
|
declare -A TIMERS=(
|
|
["first_sms"]="sms-first-campaign.timer"
|
|
["second_sms"]="sms-second-campaign.timer"
|
|
["manual"]="sms-manual-campaign.timer"
|
|
["paid_storage"]="sms-paid-storage-campaign.timer"
|
|
)
|
|
declare -A SERVICES=(
|
|
["first_sms"]="sms-first-campaign.service"
|
|
["second_sms"]="sms-second-campaign.service"
|
|
["manual"]="sms-manual-campaign.service"
|
|
["paid_storage"]="sms-paid-storage-campaign.service"
|
|
)
|
|
declare -A SCRIPTS=(
|
|
["first_sms"]="/opt/supersam/scripts/sms_first_campaign.py"
|
|
["second_sms"]="/opt/supersam/scripts/sms_second_campaign.py"
|
|
["manual"]="/opt/supersam/scripts/sms_manual_campaign.py"
|
|
["paid_storage"]="/opt/supersam/scripts/sms_paid_storage_campaign.py"
|
|
)
|
|
|
|
QUERY="SELECT campaign_type, timer_active, run_requested FROM sms_campaign_settings"
|
|
RESULTS=$(PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -t -A -F '|' -c "$QUERY" 2>/dev/null || echo "")
|
|
|
|
if [ -z "$RESULTS" ]; then
|
|
echo "$(date): Failed to query DB, skipping"
|
|
exit 0
|
|
fi
|
|
|
|
while IFS='|' read -r campaign_type timer_active run_requested; do
|
|
[ -z "$campaign_type" ] && continue
|
|
timer_name="${TIMERS[$campaign_type]:-}"
|
|
service_name="${SERVICES[$campaign_type]:-}"
|
|
script_path="${SCRIPTS[$campaign_type]:-}"
|
|
[ -z "$timer_name" ] && continue
|
|
|
|
if ! systemctl list-unit-files "$timer_name" 2>/dev/null | grep -q "$timer_name"; then
|
|
continue
|
|
fi
|
|
|
|
# ── 1. Timer enable/disable ──
|
|
is_active=$(systemctl is-active "$timer_name" 2>/dev/null || echo "inactive")
|
|
|
|
if [ "$timer_active" = "t" ] || [ "$timer_active" = "true" ]; then
|
|
if [ "$is_active" != "active" ]; then
|
|
systemctl enable --now "$timer_name" 2>/dev/null
|
|
echo "$(date): ENABLED $timer_name for $campaign_type"
|
|
fi
|
|
else
|
|
if [ "$is_active" = "active" ]; then
|
|
systemctl disable --now "$timer_name" 2>/dev/null
|
|
echo "$(date): DISABLED $timer_name for $campaign_type"
|
|
fi
|
|
fi
|
|
|
|
# ── 2. Run requested → immediate execution ──
|
|
# Don't reset flag here — script reads it from DB and resets after processing
|
|
if [ "$run_requested" = "t" ] || [ "$run_requested" = "true" ]; then
|
|
is_running=$(systemctl is-active "$service_name" 2>/dev/null || echo "inactive")
|
|
if [ "$is_running" = "active" ]; then
|
|
echo "$(date): $campaign_type already running, skipping run_requested"
|
|
continue
|
|
fi
|
|
|
|
echo "$(date): RUN REQUESTED — starting $campaign_type immediately"
|
|
systemctl start "$service_name" 2>/dev/null || {
|
|
nohup python3 "$script_path" >> "/var/log/supersam-sms-${campaign_type}.log" 2>&1 &
|
|
echo "$(date): Started $campaign_type via nohup fallback"
|
|
}
|
|
fi
|
|
|
|
done <<< "$RESULTS" |