/
service.go
171 lines (144 loc) · 5.04 KB
/
service.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
/*
Copyright 2022 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gateway
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/types/async"
"go.uber.org/zap"
)
// Service provides an interface to the async-gateway business logic
type Service interface {
CreateWorkload(id string, apiName string, queueURL string, payload io.Reader, headers http.Header) (string, error)
GetWorkload(id string, apiName string) (GetWorkloadResponse, error)
}
type service struct {
logger *zap.SugaredLogger
storage Storage
clusterUID string
session session.Session
}
// NewService creates a new async-gateway service
func NewService(clusterUID string, storage Storage, logger *zap.SugaredLogger, session session.Session) Service {
return &service{
logger: logger,
storage: storage,
clusterUID: clusterUID,
session: session,
}
}
// CreateWorkload enqueues an async workload request and uploads the request payload to S3
func (s *service) CreateWorkload(id string, apiName string, queueURL string, payload io.Reader, headers http.Header) (string, error) {
prefix := async.StoragePath(s.clusterUID, apiName)
log := s.logger.With(zap.String("id", id), zap.String("apiName", apiName))
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(headers); err != nil {
return "", errors.Wrap(err, "failed to dump headers")
}
headersPath := async.HeadersPath(prefix, id)
log.Debugw("uploading headers", zap.String("path", headersPath))
if err := s.storage.Upload(headersPath, buf, "application/json"); err != nil {
return "", errors.Wrap(err, "failed to upload headers")
}
contentType := headers.Get("Content-Type")
payloadPath := async.PayloadPath(prefix, id)
log.Debugw("uploading payload", zap.String("path", payloadPath))
if err := s.storage.Upload(payloadPath, payload, contentType); err != nil {
return "", errors.Wrap(err, "failed to upload payload")
}
log.Debug("sending message to queue")
queue := NewSQS(queueURL, &s.session)
if err := queue.SendMessage(id, id); err != nil {
return "", errors.Wrap(err, "failed to send message to queue")
}
statusPath := fmt.Sprintf("%s/%s/status/%s", prefix, id, async.StatusInQueue)
log.Debug(fmt.Sprintf("setting status to %s", async.StatusInQueue))
if err := s.storage.Upload(statusPath, strings.NewReader(""), "text/plain"); err != nil {
return "", errors.Wrap(err, "failed to upload workload status")
}
return id, nil
}
// GetWorkload retrieves the status and result, if available, of a given workload
func (s *service) GetWorkload(id string, apiName string) (GetWorkloadResponse, error) {
log := s.logger.With(zap.String("id", id), zap.String("apiName", apiName))
st, err := s.getStatus(id, apiName)
if err != nil {
return GetWorkloadResponse{}, err
}
if st != async.StatusCompleted {
return GetWorkloadResponse{
ID: id,
Status: st,
}, nil
}
// attempt to download user result
prefix := async.StoragePath(s.clusterUID, apiName)
resultPath := async.ResultPath(prefix, id)
log.Debug("downloading user result", zap.String("path", resultPath))
resultBuf, err := s.storage.Download(resultPath)
if err != nil {
return GetWorkloadResponse{}, err
}
var userResponse UserResponse
if err = json.Unmarshal(resultBuf, &userResponse); err != nil {
return GetWorkloadResponse{}, err
}
log.Debug("getting workload timestamp")
timestamp, err := s.storage.GetLastModified(resultPath)
if err != nil {
return GetWorkloadResponse{}, err
}
return GetWorkloadResponse{
ID: id,
Status: st,
Result: &userResponse,
Timestamp: ×tamp,
}, nil
}
func (s *service) getStatus(id string, apiName string) (async.Status, error) {
prefix := async.StoragePath(s.clusterUID, apiName)
log := s.logger.With(zap.String("id", id))
// download workload status
statusPrefixPath := async.StatusPrefixPath(prefix, id)
log.Debug("checking status", zap.String("path", statusPrefixPath))
files, err := s.storage.List(statusPrefixPath)
if err != nil {
return "", err
}
if len(files) == 0 {
return async.StatusNotFound, nil
}
// determine request status
st := async.StatusInQueue
for _, file := range files {
fileStatus := async.Status(file)
if !fileStatus.Valid() {
st = fileStatus
return "", fmt.Errorf("invalid workload status: %s", st)
}
if fileStatus == async.StatusInProgress {
st = fileStatus
}
if fileStatus == async.StatusCompleted || fileStatus == async.StatusFailed {
st = fileStatus
break
}
}
return st, nil
}