forked from RichardKnop/machinery
/
file.go
82 lines (66 loc) · 1.68 KB
/
file.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
package config
import (
"fmt"
"os"
"time"
"github.com/orange1fruit/machinery/v2/log"
"gopkg.in/yaml.v2"
)
// NewFromYaml creates a config object from YAML file
func NewFromYaml(cnfPath string, keepReloading bool) (*Config, error) {
cnf, err := fromFile(cnfPath)
if err != nil {
return nil, err
}
log.INFO.Printf("Successfully loaded config from file %s", cnfPath)
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 := fromFile(cnfPath)
if newErr != nil {
log.WARNING.Printf("Failed to reload config from file %s: %v", cnfPath, newErr)
continue
}
*cnf = *newCnf
}
}()
}
return cnf, nil
}
// ReadFromFile reads data from a file
func ReadFromFile(cnfPath string) ([]byte, error) {
file, err := os.Open(cnfPath)
// Config file not found
if err != nil {
return nil, fmt.Errorf("Open file error: %s", err)
}
// Config file found, let's try to read it
data := make([]byte, 1000)
count, err := file.Read(data)
if err != nil {
return nil, fmt.Errorf("Read from file error: %s", err)
}
return data[:count], nil
}
func fromFile(cnfPath string) (*Config, error) {
loadedCnf, cnf := new(Config), new(Config)
*cnf = *defaultCnf
data, err := ReadFromFile(cnfPath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(data, cnf); err != nil {
return nil, fmt.Errorf("Unmarshal YAML error: %s", err)
}
if err := yaml.Unmarshal(data, loadedCnf); err != nil {
return nil, fmt.Errorf("Unmarshal YAML error: %s", err)
}
if loadedCnf.AMQP == nil {
cnf.AMQP = nil
}
return cnf, nil
}