forked from moznion/resque_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
75 lines (65 loc) · 1.7 KB
/
config.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
package resqueExporter
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"strconv"
)
type Config struct {
GuardIntervalMillis int64
ResqueNamespace string
Redis *RedisConfig
QueueConfiguration *QueueConfiguration
ExportFailureDetails bool
}
type RedisConfig struct {
Host string
Port int
Password string
DB int64
}
type QueueConfiguration struct {
QueueToWorkers map[string][]string `yaml:"queue_to_workers"`
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func getQueueConfiguration(filePath string) (config *QueueConfiguration) {
yamlFile, err := ioutil.ReadFile(filePath)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
panic(err)
}
return config
}
func loadConfig() (*Config, error) {
guardIntervalMillisInt, _ := strconv.Atoi(getEnv("GUARD_INTERVAL_MILLIS", "0"))
guardIntervalMillis := int64(guardIntervalMillisInt)
resqueNamespace := getEnv("RESQUE_NAMESPACE", "resque")
redisHost := os.Getenv("REDIS_HOST")
redisPort, _ := strconv.Atoi(getEnv("REDIS_PORT", "6379"))
redisPassword := ""
redisDB := int64(0)
queueConfigurationFilePath := getEnv("QUEUE_CONFIGURATION_FILE_PATH", "./config.yml")
queueConfiguration := getQueueConfiguration(queueConfigurationFilePath)
exportFailureDetails := (getEnv("EXPORT_FAILURE_DETAILS", "false") == "true")
config := &Config{
GuardIntervalMillis: guardIntervalMillis,
ResqueNamespace: resqueNamespace,
Redis: &RedisConfig{
redisHost,
redisPort,
redisPassword,
redisDB,
},
QueueConfiguration: queueConfiguration,
ExportFailureDetails: exportFailureDetails,
}
return config, nil
}