/
job_http.go
73 lines (65 loc) · 1.9 KB
/
job_http.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
package job
import (
"github.com/squzy/squzy/internal/helpers"
"github.com/squzy/squzy/internal/httptools"
scheduler_config_storage "github.com/squzy/squzy/internal/scheduler-config-storage"
apiPb "github.com/squzy/squzy_generated/generated/github.com/squzy/squzy_proto"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
)
type httpError struct {
schedulerID string
startTime *timestamp.Timestamp
endTime *timestamp.Timestamp
code apiPb.SchedulerCode
description string
}
func (e *httpError) GetLogData() *apiPb.SchedulerResponse {
var err *apiPb.SchedulerSnapshot_Error
if e.code == apiPb.SchedulerCode_ERROR {
err = &apiPb.SchedulerSnapshot_Error{
Message: e.description,
}
}
return &apiPb.SchedulerResponse{
SchedulerId: e.schedulerID,
Snapshot: &apiPb.SchedulerSnapshot{
Code: e.code,
Error: err,
Type: apiPb.SchedulerType_HTTP,
Meta: &apiPb.SchedulerSnapshot_MetaData{
StartTime: e.startTime,
EndTime: e.endTime,
},
},
}
}
func newHTTPError(schedulerID string, startTime *timestamp.Timestamp, endTime *timestamp.Timestamp, code apiPb.SchedulerCode, description string) CheckError {
return &httpError{
schedulerID: schedulerID,
startTime: startTime,
endTime: endTime,
code: code,
description: description,
}
}
func ExecHTTP(schedulerID string, timeout int32, config *scheduler_config_storage.HTTPConfig, httpTool httptools.HTTPTool) CheckError {
startTime := timestamp.Now()
req := httpTool.CreateRequest(config.Method, config.URL, &config.Headers, schedulerID)
_, _, err := httpTool.SendRequestTimeoutStatusCode(req, helpers.DurationFromSecond(timeout), int(config.StatusCode))
if err != nil {
return newHTTPError(
schedulerID,
startTime,
timestamp.Now(),
apiPb.SchedulerCode_ERROR,
err.Error(),
)
}
return newHTTPError(
schedulerID,
startTime,
timestamp.Now(),
apiPb.SchedulerCode_OK,
"",
)
}