forked from alpacahq/marketstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
168 lines (155 loc) · 4.24 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package utils
import (
"errors"
"fmt"
"strconv"
"time"
"gopkg.in/yaml.v2"
. "github.com/alpacahq/marketstore/utils/log"
)
var InstanceConfig MktsConfig
func init() {
InstanceConfig.Timezone = time.UTC
}
type TriggerSetting struct {
Module string
On string
Config map[string]interface{}
}
type BgWorkerSetting struct {
Module string
Name string
Config map[string]interface{}
}
type MktsConfig struct {
RootDirectory string
ListenPort string
Timezone *time.Location
Queryable bool
StopGracePeriod time.Duration
WALRotateInterval int
EnableAdd bool
EnableRemove bool
EnableLastKnown bool
StartTime time.Time
Triggers []*TriggerSetting
BgWorkers []*BgWorkerSetting
}
func (m *MktsConfig) Parse(data []byte) error {
var err error
var aux struct {
RootDirectory string `yaml:"root_directory"`
ListenPort string `yaml:"listen_port"`
Timezone string `yaml:"timezone"`
LogLevel string `yaml:"log_level"`
Queryable string `yaml:"queryable"`
StopGracePeriod int `yaml:"stop_grace_period"`
WALRotateInterval int `yaml:"wal_rotate_interval"`
EnableAdd string `yaml:"enable_add"`
EnableRemove string `yaml:"enable_remove"`
EnableLastKnown string `yaml:"enable_last_known"`
Triggers []struct {
Module string `yaml:"module"`
On string `yaml:"on"`
Config map[string]interface{} `yaml:"config"`
} `yaml:"triggers"`
BgWorkers []struct {
Module string `yaml:"module"`
Name string `yaml:"name"`
Config map[string]interface{} `yaml:"config"`
} `yaml:"bgworkers"`
}
if err := yaml.Unmarshal(data, &aux); err != nil {
return err
}
if aux.RootDirectory == "" {
Log(FATAL, "Invalid root directory.")
return errors.New("Invalid root directory.")
}
if aux.ListenPort == "" {
Log(FATAL, "Invalid listen port.")
return errors.New("Invalid listen port.")
}
// Giving "" to LoadLocation will be UTC anyway, which is our default too.
m.Timezone, err = time.LoadLocation(aux.Timezone)
if err != nil {
Log(FATAL, "Invalid timezone.")
return errors.New("Invalid timezone")
}
if aux.WALRotateInterval == 0 {
m.WALRotateInterval = 5 // Default of rotate interval of five periods
} else {
m.WALRotateInterval = aux.WALRotateInterval
}
if aux.Queryable != "" {
queryable, err := strconv.ParseBool(aux.Queryable)
if err != nil {
Log(ERROR, "Invalid value: %v for Queryable. Running as queryable...")
} else {
m.Queryable = queryable
}
}
if aux.LogLevel != "" {
switch aux.LogLevel {
case "error":
SetLogLevel(ERROR)
case "warning":
SetLogLevel(WARNING)
case "info":
SetLogLevel(INFO)
}
} else {
SetLogLevel(INFO)
}
if aux.StopGracePeriod > 0 {
m.StopGracePeriod = time.Duration(aux.StopGracePeriod) * time.Second
}
if aux.EnableAdd != "" {
enableAdd, err := strconv.ParseBool(aux.EnableAdd)
if err != nil {
Log(ERROR, "Invalid value: %v for enable_add. Disabling add...", aux.EnableAdd)
} else {
m.EnableAdd = enableAdd
}
}
if aux.EnableRemove != "" {
enableRemove, err := strconv.ParseBool(aux.EnableRemove)
if err != nil {
Log(ERROR, "Invalid value: %v for enable_add. Disabling remove...", aux.EnableRemove)
} else {
m.EnableRemove = enableRemove
}
}
m.EnableLastKnown = false
Log(INFO, "Disabling \"enable_last_known\" feature until it is fixed...")
/*
// Broken - disable for now
if aux.EnableLastKnown != "" {
enableLastKnown, err := strconv.ParseBool(aux.EnableLastKnown)
if err != nil {
Log(ERROR, "Invalid value: %v for enable_last_known. Disabling lastKnown...", aux.EnableLastKnown)
} else {
m.EnableLastKnown = enableLastKnown
}
}
*/
m.RootDirectory = aux.RootDirectory
m.ListenPort = fmt.Sprintf(":%v", aux.ListenPort)
for _, trig := range aux.Triggers {
triggerSetting := &TriggerSetting{
Module: trig.Module,
On: trig.On,
Config: trig.Config,
}
m.Triggers = append(m.Triggers, triggerSetting)
}
for _, bg := range aux.BgWorkers {
bgWorkerSetting := &BgWorkerSetting{
Module: bg.Module,
Name: bg.Name,
Config: bg.Config,
}
m.BgWorkers = append(m.BgWorkers, bgWorkerSetting)
}
return err
}