-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyncer.go
113 lines (99 loc) · 2.58 KB
/
syncer.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
package main
import (
"fmt"
"log"
"strconv"
"github.com/go-redis/redis"
)
var stopLooper chan bool
func fetchVersion(config *ReuniAgentConfiguration) *Configuration {
helper := &HttpHelper{
URL: getFetchVersionURL(config),
Method: "GET",
Authorization: config.Authorization,
}
var data Configuration
log.Println("Fetching Version...")
err := fetchData(helper, &data)
if err != nil {
log.Println("Error fetching version:" + err.Error())
return nil
}
log.Printf("Succesfully fetching version: %v", data.Version)
return &data
}
func fetchConfiguration(config *ReuniAgentConfiguration, version int) *Configuration {
helper := &HttpHelper{
URL: getFetchConfigurationURL(config, version),
Method: "GET",
Authorization: config.Authorization,
}
var data Configuration
log.Println("Fetching Configuration...")
err := fetchData(helper, &data)
if err != nil {
log.Println("Error fetching configuration:" + err.Error())
return nil
}
serviceConfig = &data
log.Printf("Succesfully fetching configuration: %v", data)
return &data
}
func isNeedUpdate(current, expected int) bool {
return current != expected
}
func handleSync(pubSubClient *redis.PubSub) {
// Wait for confirmation that subscription is created before publishing anything.
_, err := pubSubClient.Receive()
if err != nil {
panic(err)
}
// Go channel which receives messages.
ch := pubSubClient.Channel()
getLatestConfig(fetchVersion(agentConfig).Version)
// go func() {
for {
msg, ok := <-ch
if !ok {
break
}
fmt.Println(msg.Channel, msg.Payload)
log.Print("Configuration need to be updated")
version, err := strconv.Atoi(msg.Payload)
if err != nil {
log.Println("ERROR PARSING VERSION")
} else {
getLatestConfig(version)
}
}
// }()
}
func getLatestConfig(version int) {
// version := fetchVersion(agentConfig)
// if isNeedUpdate(serviceConfig.Version, version.Version) {
log.Print("Configuration need to be updated")
configuration := fetchConfiguration(agentConfig, version)
log.Println("Set configuration to environment")
configurationSetter(configuration)
log.Println("Configuration setted to environment")
if start {
stopChannel <- true
}
go runnerStart(agentConfig)
// } else {
// log.Print("Configuration still up to date")
// }
}
// func startLooper(pubSubClient *redis.PubSub) {
// stopLooper = make(chan bool)
// for {
// handleSync(pubSubClient)
// select {
// case _ = <-stopLooper:
// log.Println("Stopping Looper")
// break
// default:
// time.Sleep(time.Duration(agentConfig.Interval) * time.Second)
// }
// }
// }