Add lock files and cron setup #41

Merged
wagesj45 merged 1 commit from codex/add-logging,-locking-and-cron-setup-scripts into main 2025-07-19 03:35:31 -05:00
5 changed files with 79 additions and 1 deletions
Showing only changes of commit 2b38de598f - Show all commits

Add lock files and cron setup

Jordan Wages 2025-07-19 03:35:18 -05:00

View file

@ -1,6 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
# Prevent concurrent executions of this script.
LOCK_FILE="/tmp/$(basename "$0").lock"
if [ -e "$LOCK_FILE" ]; then
echo "[WARN] $(basename "$0") is already running (lock file present)." >&2
exit 0
fi
touch "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
# Ensure virtual environment exists # Ensure virtual environment exists
if [ ! -d ".venv" ]; then if [ ! -d ".venv" ]; then
echo "[INFO] Creating virtual environment..." echo "[INFO] Creating virtual environment..."

View file

@ -1,6 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
# Prevent multiple simultaneous runs by using a lock file specific to this
# script. If the lock already exists, assume another instance is running and
# exit gracefully.
LOCK_FILE="/tmp/$(basename "$0").lock"
if [ -e "$LOCK_FILE" ]; then
echo "[WARN] $(basename "$0") is already running (lock file present)." >&2
exit 0
fi
touch "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
# Ensure virtual environment exists # Ensure virtual environment exists
if [ ! -d ".venv" ]; then if [ ! -d ".venv" ]; then
echo "[INFO] Creating virtual environment..." echo "[INFO] Creating virtual environment..."

View file

@ -1,6 +1,15 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
# Prevent concurrent executions of this script.
LOCK_FILE="/tmp/$(basename "$0").lock"
if [ -e "$LOCK_FILE" ]; then
echo "[WARN] $(basename "$0") is already running (lock file present)." >&2
exit 0
fi
touch "$LOCK_FILE"
trap 'rm -f "$LOCK_FILE"' EXIT
# Ensure virtual environment exists # Ensure virtual environment exists
if [ ! -d ".venv" ]; then if [ ! -d ".venv" ]; then
echo "[INFO] Creating virtual environment..." echo "[INFO] Creating virtual environment..."

View file

@ -161,7 +161,10 @@ def _generate_interval(interval: str, domain: Optional[str] = None) -> None:
report_list.append(entry) report_list.append(entry)
_save_json(out_dir / "reports.json", report_list) _save_json(out_dir / "reports.json", report_list)
typer.echo(f"Generated {interval} reports") if domain:
typer.echo(f"Generated {interval} reports for {domain}")
else:
typer.echo(f"Generated {interval} reports")
def _generate_all_domains(interval: str) -> None: def _generate_all_domains(interval: str) -> None:

46
setup.sh Executable file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env bash
set -e
# Default schedules
import_sched="*/5 * * * *"
report_sched="0 * * * *"
analysis_sched="0 0 * * *"
remove=false
usage() {
echo "Usage: $0 [--import CRON] [--reports CRON] [--analysis CRON] [--remove]"
}
while [ $# -gt 0 ]; do
case "$1" in
--import)
import_sched="$2"; shift 2;;
--reports)
report_sched="$2"; shift 2;;
--analysis)
analysis_sched="$2"; shift 2;;
--remove)
remove=true; shift;;
-h|--help)
usage; exit 0;;
*)
usage; exit 1;;
esac
done
repo_dir="$(cd "$(dirname "$0")" && pwd)"
if [ "$remove" = true ]; then
tmp=$(mktemp)
sudo crontab -l 2>/dev/null | grep -v "# ngxstat import" | grep -v "# ngxstat reports" | grep -v "# ngxstat analysis" > "$tmp" || true
sudo crontab "$tmp"
rm -f "$tmp"
echo "[INFO] Removed ngxstat cron entries"
exit 0
fi
cron_entries="${import_sched} cd ${repo_dir} && ./run-import.sh # ngxstat import\n${report_sched} cd ${repo_dir} && ./run-reports.sh # ngxstat reports\n${analysis_sched} cd ${repo_dir} && ./run-analysis.sh # ngxstat analysis"
( sudo crontab -l 2>/dev/null; echo -e "$cron_entries" ) | sudo crontab -
echo "[INFO] Installed ngxstat cron entries"