-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
task.bridge.go
163 lines (137 loc) · 4.37 KB
/
task.bridge.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
package pipeline
import (
"context"
"encoding/json"
"net/http"
"net/url"
"path"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/smartcontractkit/chainlink/core/bridges"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/pg"
)
//
// Return types:
// string
//
type BridgeTask struct {
BaseTask `mapstructure:",squash"`
Name string `json:"name"`
RequestData string `json:"requestData"`
IncludeInputAtKey string `json:"includeInputAtKey"`
Async string `json:"async"`
queryer pg.Queryer
config Config
httpClient *http.Client
}
var _ Task = (*BridgeTask)(nil)
var zeroURL = new(url.URL)
func (t *BridgeTask) Type() TaskType {
return TaskTypeBridge
}
func (t *BridgeTask) Run(ctx context.Context, lggr logger.Logger, vars Vars, inputs []Result) (result Result, runInfo RunInfo) {
inputValues, err := CheckInputs(inputs, -1, -1, 0)
if err != nil {
return Result{Error: errors.Wrap(err, "task inputs")}, runInfo
}
var (
name StringParam
requestData MapParam
includeInputAtKey StringParam
)
err = multierr.Combine(
errors.Wrap(ResolveParam(&name, From(NonemptyString(t.Name))), "name"),
errors.Wrap(ResolveParam(&requestData, From(VarExpr(t.RequestData, vars), JSONWithVarExprs(t.RequestData, vars, false), nil)), "requestData"),
errors.Wrap(ResolveParam(&includeInputAtKey, From(t.IncludeInputAtKey)), "includeInputAtKey"),
)
if err != nil {
return Result{Error: err}, runInfo
}
url, err := t.getBridgeURLFromName(name)
if err != nil {
return Result{Error: err}, runInfo
}
var metaMap MapParam
meta, _ := vars.Get("jobRun.meta")
switch v := meta.(type) {
case map[string]interface{}:
metaMap = MapParam(v)
case nil:
default:
lggr.Warnw(`"meta" field on task run is malformed, discarding`,
"task", t.DotID(),
"meta", meta,
)
}
requestData = withRunInfo(requestData, metaMap)
if t.IncludeInputAtKey != "" {
if len(inputValues) > 0 {
requestData[string(includeInputAtKey)] = inputValues[0]
}
}
if t.Async == "true" {
responseURL := t.config.BridgeResponseURL()
if *responseURL != *zeroURL {
responseURL.Path = path.Join(responseURL.Path, "/v2/resume/", t.uuid.String())
}
requestData["responseURL"] = responseURL.String()
}
requestDataJSON, err := json.Marshal(requestData)
if err != nil {
return Result{Error: err}, runInfo
}
lggr.Debugw("Bridge task: sending request",
"requestData", string(requestDataJSON),
"url", url.String(),
)
requestCtx, cancel := httpRequestCtx(ctx, t, t.config)
defer cancel()
responseBytes, statusCode, headers, elapsed, err := makeHTTPRequest(requestCtx, lggr, "POST", URLParam(url), []string{}, requestData, t.httpClient, t.config.DefaultHTTPLimit())
if err != nil {
return Result{Error: err}, RunInfo{IsRetryable: isRetryableHTTPError(statusCode, err)}
}
if t.Async == "true" {
// Look for a `pending` flag. This check is case-insensitive because http.Header normalizes header names
if _, ok := headers["X-Chainlink-Pending"]; ok {
return result, pendingRunInfo()
}
var response struct {
Pending bool `json:"pending"`
}
if err := json.Unmarshal(responseBytes, &response); err == nil && response.Pending {
return Result{}, pendingRunInfo()
}
}
// NOTE: We always stringify the response since this is required for all current jobs.
// If a binary response is required we might consider adding an adapter
// flag such as "BinaryMode: true" which passes through raw binary as the
// value instead.
result = Result{Value: string(responseBytes)}
promHTTPFetchTime.WithLabelValues(t.DotID()).Set(float64(elapsed))
promHTTPResponseBodySize.WithLabelValues(t.DotID()).Set(float64(len(responseBytes)))
lggr.Debugw("Bridge task: fetched answer",
"answer", result.Value,
"url", url.String(),
"dotID", t.DotID(),
)
return result, runInfo
}
func (t BridgeTask) getBridgeURLFromName(name StringParam) (URLParam, error) {
var bt bridges.BridgeType
err := t.queryer.Get(&bt, "SELECT * FROM bridge_types WHERE name = $1", string(name))
if err != nil {
return URLParam{}, errors.Wrapf(err, "could not find bridge with name '%s'", name)
}
return URLParam(bt.URL), nil
}
func withRunInfo(request MapParam, meta MapParam) MapParam {
output := make(MapParam)
for k, v := range request {
output[k] = v
}
if meta != nil {
output["meta"] = meta
}
return output
}