tangled
alpha
login
or
join now
openstatus.dev
/
openstatus
6
fork
atom
Openstatus
www.openstatus.dev
6
fork
atom
overview
issues
pulls
pipelines
test (#1514)
authored by
Thibault Le Ouay
and committed by
GitHub
4 months ago
684524d5
10eb885c
+12
-57
1 changed file
expand all
collapse all
unified
split
apps
checker
checker
update.go
+12
-57
apps/checker/checker/update.go
···
4
4
"bytes"
5
5
"context"
6
6
"encoding/json"
7
7
-
"fmt"
7
7
+
"net/http"
8
8
"os"
9
9
+
"time"
9
10
10
11
"github.com/rs/zerolog/log"
11
11
-
"google.golang.org/api/option"
12
12
13
13
-
"cloud.google.com/go/auth"
14
14
-
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
15
15
-
taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
16
13
)
17
14
18
15
type UpdateData struct {
···
25
22
Latency int64 `json:"latency,omitempty"`
26
23
}
27
24
28
28
-
func UpdateStatus(ctx context.Context, updateData UpdateData) error {
29
29
-
25
25
+
func UpdateStatus(ctx context.Context, updateData UpdateData) {
30
26
url := "https://openstatus-workflows.fly.dev/updateStatus"
31
27
basic := "Basic " + os.Getenv("CRON_SECRET")
32
28
payloadBuf := new(bytes.Buffer)
33
29
34
34
-
opts := &auth.Options2LO{
35
35
-
Email: os.Getenv("GCP_CLIENT_EMAIL"),
36
36
-
PrivateKey: []byte(os.Getenv("GCP_PRIVATE_KEY")),
37
37
-
Scopes: []string{
38
38
-
"https://www.googleapis.com/auth/cloud-platform",
39
39
-
},
40
40
-
TokenURL: "https://oauth2.googleapis.com/token",
41
41
-
}
42
42
-
43
43
-
tp, err := auth.New2LOTokenProvider(opts)
44
44
-
if err != nil {
45
45
-
log.Ctx(ctx).Error().Err(err).Msg("error while creating token provider")
46
46
-
return err
47
47
-
}
48
48
-
49
49
-
creds := auth.NewCredentials(&auth.CredentialsOptions{
50
50
-
TokenProvider: tp,
51
51
-
})
52
52
-
53
53
-
client, err := cloudtasks.NewClient(ctx, option.WithAuthCredentials(creds))
54
54
-
if err != nil {
55
55
-
log.Ctx(ctx).Error().Err(err).Msg("error while creating cloud tasks client")
56
56
-
57
57
-
}
58
58
-
defer client.Close()
59
59
-
60
30
if err := json.NewEncoder(payloadBuf).Encode(updateData); err != nil {
61
31
log.Ctx(ctx).Error().Err(err).Msg("error while updating status")
62
62
-
return err
63
63
-
}
64
64
-
projectID := os.Getenv("GCP_PROJECT_ID")
65
65
-
queuePath := fmt.Sprintf("projects/%s/locations/europe-west1/queues/alerting", projectID)
66
66
-
req := &taskspb.CreateTaskRequest{
67
67
-
Parent: queuePath,
68
68
-
Task: &taskspb.Task{
69
69
-
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest
70
70
-
MessageType: &taskspb.Task_HttpRequest{
71
71
-
HttpRequest: &taskspb.HttpRequest{
72
72
-
HttpMethod: taskspb.HttpMethod_POST,
73
73
-
Url: url,
74
74
-
Headers: map[string]string{"Authorization": basic, "Content-Type": "application/json"},
75
75
-
},
76
76
-
},
77
77
-
},
32
32
+
return
78
33
}
34
34
+
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, payloadBuf)
35
35
+
req.Header.Set("Authorization", basic)
36
36
+
req.Header.Set("Content-Type", "application/json")
79
37
80
80
-
// Add a payload message if one is present.
81
81
-
req.Task.GetHttpRequest().Body = payloadBuf.Bytes()
82
82
-
83
83
-
_, err = client.CreateTask(ctx, req)
84
84
-
if err != nil {
85
85
-
log.Ctx(ctx).Error().Err(err).Msg("error while creating the cloud task")
86
86
-
return fmt.Errorf("cloudtasks.CreateTask: %w", err)
38
38
+
client := &http.Client{Timeout: time.Second * 10}
39
39
+
if _, err := client.Do(req); err != nil {
40
40
+
log.Ctx(ctx).Error().Err(err).Msg("error while updating status")
87
41
}
88
42
89
89
-
return nil
43
43
+
defer req.Body.Close()
44
44
+
// Should we add a retry mechanism here?
90
45
}