...
1
18
19
20 package watcher
21
22 import (
23 "discovery/config"
24 "discovery/db"
25 "discovery/log"
26 "discovery/types"
27 "encoding/json"
28 "time"
29 )
30
31
32
33 func init() {
34
45 }
46
47 func PollingLooper() {
48 for {
49
50 if config.GetMachineIp() == "" {
51 log.Log.Warningf("Machine has not configured its IP, service is idle. Retrying in 30 seconds...")
52 time.Sleep(30 * time.Second)
53 continue
54 }
55
56 machinesToPoll, err := db.MachinesGetAliveAndSuspected()
57 if err != nil {
58 log.Log.Debugf("Cannot get machines to poll, retrying in 30 seconds")
59 time.Sleep(30 * time.Second)
60 continue
61 }
62
63 log.Log.Debugf("Starting poll for %d machines", len(machinesToPoll))
64 for _, m := range machinesToPoll {
65 log.Log.Debugf("Polling machine %s", m.IP)
66
67
68 if m.IP == config.GetMachineIp() {
69
70 log.Log.Infof("Removing current machine '%s' from entry list", m.IP)
71 err = db.MachineRemove(m.IP)
72 if err != nil {
73 log.Log.Errorf("Cannot remove self machine entry in list: %s", err)
74 }
75 continue
76 }
77
78
79 ping, err := pollMachine(&m)
80
81
82 if err != nil {
83 db.DeclarePollFailed(&m)
84 } else {
85 db.DeclarePollSucceeded(&m, ping.Seconds())
86 }
87 }
88
89 time.Sleep(time.Duration(config.GetPollTime()) * time.Second)
90 }
91 }
92
93
94 func pollMachine(machine *types.Machine) (*time.Duration, error) {
95
96 startTime := time.Now()
97 res, err := GetForPoll(machine.IP)
98 elapsedTime := time.Since(startTime)
99 if err != nil {
100 log.Log.Debugf("Error while polling machine %s: %s", machine.IP, err.Error())
101 return nil, err
102 }
103
104
105
106 if res.Header.Get(config.GetParamIp) != machine.IP {
107 answeringMachine, err := db.MachineGet(machine.IP)
108 if err == nil && answeringMachine != nil {
109 answeringMachine.IP = res.Header.Get(config.GetParamIp)
110 answeringMachine.Name = res.Header.Get(config.GetParamName)
111 answeringMachine.GroupName = res.Header.Get(config.GetParamGroupName)
112 _, _ = db.MachineUpdate(answeringMachine)
113 }
114 }
115
116
117 machine.Name = res.Header.Get(config.GetParamName)
118 machine.GroupName = res.Header.Get(config.GetParamGroupName)
119
120
121 var machines []types.Machine
122 err = json.NewDecoder(res.Body).Decode(&machines)
123 _ = res.Body.Close()
124 if err != nil {
125 log.Log.Debugf("Error while parsing polled machine %s response: %s", machine.IP, err.Error())
126 return nil, err
127 }
128
129 for _, m := range machines {
130 err = db.MachineAdd(&m, true)
131 }
132
133 return &elapsedTime, nil
134 }
135
View as plain text