-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobs_client.go
413 lines (362 loc) · 12.7 KB
/
jobs_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package modzy
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"time"
"github.com/docker/go-units"
"github.com/modzy/sdk-go/model"
"github.com/pkg/errors"
)
type JobsClient interface {
// GetJobDetails will get the details of a job
GetJobDetails(ctx context.Context, input *GetJobDetailsInput) (*GetJobDetailsOutput, error)
// ListJobsHistory will list job history. This supports paging, filtering and sorting.
ListJobsHistory(ctx context.Context, input *ListJobsHistoryInput) (*ListJobsHistoryOutput, error)
// SubmitJobText will submit a new job of type "text"
SubmitJobText(ctx context.Context, input *SubmitJobTextInput) (*SubmitJobTextOutput, error)
// SubmitJobEmbedded will submit a new job of type "embedded" which is URI-encoded byte data
SubmitJobEmbedded(ctx context.Context, input *SubmitJobEmbeddedInput) (*SubmitJobEmbeddedOutput, error)
// SubmitJobFile will submit a new job and post the provided byte data as multiple chunks of data based on your account's maximum chunk size.
SubmitJobFile(ctx context.Context, input *SubmitJobFileInput) (*SubmitJobFileOutput, error)
// SubmitJobS3 submits a job that reads inputs from an S3 bucket
SubmitJobS3(ctx context.Context, input *SubmitJobS3Input) (*SubmitJobS3Output, error)
// SubmitJobJDBC submits a job that reads inputs from a Postgres database through a provided query
SubmitJobJDBC(ctx context.Context, input *SubmitJobJDBCInput) (*SubmitJobJDBCOutput, error)
// WaitForJobCompletion will block until a job has finished processing.
WaitForJobCompletion(ctx context.Context, input *WaitForJobCompletionInput, pollInterval time.Duration) (*GetJobDetailsOutput, error)
// CancelJob will cancel a job
CancelJob(ctx context.Context, input *CancelJobInput) (*CancelJobOutput, error)
// GetJobResults will get the results for a job
GetJobResults(ctx context.Context, input *GetJobResultsInput) (*GetJobResultsOutput, error)
// GetJobFeatures will read settings related to submitting jobs such as chunk size
GetJobFeatures(ctx context.Context) (*GetJobFeaturesOutput, error)
}
type standardJobsClient struct {
baseClient *standardClient
}
var _ JobsClient = &standardJobsClient{}
func (c *standardJobsClient) GetJobDetails(ctx context.Context, input *GetJobDetailsInput) (*GetJobDetailsOutput, error) {
var out model.JobDetails
url := fmt.Sprintf("/api/jobs/%s", input.JobIdentifier)
_, err := c.baseClient.requestor.Get(ctx, url, &out)
if err != nil {
return nil, err
}
return &GetJobDetailsOutput{
Details: out,
}, nil
}
func (c *standardJobsClient) ListJobsHistory(ctx context.Context, input *ListJobsHistoryInput) (*ListJobsHistoryOutput, error) {
input.Paging = input.Paging.withDefaults()
var items []model.JobDetails
url := "/api/jobs/history"
_, links, err := c.baseClient.requestor.List(ctx, url, input.Paging, &items)
if err != nil {
return nil, err
}
// decide if we have a next page (the next link is not always accurate?)
var nextPage *ListJobsHistoryInput
if _, hasNextLink := links["next"]; len(items) == input.Paging.PerPage && hasNextLink {
nextPage = &ListJobsHistoryInput{
Paging: input.Paging.Next(),
}
}
return &ListJobsHistoryOutput{
Jobs: items,
NextPage: nextPage,
}, nil
}
func (c *standardJobsClient) SubmitJobText(ctx context.Context, input *SubmitJobTextInput) (*SubmitJobTextOutput, error) {
toPostSources := map[string]model.TextInputItem{}
for k, v := range input.Inputs {
input := map[string]string{}
for innerK, innerV := range v {
input[innerK] = innerV
}
toPostSources[k] = input
}
toPost := model.SubmitTextJob{
Model: model.SubmitJobModelInfo{
Identifier: input.ModelIdentifier,
Version: input.ModelVersion,
},
Explain: input.Explain,
Timeout: int(input.Timeout / time.Millisecond),
Input: model.TextInput{
Type: "text",
Sources: toPostSources,
},
}
var response model.SubmitJobResponse
url := "/api/jobs"
_, err := c.baseClient.requestor.Post(ctx, url, toPost, &response)
if err != nil {
return nil, err
}
return &SubmitJobTextOutput{
Response: response,
JobActions: NewJobActions(c.baseClient, response.JobIdentifier),
}, nil
}
func (c *standardJobsClient) SubmitJobEmbedded(ctx context.Context, input *SubmitJobEmbeddedInput) (*SubmitJobEmbeddedOutput, error) {
toPostSources := map[string]model.EmbeddedInputItem{}
for k, v := range input.Inputs {
input := map[string]string{}
for innerK, innerV := range v {
dataReader, err := innerV()
if err != nil {
return nil, errors.WithMessagef(err, "Failed to get data reader for item %s/%s", k, innerK)
}
encodedString, err := io.ReadAll(dataReader)
if err != nil {
return nil, errors.WithMessagef(err, "Failed to stream data for item %s/%s", k, innerK)
}
input[innerK] = string(encodedString)
}
toPostSources[k] = input
}
toPost := model.SubmitEmbeddedJob{
Model: model.SubmitJobModelInfo{
Identifier: input.ModelIdentifier,
Version: input.ModelVersion,
},
Explain: input.Explain,
Timeout: int(input.Timeout / time.Millisecond),
Input: model.EmbeddedInput{
Type: "embedded",
Sources: toPostSources,
},
}
var response model.SubmitJobResponse
url := "/api/jobs"
_, err := c.baseClient.requestor.Post(ctx, url, toPost, &response)
if err != nil {
return nil, err
}
return &SubmitJobEmbeddedOutput{
Response: response,
JobActions: NewJobActions(c.baseClient, response.JobIdentifier),
}, nil
}
func (c *standardJobsClient) SubmitJobFile(ctx context.Context, input *SubmitJobFileInput) (*SubmitJobFileOutput, error) {
chunkSize, err := c.getMaxChunkSize(ctx, input.ChunkSize)
if err != nil {
return nil, errors.WithMessage(err, "Failed to get max chunk size")
}
noInputJob := model.SubmitChunkedJob{
Model: model.SubmitJobModelInfo{
Identifier: input.ModelIdentifier,
Version: input.ModelVersion,
},
Explain: input.Explain,
Timeout: int(input.Timeout / time.Millisecond),
}
var response model.SubmitJobResponse
if _, err := c.baseClient.requestor.Post(ctx, "/api/jobs", noInputJob, &response); err != nil {
return nil, errors.WithMessage(err, "failed to post open job before posting input chunks")
}
jobActions := NewJobActions(c.baseClient, response.JobIdentifier)
chunkErr := c.postInputsAsChunks(ctx, response.JobIdentifier, chunkSize, input.Inputs)
if chunkErr != nil {
// uploading the inputs failed, close the job
_, _ = jobActions.Cancel(ctx)
return nil, errors.WithMessage(chunkErr, "job canceled due to failure to upload data")
}
// close the job since everything is posted
closeURL := fmt.Sprintf("/api/jobs/%s/close", response.JobIdentifier)
if _, err := c.baseClient.requestor.Post(ctx, closeURL, nil, nil); err != nil {
return nil, errors.WithMessage(err, "failed to close open job after successfully uploading inputs")
}
return &SubmitJobFileOutput{
Response: response,
JobActions: jobActions,
}, nil
}
func (c *standardJobsClient) getMaxChunkSize(ctx context.Context, defaultChunkSize int) (int64, error) {
features, err := c.GetJobFeatures(ctx)
if err != nil {
return 0, err
}
maxChunkSize, err := units.FromHumanSize(features.Features.InputChunkMaximumSize)
if err != nil {
return 0, errors.WithMessage(err, "failed to parse InputChunkMaximumSize as an integer")
}
if maxChunkSize == 0 {
maxChunkSize = 1024 * 1024
}
chunkSize := int64(defaultChunkSize)
if chunkSize == 0 || chunkSize > maxChunkSize {
chunkSize = maxChunkSize
}
return chunkSize, nil
}
func (c *standardJobsClient) postInputsAsChunks(ctx context.Context, jobID string, chunkSize int64, inputs map[string]FileInputItem) error {
// go through each input and submit the data in chunks as necessary
for k, v := range inputs {
for innerK, innerV := range v {
dataReader, err := innerV()
if err != nil {
return errors.WithMessagef(err, "failed to get data reader for item %s/%s", k, innerK)
}
// post as many chunks as necessary
buf, err := ioutil.ReadAll(dataReader)
if err != nil {
return errors.WithMessage(err, "failed reading a chunk of data")
}
start := 0
end := 0
for {
end = start + int(chunkSize)
if end > len(buf) {
end = len(buf)
}
if start == end {
break
}
chunk := buf[start:end]
chunkURL := fmt.Sprintf("/api/jobs/%s/%s/%s", jobID, k, innerK)
chunkReader := bytes.NewReader(chunk)
if _, err := c.baseClient.requestor.PostMultipart(ctx, chunkURL, map[string]io.Reader{"input": chunkReader}, nil); err != nil {
return errors.WithMessage(err, "failed to post a chunk of data")
}
start = end
}
}
}
return nil
}
func (c *standardJobsClient) SubmitJobS3(ctx context.Context, input *SubmitJobS3Input) (*SubmitJobS3Output, error) {
toPostSources := map[string]model.S3InputItem{}
for k, v := range input.Inputs {
input := map[string]model.S3InputItemKey{}
for innerK, innerV := range v {
s3Location, err := innerV()
if err != nil {
return nil, errors.WithMessagef(err, "Failed to get s3 key definition for item %s/%s", k, innerK)
}
input[innerK] = model.S3InputItemKey{
Bucket: s3Location.Bucket,
Key: s3Location.Key,
}
}
toPostSources[k] = input
}
toPost := model.SubmitS3Job{
Model: model.SubmitJobModelInfo{
Identifier: input.ModelIdentifier,
Version: input.ModelVersion,
},
Explain: input.Explain,
Timeout: int(input.Timeout / time.Millisecond),
Input: model.S3Input{
Type: "aws-s3",
AccessKeyID: input.AWSAccessKeyID,
SecretAccessKey: input.AWSSecretAccessKey,
Region: input.AWSRegion,
Sources: toPostSources,
},
}
var response model.SubmitJobResponse
url := "/api/jobs"
_, err := c.baseClient.requestor.Post(ctx, url, toPost, &response)
if err != nil {
return nil, err
}
return &SubmitJobEmbeddedOutput{
Response: response,
JobActions: NewJobActions(c.baseClient, response.JobIdentifier),
}, nil
}
func (c *standardJobsClient) SubmitJobJDBC(ctx context.Context, input *SubmitJobJDBCInput) (*SubmitJobJDBCOutput, error) {
toPost := model.SubmitJDBCJob{
Model: model.SubmitJobModelInfo{
Identifier: input.ModelIdentifier,
Version: input.ModelVersion,
},
Explain: input.Explain,
Timeout: int(input.Timeout / time.Millisecond),
Input: model.JDBCInput{
Type: "jdbc",
URL: input.JDBCConnectionURL,
Username: input.DatabaseUsername,
Password: input.DatabasePassword,
Driver: "org.postgresql.Driver",
Query: input.Query,
},
}
var response model.SubmitJobResponse
url := "/api/jobs"
_, err := c.baseClient.requestor.Post(ctx, url, toPost, &response)
if err != nil {
return nil, err
}
return &SubmitJobEmbeddedOutput{
Response: response,
JobActions: NewJobActions(c.baseClient, response.JobIdentifier),
}, nil
}
// WaitForJobCompletion will wait until the provided job is done processing.
// The minimum pollInterval is 5 seconds.
// If the provided context is canceled, this wait will error.
func (c *standardJobsClient) WaitForJobCompletion(ctx context.Context, input *WaitForJobCompletionInput, pollInterval time.Duration) (*GetJobDetailsOutput, error) {
timer := time.NewTimer(pollInterval)
for {
select {
case <-ctx.Done():
return nil, fmt.Errorf("wait for job completion was canceled due to provided context being canceled")
case <-timer.C:
job, err := c.GetJobDetails(ctx, &GetJobDetailsInput{input.JobIdentifier})
if err != nil {
return nil, err
}
// check
if job.Details.Status == JobStatusCanceled ||
job.Details.Status == JobStatusCompleted ||
job.Details.Status == JobStatusTimedOut {
// job is done
return job, nil
}
if job.Details.Status == JobStatusOpen {
return nil, fmt.Errorf("job is currently OPEN and will never complete")
}
// not done -- wait and try again
timer.Reset(pollInterval)
}
}
}
func (c *standardJobsClient) CancelJob(ctx context.Context, input *CancelJobInput) (*CancelJobOutput, error) {
var response model.JobDetails
url := fmt.Sprintf("/api/jobs/%s", input.JobIdentifier)
_, err := c.baseClient.requestor.Delete(ctx, url, &response)
if err != nil {
return nil, err
}
return &CancelJobOutput{
Details: response,
}, nil
}
func (c *standardJobsClient) GetJobResults(ctx context.Context, input *GetJobResultsInput) (*GetJobResultsOutput, error) {
var response model.JobResults
url := fmt.Sprintf("/api/results/%s", input.JobIdentifier)
_, err := c.baseClient.requestor.Get(ctx, url, &response)
if err != nil {
return nil, err
}
return &GetJobResultsOutput{
Results: response,
}, nil
}
func (c *standardJobsClient) GetJobFeatures(ctx context.Context) (*GetJobFeaturesOutput, error) {
var response model.JobFeatures
url := "/api/jobs/features"
_, err := c.baseClient.requestor.Get(ctx, url, &response)
if err != nil {
return nil, err
}
return &GetJobFeaturesOutput{
Features: response,
}, nil
}