Add lock files and cron setup
This commit is contained in:
parent
d33a3f162f
commit
2b38de598f
5 changed files with 79 additions and 1 deletions
|
@ -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..."
|
||||||
|
|
|
@ -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..."
|
||||||
|
|
|
@ -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..."
|
||||||
|
|
|
@ -161,6 +161,9 @@ 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)
|
||||||
|
if domain:
|
||||||
|
typer.echo(f"Generated {interval} reports for {domain}")
|
||||||
|
else:
|
||||||
typer.echo(f"Generated {interval} reports")
|
typer.echo(f"Generated {interval} reports")
|
||||||
|
|
||||||
|
|
||||||
|
|
46
setup.sh
Executable file
46
setup.sh
Executable 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"
|
Loading…
Add table
Add a link
Reference in a new issue