Files
AdventureLog/backend/scripts/run-cron-job.sh
Sean Morley 7f6bf1390a Add Docker and environment configuration files for AdventureLog
- Introduced .dockerignore to exclude unnecessary files from Docker context.
- Added .env.aio.example for minimal configuration of the AdventureLog All-in-One setup.
- Updated .env.example to include optional SITE_URL and GUNICORN_WORKERS settings.
- Enhanced deploy.sh script for improved deployment flexibility and backup options.
- Updated docker-compose files to use PostGIS 16-3.5 and added health checks for services.
- Created docker-compose.aio.yml for All-in-One deployment configuration.
- Improved health checks and service dependencies in docker-compose.dev.yml and docker-compose.yml.
- Added GitHub workflows for building and pushing Docker images, including smoke tests for AIO setup.
2026-06-06 18:54:25 -04:00

55 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Run a Django management command as a cron job with logging and overlap protection.
#
# Usage:
# run-cron-job.sh <job-name> <manage.py-command> [command-args...]
#
# Output goes to /var/log/adventurelog/<job-name>.log and Docker logs (via PID 1 stdout).
set -euo pipefail
JOB_NAME="${1:?job name required}"
shift
cd /code
LOCK_FILE="/var/lock/adventurelog-${JOB_NAME}.lock"
LOG_DIR="/var/log/adventurelog"
LOG_FILE="${LOG_DIR}/${JOB_NAME}.log"
mkdir -p "$LOG_DIR"
log() {
printf '[cron:%s] %s\n' "$JOB_NAME" "$*" >>/proc/1/fd/1 2>/dev/null || \
printf '[cron:%s] %s\n' "$JOB_NAME" "$*"
}
# Cron runs with a minimal environment; load the snapshot written at container start.
if [[ -f /etc/adventurelog/cron.env ]]; then
set -a
# shellcheck disable=SC1091
source /etc/adventurelog/cron.env
set +a
else
log "WARNING: /etc/adventurelog/cron.env missing — Django may fail to connect to the database"
fi
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log "Skipped — previous run still in progress"
exit 0
fi
log "Starting manage.py $*"
set +e
/usr/local/bin/python3 manage.py "$@" 2>&1 | tee -a "$LOG_FILE" >>/proc/1/fd/1
exit_code=${PIPESTATUS[0]}
set -e
if [[ "$exit_code" -ne 0 ]]; then
log "Failed with exit code ${exit_code}. See ${LOG_FILE}"
exit "$exit_code"
fi
log "Finished manage.py $*"