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

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

HEAD_IP="$(container_ip "${HEAD_CONTAINER}")"
HEAD_ADDR="${HEAD_IP}:6379"

start_one() {
  local worker="$1"
  local ip
  ip="$(container_ip "${worker}")"
  echo "[${worker}] ray stop (best-effort)"
  dexec "${worker}" bash -lc "ray stop --force || true"
  echo "[${worker}] start ray worker -> head ${HEAD_ADDR}"
  dexec "${worker}" bash -lc "ray start --address='${HEAD_ADDR}' --node-ip-address='${ip}' --resources='{\"worker_node\": 100}' --disable-usage-stats"
}

start_one "${WORKER0_CONTAINER}"
start_one "${WORKER1_CONTAINER}"

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

