From 1594b12546c6da170c95c6d93f849d3be29f9238 Mon Sep 17 00:00:00 2001 From: alyssa Date: Thu, 14 Nov 2024 10:30:10 +0900 Subject: [PATCH] feat(scheduled_tasks): use env variables instead of consul to find gateway instances --- go.work | 8 ++--- go.work.sum | 3 +- services/scheduled_tasks/go.mod | 2 +- services/scheduled_tasks/repo.go | 53 ++++++-------------------------- 4 files changed, 17 insertions(+), 49 deletions(-) diff --git a/go.work b/go.work index fb60983f..a9e9d1ec 100644 --- a/go.work +++ b/go.work @@ -1,5 +1,5 @@ -go 1.19 +go 1.23 -use ( - ./services/scheduled_tasks -) +toolchain go1.23.2 + +use ./services/scheduled_tasks diff --git a/go.work.sum b/go.work.sum index 20bb40cf..72bcf761 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,3 +1,4 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= +github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= diff --git a/services/scheduled_tasks/go.mod b/services/scheduled_tasks/go.mod index 7cea5024..74020db5 100644 --- a/services/scheduled_tasks/go.mod +++ b/services/scheduled_tasks/go.mod @@ -1,6 +1,6 @@ module scheduled_tasks -go 1.18 +go 1.23 require ( github.com/getsentry/sentry-go v0.15.0 diff --git a/services/scheduled_tasks/repo.go b/services/scheduled_tasks/repo.go index ece7e0dd..31554850 100644 --- a/services/scheduled_tasks/repo.go +++ b/services/scheduled_tasks/repo.go @@ -20,59 +20,26 @@ type httpstats struct { func query_http_cache() []httpstats { var values []httpstats - url := os.Getenv("CONSUL_URL") - if url == "" { - panic("missing CONSUL_URL in environment") + http_cache_url := os.Getenv("HTTP_CACHE_URL") + if http_cache_url == "" { + panic("missing HTTP_CACHE_URL in environment") } - expected_gateway_count, err := strconv.Atoi(os.Getenv("EXPECTED_GATEWAY_COUNT")) + cluster_count, err := strconv.Atoi(os.Getenv("CLUSTER_COUNT")) if err != nil { - panic(fmt.Sprintf("missing or invalid EXPECTED_GATEWAY_COUNT in environment")) + panic(fmt.Sprintf("missing or invalid CLUSTER_COUNT in environment")) } - resp, err := http.Get(fmt.Sprintf("%v/v1/health/service/pluralkit-gateway", url)) - if err != nil { - panic(err) - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - panic(fmt.Sprintf("got status %v trying to query consul for all_gateway_instances", resp.Status)) - } - - var ips []string - - data, err := io.ReadAll(resp.Body) - if err != nil { - panic(err) - } - var cs []any - err = json.Unmarshal(data, &cs) - if err != nil { - panic(err) - } - - if len(cs) != expected_gateway_count { - panic(fmt.Sprintf("got unexpected number of gateway instances from consul (expected %v, got %v)", expected_gateway_count, len(cs))) - } - - for idx, itm := range cs { - if ip, ok := itm.(map[string]any)["Service"].(map[string]any)["Address"].(string); ok { - ips = append(ips, ip) - } else { - panic(fmt.Sprintf("got bad data from consul for all_gateway_instances, at index %v", idx)) - } - } - - log.Printf("querying %v gateway clusters for discord stats\n", len(ips)) - - for _, ip := range ips { - resp, err := http.Get("http://"+ip+":5000/stats") + for i := range cluster_count { + log.Printf("querying gateway cluster %v for discord stats\n", i) + url := fmt.Sprintf("http://cluster%v.%s:5000/stats", i, http_cache_url) + resp, err := http.Get(url) if err != nil { panic(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusFound { - panic(fmt.Sprintf("got status %v trying to query %v:5000", resp.Status, ip)) + panic(fmt.Sprintf("got status %v trying to query %v.%s:5000", resp.Status, i, http_cache_url)) } var s httpstats data, err := io.ReadAll(resp.Body)