forked from vevsatechnologies/dcrextdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (145 loc) · 3.44 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
DisableLevelTruncation: true,
TimestampFormat: "2006-01-02 15:04:05",
})
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
}
func mainCore() error {
cfg, err := loadConfig()
if err != nil {
log.Fatal("Unable to load config: ", err)
}
if cfg.Quiet {
log.SetLevel(log.ErrorLevel)
}
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
cfg.DBHost, cfg.DBPort, cfg.DBUser, cfg.DBPass, cfg.DBName)
db, err := NewPgDb(psqlInfo)
defer db.Close()
err = db.Ping()
if err != nil {
db.Close()
log.Fatal("Error connecting to Postgresl: ", err)
}
if cfg.DropTables {
log.Info("Dropping tables")
err = db.DropExchangeDataTable()
if err != nil {
db.Close()
log.Fatal("Could not drop tables: ", err)
} else {
log.Info("Tables dropped")
return err
}
}
log.Info("Attemping to retrieve exchange data")
data := make([]exchangeDataTick, 0)
if exists := db.ExchangeDataTableExits(); exists {
t, err := db.LastExchangeEntryTime()
if err != nil {
if strings.Contains(err.Error(), "no rows") {
t = 0
} else {
log.Error("Could not retrieve last entry time: ", err)
return err
}
}
log.Info("Retireving exchange data from ", time.Unix(t, 0).String())
if d, err := collectExchangeData(t); err == nil {
data = d
} else {
log.Error("Could not retrieve exchange data: ", err)
return err
}
} else {
log.Info("Creating new exchange data table")
if err := db.CreateExchangeDataTable(); err != nil {
log.Error("Error creating exchange data table: ", err)
return err
}
log.Info("Retrieving exchange data")
if d, err := collectExchangeData(0); err == nil {
data = d
} else {
log.Error("Could not retrieve exchange data: ", err)
return err
}
}
log.Debug("Collected exchange entry count: ", len(data))
err = db.AddExchangeData(data)
if err != nil {
log.Error("Error adding exchange entries: ", err)
return err
}
log.Info("Collected entries stored")
quit := make(chan struct{})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
signal.Stop(c)
log.Info("CTRL+C hit. Closing goroutines.")
close(quit)
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
last, err := db.LastExchangeEntryTime()
if err != nil {
log.Error("Could not retrieve last entry time ", err)
wg.Done()
return
}
// Sleep till 30 seconds before next collection time
time.Sleep(time.Duration(last+1730-time.Now().Unix()) * time.Second)
ticker := time.NewTicker(time.Second * time.Duration(1800)) // Set a timer for every 30 minutes
defer func() {
ticker.Stop()
wg.Done()
}()
log.Info("Starting collector")
for {
select {
case t := <-ticker.C:
log.Info("Collecting recent exchange data")
data, err := collectExchangeData(last)
last = t.Unix()
if err != nil {
log.Error("Could not retrieve exchange data: ", err)
return
}
err = db.AddExchangeData(data)
if err != nil {
log.Error("Error adding exchange data entries: ", err)
return
}
log.Info("Added recent exchange data")
case <-quit:
log.Info("Closing collector")
return
}
}
}()
wg.Wait()
return nil
}
func main() {
if err := mainCore(); err != nil {
os.Exit(1)
}
os.Exit(0)
}