Openstatus www.openstatus.dev

Please help (#1518)

* fix queue

fix fmt

* this is driving me insame

authored by

Thibault Le Ouay and committed by
GitHub
dafd3a76 8ef9f3ed

+59 -14
+59 -14
apps/checker/checker/update.go
··· 4 4 "bytes" 5 5 "context" 6 6 "encoding/json" 7 - "net/http" 8 - "os" 9 - "time" 10 - 7 + "fmt" 11 8 "github.com/rs/zerolog/log" 9 + "google.golang.org/api/option" 10 + "os" 12 11 12 + "cloud.google.com/go/auth" 13 + cloudtasks "cloud.google.com/go/cloudtasks/apiv2" 14 + taskspb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb" 13 15 ) 14 16 15 17 type UpdateData struct { ··· 22 24 Latency int64 `json:"latency,omitempty"` 23 25 } 24 26 25 - func UpdateStatus(ctx context.Context, updateData UpdateData) { 27 + func UpdateStatus(ctx context.Context, updateData UpdateData) error { 28 + 26 29 url := "https://openstatus-workflows.fly.dev/updateStatus" 27 30 basic := "Basic " + os.Getenv("CRON_SECRET") 28 31 payloadBuf := new(bytes.Buffer) 29 32 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 + 30 60 if err := json.NewEncoder(payloadBuf).Encode(updateData); err != nil { 31 61 log.Ctx(ctx).Error().Err(err).Msg("error while updating status") 32 - return 62 + return err 33 63 } 34 - req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, payloadBuf) 35 - req.Header.Set("Authorization", basic) 36 - req.Header.Set("Content-Type", "application/json") 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 + }, 78 + } 37 79 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) 41 87 } 42 88 43 - defer req.Body.Close() 44 - // Should we add a retry mechanism here? 89 + return nil 45 90 }