...
1
18
19 package db
20
21 import (
22 "context"
23 "discovery/config"
24 "discovery/log"
25 "discovery/types"
26 "fmt"
27 "time"
28 )
29
30
31 func DeclarePollFailed(machine *types.Machine) {
32 machine.DeadPolls++
33
34
35 if !machine.Alive {
36 log.Log.Warningf("Deleting machine %s since already dead", machine.IP)
37 return
38 }
39
40
41 if machine.DeadPolls >= config.GetMachineDeadPollsRemovingThreshold() {
42 machine.Alive = false
43 }
44 machine.LastUpdate = time.Now().Unix()
45
46 _, err := MachineUpdate(machine)
47 if err != nil {
48 log.Log.Warningf("Could not update the machine %s", machine.IP)
49 }
50 log.Log.Debugf("Poll for machine %s failed", machine.IP)
51 }
52
53
54 func DeclarePollSucceeded(machine *types.Machine, ping float64) {
55 machine.Ping = ping
56 machine.Alive = true
57 machine.DeadPolls = 0
58 machine.LastUpdate = time.Now().Unix()
59
60 _, err := MachineUpdate(machine)
61 if err != nil {
62 log.Log.Warningf("Could not update the machine %s", machine.IP)
63 }
64 log.Log.Debugf("Poll for machine %s succeeded", machine.IP)
65 }
66
67
70
71 func getDatabaseDirPath() string {
72 return config.GetDataPath()
73 }
74
75 func getDatabaseFilePath() string {
76 return getDatabaseDirPath() + "/" + DatabaseName
77 }
78
79 func executeTransaction(query string, args ...interface{}) error {
80 tx, err := db.Begin()
81 if err != nil {
82 return fmt.Errorf("cannot begin transaction: %s", err.Error())
83 }
84
85 stmt, err := db.Prepare(query)
86 if err != nil {
87 return fmt.Errorf("cannot prepare query: %s", err.Error())
88 }
89
90 _, err = stmt.ExecContext(context.Background(), query, args)
91 if err != nil {
92 return fmt.Errorf("cannot execute query: %s", err.Error())
93 }
94
95 err = tx.Commit()
96 if err != nil {
97 return fmt.Errorf("cannot commit query: %s", err.Error())
98 }
99
100 return nil
101 }
102
View as plain text