-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscheduler.go
53 lines (46 loc) · 1.19 KB
/
scheduler.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
package couchbasearray
import (
"errors"
"log"
"time"
)
// StartScheduler starts a scheduling loop
func StartScheduler(servicePath string, timeoutInSeconds int, stop <-chan bool, masterIPPath string) {
for {
currentStates, err := Schedule(servicePath)
if err != nil {
log.Println(err)
}
master, err := GetMasterNode(currentStates)
if err == nil {
ttl := time.Now().Add(time.Duration(timeoutInSeconds+3) * time.Second).UnixNano()
master.TTL = ttl
currentStates[master.SessionID] = master
etcdClient = NewEtcdClient()
if _, err = etcdClient.Set(masterIPPath, master.IPAddress, uint64(timeoutInSeconds)); err != nil {
log.Println(err)
}
}
if err == nil {
err = SaveClusterStates(servicePath, currentStates)
if err != nil {
log.Println(err)
}
}
select {
case <-time.After(time.Duration(timeoutInSeconds) * time.Second):
case <-stop:
log.Println("Stopping scheduling")
}
}
}
// GetMasterNode gets the master node
func GetMasterNode(nodes map[string]NodeState) (NodeState, error) {
var master NodeState
for _, masterState := range nodes {
if masterState.Master {
return masterState, nil
}
}
return master, errors.New("Not found")
}