|
| 1 | +# Copyright 2022 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +The ray_jobs sub-module contains methods needed to submit jobs and connect to Ray Clusters that were not created by CodeFlare. |
| 17 | +The SDK acts as a wrapper for the Ray Job Submission Client. |
| 18 | +""" |
| 19 | +from ray.job_submission import JobSubmissionClient |
| 20 | +from typing import Iterator, Optional, Dict, Any, Union |
| 21 | + |
| 22 | + |
| 23 | +class RayJobClient: |
| 24 | + """ |
| 25 | + An object for that acts as the Ray Job Submission Client. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__( |
| 29 | + self, |
| 30 | + address: Optional[str] = None, |
| 31 | + create_cluster_if_needed: bool = False, |
| 32 | + cookies: Optional[Dict[str, Any]] = None, |
| 33 | + metadata: Optional[Dict[str, Any]] = None, |
| 34 | + headers: Optional[Dict[str, Any]] = None, |
| 35 | + verify: Optional[Union[str, bool]] = True, |
| 36 | + ): |
| 37 | + self.rayJobClient = JobSubmissionClient( |
| 38 | + address=address, |
| 39 | + create_cluster_if_needed=create_cluster_if_needed, |
| 40 | + cookies=cookies, |
| 41 | + metadata=metadata, |
| 42 | + headers=headers, |
| 43 | + verify=verify, |
| 44 | + ) |
| 45 | + |
| 46 | + def submit_job( |
| 47 | + self, |
| 48 | + entrypoint: str, |
| 49 | + job_id: Optional[str] = None, |
| 50 | + runtime_env: Optional[Dict[str, Any]] = None, |
| 51 | + metadata: Optional[Dict[str, str]] = None, |
| 52 | + submission_id: Optional[str] = None, |
| 53 | + entrypoint_num_cpus: Optional[Union[int, float]] = None, |
| 54 | + entrypoint_num_gpus: Optional[Union[int, float]] = None, |
| 55 | + entrypoint_resources: Optional[Dict[str, float]] = None, |
| 56 | + ) -> str: |
| 57 | + """ |
| 58 | + Method for submitting jobs to a Ray Cluster and returning the job id with entrypoint being a mandatory field. |
| 59 | + """ |
| 60 | + return self.rayJobClient.submit_job( |
| 61 | + entrypoint=entrypoint, |
| 62 | + job_id=job_id, |
| 63 | + runtime_env=runtime_env, |
| 64 | + metadata=metadata, |
| 65 | + submission_id=submission_id, |
| 66 | + entrypoint_num_cpus=entrypoint_num_cpus, |
| 67 | + entrypoint_num_gpus=entrypoint_num_gpus, |
| 68 | + entrypoint_resources=entrypoint_resources, |
| 69 | + ) |
| 70 | + |
| 71 | + def delete_job(self, job_id: str) -> bool: |
| 72 | + """ |
| 73 | + Method for deleting jobs with the job id being a mandatory field. |
| 74 | + """ |
| 75 | + return self.rayJobClient.delete_job(job_id=job_id) |
| 76 | + |
| 77 | + def get_address(self) -> str: |
| 78 | + """ |
| 79 | + Method for getting the address from the RayJobClient |
| 80 | + """ |
| 81 | + return self.rayJobClient.get_address() |
| 82 | + |
| 83 | + def get_job_info(self, job_id: str): |
| 84 | + """ |
| 85 | + Method for getting the job info with the job id being a mandatory field. |
| 86 | + """ |
| 87 | + return self.rayJobClient.get_job_info(job_id=job_id) |
| 88 | + |
| 89 | + def get_job_logs(self, job_id: str) -> str: |
| 90 | + """ |
| 91 | + Method for getting the job info with the job id being a mandatory field. |
| 92 | + """ |
| 93 | + return self.rayJobClient.get_job_logs(job_id=job_id) |
| 94 | + |
| 95 | + def get_job_status(self, job_id: str) -> str: |
| 96 | + """ |
| 97 | + Method for getting the job's status with the job id being a mandatory field. |
| 98 | + """ |
| 99 | + return self.rayJobClient.get_job_status(job_id=job_id) |
| 100 | + |
| 101 | + def list_jobs(self): |
| 102 | + """ |
| 103 | + Method for getting a list of current jobs in the Ray Cluster. |
| 104 | + """ |
| 105 | + return self.rayJobClient.list_jobs() |
| 106 | + |
| 107 | + def stop_job(self, job_id: str) -> bool: |
| 108 | + """ |
| 109 | + Method for stopping a job with the job id being a mandatory field. |
| 110 | + """ |
| 111 | + return self.rayJobClient.stop_job(job_id=job_id) |
| 112 | + |
| 113 | + def tail_job_logs(self, job_id: str) -> Iterator[str]: |
| 114 | + """ |
| 115 | + Method for getting an iterator that follows the logs of a job with the job id being a mandatory field. |
| 116 | + """ |
| 117 | + return self.rayJobClient.tail_job_logs(job_id=job_id) |
0 commit comments