Skip to content

Commit dafd3a7

Browse files
Please help (#1518)
* fix queue fix fmt * this is driving me insame
1 parent 8ef9f3e commit dafd3a7

File tree

1 file changed

+59
-14
lines changed

1 file changed

+59
-14
lines changed

apps/checker/checker/update.go

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"net/http"
8-
"os"
9-
"time"
10-
7+
"fmt"
118
"github.com/rs/zerolog/log"
9+
"google.golang.org/api/option"
10+
"os"
1211

12+
"cloud.google.com/go/auth"
13+
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
14+
taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
1315
)
1416

1517
type UpdateData struct {
@@ -22,24 +24,67 @@ type UpdateData struct {
2224
Latency int64 `json:"latency,omitempty"`
2325
}
2426

25-
func UpdateStatus(ctx context.Context, updateData UpdateData) {
27+
func UpdateStatus(ctx context.Context, updateData UpdateData) error {
28+
2629
url := "https://openstatus-workflows.fly.dev/updateStatus"
2730
basic := "Basic " + os.Getenv("CRON_SECRET")
2831
payloadBuf := new(bytes.Buffer)
2932

33+
opts := &auth.Options2LO{
34+
Email: os.Getenv("GCP_CLIENT_EMAIL"),
35+
PrivateKey: []byte(os.Getenv("GCP_PRIVATE_KEY")),
36+
PrivateKeyID: os.Getenv("GCP_PRIVATE_KEY_ID"),
37+
Scopes: []string{
38+
"https://www.googleapis.com/auth/cloud-platform",
39+
},
40+
TokenURL: "https://oauth2.googleapis.com/token",
41+
}
42+
43+
tp, err := auth.New2LOTokenProvider(opts)
44+
if err != nil {
45+
log.Ctx(ctx).Error().Err(err).Msg("error while creating token provider")
46+
return err
47+
}
48+
49+
creds := auth.NewCredentials(&auth.CredentialsOptions{
50+
TokenProvider: tp,
51+
})
52+
53+
client, err := cloudtasks.NewClient(ctx, option.WithAuthCredentials(creds))
54+
if err != nil {
55+
log.Ctx(ctx).Error().Err(err).Msg("error while creating cloud tasks client")
56+
57+
}
58+
defer client.Close()
59+
3060
if err := json.NewEncoder(payloadBuf).Encode(updateData); err != nil {
3161
log.Ctx(ctx).Error().Err(err).Msg("error while updating status")
32-
return
62+
return err
63+
}
64+
projectID := os.Getenv("GCP_PROJECT_ID")
65+
queuePath := fmt.Sprintf("projects/%s/locations/europe-west1/queues/alerting", projectID)
66+
req := &taskspb.CreateTaskRequest{
67+
Parent: queuePath,
68+
Task: &taskspb.Task{
69+
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest
70+
MessageType: &taskspb.Task_HttpRequest{
71+
HttpRequest: &taskspb.HttpRequest{
72+
HttpMethod: taskspb.HttpMethod_POST,
73+
Url: url,
74+
Headers: map[string]string{"Authorization": basic, "Content-Type": "application/json"},
75+
},
76+
},
77+
},
3378
}
34-
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, payloadBuf)
35-
req.Header.Set("Authorization", basic)
36-
req.Header.Set("Content-Type", "application/json")
3779

38-
client := &http.Client{Timeout: time.Second * 10}
39-
if _, err := client.Do(req); err != nil {
40-
log.Ctx(ctx).Error().Err(err).Msg("error while updating status")
80+
// Add a payload message if one is present.
81+
req.Task.GetHttpRequest().Body = payloadBuf.Bytes()
82+
83+
_, err = client.CreateTask(ctx, req)
84+
if err != nil {
85+
log.Ctx(ctx).Error().Err(err).Msg("error while creating the cloud task")
86+
return fmt.Errorf("cloudtasks.CreateTask: %w", err)
4187
}
4288

43-
defer req.Body.Close()
44-
// Should we add a retry mechanism here?
89+
return nil
4590
}

0 commit comments

Comments
 (0)