#!/usr/bin/env bash
set -Eeuo pipefail
CALLBACK_URL='{{CallbackUrl}}'
JOB_ID='{{JobId}}'
callback() {
  local status="$1" code="$2" message="$3" attempt
  for attempt in 1 2 3; do
    if curl -fsS --max-time 60 -X POST -H 'Content-Type: application/json' --data "{\"status\":\"${status}\",\"exit_code\":${code},\"completed\":true,\"message\":\"${message}\"}" "$CALLBACK_URL" >/dev/null; then
      return 0
    fi
    echo "Callback attempt ${attempt} of 3 failed" >&2
    if [ "$attempt" -lt 3 ]; then sleep $((attempt * 2)); fi
  done
  return 1
}
trap 'code=$?; callback failed "$code" "Job failed" || true; exit "$code"' ERR
echo "$(date --iso-8601=seconds) job=${JOB_ID} started"
# --- Insert custom commands here ---

# --- End custom commands ---
callback success 0 'Job completed'
