prod2u-remote-job.sh 1.0 KB

123456789101112131415161718192021222324
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Single detached remote job runner. It records a minimal, atomic status.
  4. run_root="${PROD2U_RUN_ROOT:-/data/prod2u/runs}"
  5. job_id="${1:-$(date -u +%Y%m%dT%H%M%SZ)-$$}"
  6. shift || true
  7. job_dir="$run_root/$job_id"
  8. mkdir -p "$job_dir"
  9. printf '{"schema":"prod2u-remote-job-v1","run_id":"%s","status":"RUNNING"}\n' "$job_id" >"$job_dir/RUN-STATUS.json.tmp"
  10. mv "$job_dir/RUN-STATUS.json.tmp" "$job_dir/RUN-STATUS.json"
  11. if (($# == 0)); then
  12. printf '{"schema":"prod2u-remote-job-v1","run_id":"%s","status":"PASS","exit_code":0}\n' "$job_id" >"$job_dir/RESULT.json.tmp"
  13. mv "$job_dir/RESULT.json.tmp" "$job_dir/RESULT.json"
  14. exit 0
  15. fi
  16. set +e
  17. "$@" >"$job_dir/stdout.log" 2>"$job_dir/stderr.log"
  18. rc=$?
  19. set -e
  20. status=PASS; if ((rc != 0)); then status=FAIL; fi
  21. printf '{"schema":"prod2u-remote-job-v1","run_id":"%s","status":"%s","exit_code":%d}\n' "$job_id" "$status" "$rc" >"$job_dir/RESULT.json.tmp"
  22. mv "$job_dir/RESULT.json.tmp" "$job_dir/RESULT.json"
  23. printf '%d\n' "$rc" >"$job_dir/exit-code"
  24. exit "$rc"