|
| 1 | +/* |
| 2 | +Copyright 2023. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package support |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "encoding/json" |
| 22 | + "io/ioutil" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | +) |
| 26 | + |
| 27 | +type RayJobSetup struct { |
| 28 | + EntryPoint string `json:"entrypoint"` |
| 29 | + RuntimeEnv map[string]any `json:"runtime_env"` |
| 30 | +} |
| 31 | + |
| 32 | +type RayJobResponse struct { |
| 33 | + JobID string `json:"job_id"` |
| 34 | + SubmissionID string `json:"submission_id"` |
| 35 | +} |
| 36 | + |
| 37 | +type RayJobDetailsResponse struct { |
| 38 | + JobID string `json:"job_id"` |
| 39 | + SubmissionID string `json:"submission_id"` |
| 40 | + Status string `json:"status"` |
| 41 | +} |
| 42 | + |
| 43 | +type RayJobLogsResponse struct { |
| 44 | + Logs string `json:"logs"` |
| 45 | +} |
| 46 | + |
| 47 | +var _ RayClusterClient = (*rayClusterClient)(nil) |
| 48 | + |
| 49 | +type rayClusterClient struct { |
| 50 | + endpoint url.URL |
| 51 | +} |
| 52 | + |
| 53 | +type RayClusterClient interface { |
| 54 | + CreateJob(job *RayJobSetup) (*RayJobResponse, error) |
| 55 | + GetJobDetails(jobID string) (*RayJobDetailsResponse, error) |
| 56 | + GetJobLogs(jobID string) (string, error) |
| 57 | +} |
| 58 | + |
| 59 | +func NewRayClusterClient(dashboardEndpoint url.URL) RayClusterClient { |
| 60 | + return &rayClusterClient{endpoint: dashboardEndpoint} |
| 61 | +} |
| 62 | + |
| 63 | +func (client *rayClusterClient) CreateJob(job *RayJobSetup) (response *RayJobResponse, err error) { |
| 64 | + marshalled, err := json.Marshal(job) |
| 65 | + if err != nil { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + createJobURL := client.endpoint.String() + "/api/jobs/" |
| 70 | + resp, err := http.Post(createJobURL, "application/json", bytes.NewReader(marshalled)) |
| 71 | + if err != nil { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + respData, err := ioutil.ReadAll(resp.Body) |
| 76 | + if err != nil { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + err = json.Unmarshal(respData, &response) |
| 81 | + return |
| 82 | +} |
| 83 | + |
| 84 | +func (client *rayClusterClient) GetJobDetails(jobID string) (response *RayJobDetailsResponse, err error) { |
| 85 | + createJobURL := client.endpoint.String() + "/api/jobs/" + jobID |
| 86 | + resp, err := http.Get(createJobURL) |
| 87 | + if err != nil { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + respData, err := ioutil.ReadAll(resp.Body) |
| 92 | + if err != nil { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + err = json.Unmarshal(respData, &response) |
| 97 | + return |
| 98 | +} |
| 99 | + |
| 100 | +func (client *rayClusterClient) GetJobLogs(jobID string) (logs string, err error) { |
| 101 | + createJobURL := client.endpoint.String() + "/api/jobs/" + jobID + "/logs" |
| 102 | + resp, err := http.Get(createJobURL) |
| 103 | + if err != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + respData, err := ioutil.ReadAll(resp.Body) |
| 108 | + if err != nil { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + jobLogs := RayJobLogsResponse{} |
| 113 | + err = json.Unmarshal(respData, &jobLogs) |
| 114 | + return jobLogs.Logs, err |
| 115 | +} |
0 commit comments