Openstatus www.openstatus.dev

Maybe (#1520)

* fix queue

fix fmt

* this is driving me insame

* this should be ok

authored by

Thibault Le Ouay and committed by
GitHub
b2247098 62592a9e

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