-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
125 lines (101 loc) · 2.61 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"sync"
"github.com/iron-io/iron_go3/config"
"github.com/iron-io/iron_go3/worker"
)
type Config struct {
Environments map[string]config.Settings `json:"envs"`
Alerts []QueueWorkerAlert `json:"alerts"`
CacheEnv string `json:"cacheEnv"`
Runtime *int `json:"runtime,omitempty"`
}
type QueueWorkerAlert struct {
QueueName string `json:"queueName"`
QueueEnv string `json:"queueEnv"`
WorkerName string `json:"workerName"`
WorkerEnv string `json:"workerEnv"`
Cluster string `json:"cluster"`
Priority int `json:"priority"`
Interval *int `json:"interval"`
Triggers []Trigger `json:"triggers"`
Min *int `json:"min"`
Max *int `json:"max"`
}
type Trigger struct {
Typ string `json:"type"`
Value int `json:"value"`
}
func getConfig(fileName string) (*Config, error) {
c := &Config{}
configData, err := ioutil.ReadFile(fileName)
if err != nil {
reader, err := worker.ConfigReader()
defer reader.Close()
if err != nil {
return nil, fmt.Errorf("Could not read config: %v", err)
}
configData, err = ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("Could not read all of config: %v", err)
}
}
if len(configData) == 0 {
return nil, fmt.Errorf("No config provided")
}
err = json.Unmarshal(configData, c)
if err != nil {
return nil, fmt.Errorf("Could not unmarshal data %v\n", err)
}
return c, nil
}
func (a *QueueWorkerAlert) needPreviousSize() bool {
for _, t := range a.Triggers {
if t.Typ == "progressive" {
return true
}
}
return false
}
func (con *Config) getSettings(product, env string) (*config.Settings, bool) {
v := config.Presets[product]
settings := &v
if productSettings, ok := con.Environments["iron_"+product]; ok {
settings.UseSettings(&productSettings)
}
if envSettings, ok := con.Environments[env]; ok {
settings.UseSettings(&envSettings)
return settings, true
}
return settings, false
}
type state struct {
sync.RWMutex
queueSizes map[string]int
codeIds map[string]string
}
func (st *state) setQueueSize(key string, i int) {
st.Lock()
defer st.Unlock()
st.queueSizes[key] = i
}
func (st *state) getQueueSize(key string) (int, bool) {
st.RLock()
defer st.RUnlock()
i, exists := st.queueSizes[key]
return i, exists
}
func (st *state) setCodeId(name, id string) {
st.Lock()
defer st.Unlock()
st.codeIds[name] = id
}
func (st *state) getCodeId(name string) (string, bool) {
st.RLock()
defer st.RUnlock()
id, exists := st.codeIds[name]
return id, exists
}