forked from RichardKnop/machinery
/
env.go
58 lines (46 loc) · 1.19 KB
/
env.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
package config
import (
"time"
"github.com/RichardKnop/machinery/v1/log"
"github.com/kelseyhightower/envconfig"
)
// NewFromEnvironment creates a config object from environment variables
func NewFromEnvironment(keepReloading bool) (*Config, error) {
cnf, err := fromEnvironment()
if err != nil {
return nil, err
}
log.INFO.Print("Successfully loaded config from the environment")
if keepReloading {
// Open a goroutine to watch remote changes forever
go func() {
for {
// Delay after each request
time.Sleep(reloadDelay)
// Attempt to reload the config
newCnf, newErr := fromEnvironment()
if newErr != nil {
log.WARNING.Printf("Failed to reload config from the environment: %v", newErr)
continue
}
*cnf = *newCnf
// log.INFO.Printf("Successfully reloaded config from the environment")
}
}()
}
return cnf, nil
}
func fromEnvironment() (*Config, error) {
loadedCnf, cnf := new(Config), new(Config)
*cnf = *defaultCnf
if err := envconfig.Process("", cnf); err != nil {
return nil, err
}
if err := envconfig.Process("", loadedCnf); err != nil {
return nil, err
}
if loadedCnf.AMQP == nil {
cnf.AMQP = nil
}
return cnf, nil
}