Catch email lists with 0 subscribers, and send an alert email.

This commit is contained in:
Jonathan Rosenbaum 2026-06-18 02:20:39 -04:00
parent edd58efa44
commit f716963be8

View File

@ -42,6 +42,83 @@ error_exit() {
exit 1 exit 1
} }
# Send aggregated alert email for zero members or zero success
send_zero_member_alert() {
if [ -z "$EMAIL_TO" ] || [ -z "$SMTP_HOST" ]; then
return
fi
local subject="LWVWV Subscription Alert: Issues Detected"
local body="WARNING: The LWVWV subscription process encountered issues.
================================
SUBSCRIPTION SUMMARY
================================
Total Lists Processed: ${#LIST_CONFIGS[@]}
Successful: $SUCCESS_COUNT
Failed: ${#FAILED_LISTS[@]}
Lists with 0 Members: ${#ZERO_MEMBER_LISTS[@]}
================================
LISTS WITH 0 MEMBERS FOUND
================================"
if [ ${#ZERO_MEMBER_LISTS[@]} -gt 0 ]; then
for list_info in "${ZERO_MEMBER_LISTS[@]}"; do
body="$body
- $list_info"
done
else
body="$body
(None)"
fi
if [ ${#FAILED_LISTS[@]} -gt 0 ]; then
body="$body
================================
FAILED LISTS
================================"
for list_id in "${FAILED_LISTS[@]}"; do
body="$body
- $list_id"
done
fi
body="$body
================================
TROUBLESHOOTING
================================
This may indicate:
- CSV format issues (e.g., UTF-8 BOM in column headers)
- Missing data in the portal
- Filter configuration problems
- Connector API issues
Please check:
1. The roster CSV file format
2. Portal data availability
3. League ID filters in configuration
4. Connector service status
================================
Roster File: $ROSTER_FILE
Timestamp: $(date)
================================"
# Send email using s-nail with SMTP configuration
echo "$body" | s-nail -r "$EMAIL_FROM" \
-s "$subject" \
-S smtp="$SMTP_HOST:$SMTP_PORT" \
-S smtp-use-starttls \
-S smtp-auth="$SMTP_AUTH" \
-S smtp-auth-user="$SMTP_USER" \
-S smtp-auth-password="$SMTP_PASSWORD" \
-S ssl-verify="${SSL_VERIFY_IGNORE:-ignore}" \
"$EMAIL_TO" 2>/dev/null || log "Failed to send alert email"
}
# Create work directory # Create work directory
mkdir -p "$WORK_DIR" mkdir -p "$WORK_DIR"
@ -111,6 +188,7 @@ log "=========================================="
# Track success/failure per list # Track success/failure per list
SUCCESS_COUNT=0 SUCCESS_COUNT=0
FAILED_LISTS=() FAILED_LISTS=()
ZERO_MEMBER_LISTS=() # Track lists that had 0 members found
# Helper function to increment counter (avoids set -e issues with arithmetic) # Helper function to increment counter (avoids set -e issues with arithmetic)
increment_count() { increment_count() {
@ -154,6 +232,7 @@ for CONFIG in "${LIST_CONFIGS[@]}"; do
if [ "$MEMBER_COUNT" -eq 0 ]; then if [ "$MEMBER_COUNT" -eq 0 ]; then
log "⚠ No members found for League ID $LEAGUE_ID, skipping $LIST_ID" log "⚠ No members found for League ID $LEAGUE_ID, skipping $LIST_ID"
ZERO_MEMBER_LISTS+=("$LIST_ID (League: $LEAGUE_ID)")
rm -f "$FILTERED_CSV" rm -f "$FILTERED_CSV"
continue continue
fi fi
@ -189,6 +268,11 @@ if [ -z "$1" ] && [ -f "$ROSTER_FILE" ]; then
rm -f "$ROSTER_FILE" rm -f "$ROSTER_FILE"
fi fi
# Check if we need to send alert (0 members found for any list, or 0 successful subscriptions)
if [ ${#ZERO_MEMBER_LISTS[@]} -gt 0 ] || [ $SUCCESS_COUNT -eq 0 ]; then
send_zero_member_alert
fi
# If any lists failed, report error # If any lists failed, report error
if [ ${#FAILED_LISTS[@]} -gt 0 ]; then if [ ${#FAILED_LISTS[@]} -gt 0 ]; then
error_exit "Failed to subscribe to the following list(s): ${FAILED_LISTS[*]}" error_exit "Failed to subscribe to the following list(s): ${FAILED_LISTS[*]}"