-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (64 loc) · 1.7 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/evaafi/go-indexer/config"
"github.com/evaafi/go-indexer/indexer"
)
func main() {
cfg, err := config.LoadConfig("config.yaml")
config.CFG = cfg
if err != nil {
panic(fmt.Sprintf("Cant connect to database: %v", err))
}
db, err := config.GetDBInstance()
if err != nil {
panic(fmt.Sprintf("Cant create database istance: %v", err))
}
tables := []interface{}{
&config.IdxUsers{},
&config.IdxUsersLp{},
&config.IdxUsersAlts{},
&config.IdxLog{},
&config.IdxSyncState{},
}
for _, table := range tables {
if err := db.AutoMigrate(table); err != nil {
panic(fmt.Sprintf("Migration error: %v", err))
}
}
if cfg.ForceResyncOnEveryStart {
fmt.Println("Force resync enabled, truncating all indexing tables...")
for _, table := range tables {
if err := db.Exec(fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE;", config.GetTableName(db, table))).Error; err != nil {
panic(fmt.Sprintf("Failed to truncate table: %v", err))
}
}
fmt.Println("All tables truncated successfully.")
}
config.EnsureInitialIdxSyncStateData(db)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fmt.Println("Start indexing...")
go indexer.RunIndexer(ctx, cfg)
if cfg.Mode == config.ModeLiquidator {
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
log.Println("Received termination signal, stopping application...")
close(indexer.Shutdown)
indexer.WG.Wait()
fmt.Println("Saving queue")
err = indexer.SaveQueue()
if err != nil {
fmt.Printf("Error per saving queue: %s\n", err)
}
cancel()
time.Sleep(3 * time.Second)
}