-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
93 lines (79 loc) · 2.17 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"os"
"time"
"context"
"github.com/redis/go-redis/v9"
"go.opentelemetry.io/otel/attribute"
. "github.com/fireacademy/golden-gate/redis"
telemetry "github.com/fireacademy/telemetry"
)
var DataDudeAPIKeyInfoURL string
var DataDudeBillCreditsPackageURL string
var DataDudeRecordUsageURL string
var ManagementToken string
func loop() {
time.Sleep(5 * time.Second)
for true {
apiKey, err := RDB.SPop(context.Background(), PROCESS_QUEUE_SET_NAME).Result()
if err != nil {
if err == redis.Nil {
time.Sleep(time.Second)
continue
} else {
ctx, span := telemetry.GetSpan(context.Background(), "loop")
defer span.End()
telemetry.LogError(ctx, err, "error when popping item from API kyes queue")
panic(err)
}
}
ctx, span := telemetry.GetSpan(context.Background(), "loop")
span.SetAttributes(
attribute.String("api_key", apiKey),
)
_, _, err = RefreshAPIKey(ctx, apiKey)
if err != nil {
telemetry.LogError(ctx, err, "error while refreshing API key")
err = RDB.SAdd(ctx, PROCESS_QUEUE_SET_NAME, apiKey).Err()
if err != nil {
telemetry.LogError(ctx, err, "error while adding API key back to queue")
}
}
span.End()
}
}
func getDataDudeApiKeyInfoURL() string {
url := os.Getenv("DATA_DUDE_API_KEY_INFO_URL")
if url == "" {
panic("DATA_DUDE_API_KEY_INFO_URL not set.")
}
return url
}
func getDataDudeBillCreditsPackageURL() string {
url := os.Getenv("DATA_DUDE_BILL_CREDITS_PACKAGE_URL")
if url == "" {
panic("DATA_DUDE_BILL_CREDITS_PACKAGE_URL not set.")
}
return url
}
func getDataDudeRecordUsageURL() string {
url := os.Getenv("DATA_DUDE_RECORD_USAGE_URL")
if url == "" {
panic("DATA_DUDE_RECORD_USAGE_URL not set.")
}
return url
}
func getManagementToken() string {
token := os.Getenv("DATA_DUDE_MANAGEMENT_TOKEN")
if token == "" {
panic("DATA_DUDE_MANAGEMENT_TOKEN not set.")
}
return token
}
func SetupCheck() {
DataDudeAPIKeyInfoURL = getDataDudeApiKeyInfoURL()
DataDudeBillCreditsPackageURL = getDataDudeBillCreditsPackageURL()
DataDudeRecordUsageURL = getDataDudeRecordUsageURL()
ManagementToken = getManagementToken()
go loop()
}