{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ray\n",
    "from ray.job_submission import JobSubmissionClient\n",
    "import time\n",
    "from ray.runtime_env import RuntimeEnv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ray cluster information\n",
    "ray_head_ip = \"kuberay-head-svc.kuberay.svc.cluster.local\"\n",
    "ray_head_port = 8265\n",
    "ray_address = f\"http://{ray_head_ip}:{ray_head_port}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Submit Ray job using JobSubmissionClient\n",
    "client = JobSubmissionClient(ray_address)\n",
    "job_id = client.submit_job(\n",
    "    entrypoint=\"python mandlebrot.py\",\n",
    "    runtime_env={\n",
    "        \"working_dir\": \"./\",\n",
    "        \"pip\": [\"piexif\",\"ipyplot\",\"whylogs[image,whylabs]\", \"matplotlib==3.9.0\"],\n",
    "        \"env_vars\": {\"http_proxy\":\"<ADD-http-proxy>\",\"https_proxy\":\"<ADD-https-proxy>\"}\n",
    "    },\n",
    "    entrypoint_num_cpus=3\n",
    ")\n",
    "\n",
    "print(f\"Ray job submitted with job_id: {job_id}\")\n",
    "\n",
    "# Waiting for Ray to finish the job and print the result\n",
    "while True:\n",
    "    status = client.get_job_status(job_id)\n",
    "    if status in [ray.job_submission.JobStatus.RUNNING, ray.job_submission.JobStatus.PENDING]:\n",
    "        time.sleep(5)\n",
    "    else:\n",
    "        break\n",
    "try:\n",
    "    logs = client.get_job_logs(job_id) \n",
    "    print(logs)\n",
    "except RuntimeError as e:\n",
    "    print(f\"Failed to get job logs, please check logs on ray dashboard \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
