forked from nikepan/clickhouse-bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (73 loc) · 1.92 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
package main
import (
"context"
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
)
type clickhouseConfig struct {
Servers []string `json:"servers"`
DownTimeout int `json:"down_timeout"`
}
type config struct {
Listen string `json:"listen"`
Clickhouse clickhouseConfig `json:"clickhouse"`
FlushCount int `json:"flush_count"`
FlushInterval int `json:"flush_interval"`
DumpDir string `json:"dump_dir"`
Debug bool `json:"debug"`
}
func safeQuit(collect *Collector, sender Sender) {
collect.FlushAll()
if count := sender.Len(); count > 0 {
log.Printf("Sending %+v tables\n", count)
}
for !sender.Empty() && !collect.Empty() {
collect.WaitFlush()
}
collect.WaitFlush()
os.Exit(1)
}
func main() {
log.SetOutput(os.Stdout)
configFile := flag.String("config", "config.json", "config file (json)")
flag.Parse()
cnf := config{}
err := ReadJSON(*configFile, &cnf)
if err != nil {
log.Printf("Config file %+v not found. Use config.sample.json\n", *configFile)
err := ReadJSON("config.sample.json", &cnf)
if err != nil {
log.Fatalf("Read config: %+v\n", err.Error())
}
}
dumper := new(FileDumper)
dumper.Path = cnf.DumpDir
sender := NewClickhouse(cnf.Clickhouse.DownTimeout)
sender.Dumper = dumper
for _, url := range cnf.Clickhouse.Servers {
sender.AddServer(url)
}
collect := NewCollector(sender, cnf.FlushCount, cnf.FlushInterval)
// send collected data on SIGTERM and exit
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
srv := InitServer(cnf.Listen, collect, cnf.Debug)
_, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
go func() {
for {
_ = <-signals
log.Printf("STOP signal\n")
safeQuit(collect, sender)
}
}()
err = srv.Start()
if err != nil {
log.Printf("ListenAndServe: %+v\n", err)
safeQuit(collect, sender)
}
}