|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Cron-friendly wrapper around scripts/update.py: |
| 4 | +# 1. Pull any new remote commits (rebase, autostash safety net) |
| 5 | +# 2. Run the Python updater to refresh the proxies/ tree |
| 6 | +# 3. Commit + push only if anything actually changed |
| 7 | +# |
| 8 | +# Designed to be invoked from a crontab — see the "Server cron setup" |
| 9 | +# section in README.md for installation steps. |
| 10 | +# |
| 11 | +# Exits non-zero on any failure so cron's MAILTO surfaces it. |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +# Resolve the repo root from this script's location so the wrapper works no |
| 16 | +# matter where you put the checkout. Override with REPO_DIR=... if you need |
| 17 | +# to point at a different working tree. |
| 18 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 19 | +REPO_DIR="${REPO_DIR:-$(cd "$SCRIPT_DIR/.." && pwd)}" |
| 20 | + |
| 21 | +cd "$REPO_DIR" |
| 22 | + |
| 23 | +# Author for the bot commits. Override at the cron call site if you want a |
| 24 | +# different identity (e.g. a dedicated service account). |
| 25 | +GIT_USER_NAME="${GIT_USER_NAME:-proxyscrape-mirror-bot}" |
| 26 | +GIT_USER_EMAIL="${GIT_USER_EMAIL:-support@proxyscrape.com}" |
| 27 | + |
| 28 | +# Lock to prevent overlapping runs. A long network stall on one tick should |
| 29 | +# never collide with the next. flock returns 1 immediately when the lock is |
| 30 | +# held; that's the correct behavior for a 5-min cron — skip this tick. |
| 31 | +LOCKFILE="${TMPDIR:-/tmp}/free-proxy-list-mirror.lock" |
| 32 | +exec 9>"$LOCKFILE" |
| 33 | +if ! flock -n 9; then |
| 34 | + echo "[$(date -u +'%Y-%m-%dT%H:%M:%SZ')] Another instance is running; skipping tick" >&2 |
| 35 | + exit 0 |
| 36 | +fi |
| 37 | + |
| 38 | +log() { |
| 39 | + echo "[$(date -u +'%Y-%m-%dT%H:%M:%SZ')] $*" |
| 40 | +} |
| 41 | + |
| 42 | +log "Starting update in $REPO_DIR" |
| 43 | + |
| 44 | +# Pull remote updates first — anyone editing the repo (humans, other servers) |
| 45 | +# stays compatible. --autostash protects against any stray local changes. |
| 46 | +git pull --rebase --autostash --quiet origin main |
| 47 | + |
| 48 | +# Run the updater. Failures here propagate (set -e). |
| 49 | +python3 scripts/update.py |
| 50 | + |
| 51 | +# Stage only the data we publish. The script also writes stats.json under |
| 52 | +# proxies/, so a single `git add proxies/` covers everything we care about. |
| 53 | +git add proxies/ |
| 54 | + |
| 55 | +if git diff --cached --quiet; then |
| 56 | + log "No changes — upstream list is unchanged this tick" |
| 57 | + exit 0 |
| 58 | +fi |
| 59 | + |
| 60 | +TS=$(date -u +"%Y-%m-%d %H:%M:%S UTC") |
| 61 | +git -c user.name="$GIT_USER_NAME" -c user.email="$GIT_USER_EMAIL" \ |
| 62 | + commit --quiet -m "chore(data): automatic update — $TS" |
| 63 | + |
| 64 | +# `--quiet` suppresses progress; stderr still surfaces auth/network errors. |
| 65 | +git push --quiet |
| 66 | + |
| 67 | +log "Pushed update" |
0 commit comments