#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib.sh
source "${SCRIPT_DIR}/lib.sh"

echo "[head] ray status"
dexec "${HEAD_CONTAINER}" bash -lc "ray status || true"

echo "[head] ray jobs list (optional)"
dexec "${HEAD_CONTAINER}" bash -lc "ray job list --address=http://127.0.0.1:8265 || true"

LATEST_JOB_DIR="$(dexec "${HEAD_CONTAINER}" bash -lc "ls -1dt /mnt/shared/jobs/* 2>/dev/null | head -n 1 || true")"
if [[ -n "${LATEST_JOB_DIR}" ]]; then
  echo "[host] latest job dir: ${LATEST_JOB_DIR}"
  echo "[host] ray submission id (if exists):"
  SUB_ID="$(dexec "${HEAD_CONTAINER}" bash -lc "cat ${LATEST_JOB_DIR}/config/ray_submission_id.txt 2>/dev/null || true")"
  echo "${SUB_ID}"
  if [[ -n "${SUB_ID}" ]]; then
    echo "[head] ray job status:"
    dexec "${HEAD_CONTAINER}" bash -lc "ray job status --address=http://127.0.0.1:8265 ${SUB_ID} || true"
    echo "[head] ray job logs (tail):"
    dexec "${HEAD_CONTAINER}" bash -lc "ray job logs --address=http://127.0.0.1:8265 ${SUB_ID} 2>/dev/null | tail -n 60 || true"
  fi
fi
