-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
96 lines (77 loc) · 1.8 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
package main
import (
"flag"
"log"
"os"
"os/signal"
"sync"
"syscall"
"github.com/blind-oracle/psql-streamer/common"
"github.com/blind-oracle/psql-streamer/db"
"github.com/blind-oracle/psql-streamer/sink"
"github.com/blind-oracle/psql-streamer/source"
)
var (
wg sync.WaitGroup
chShutdown = make(chan struct{})
boltdb *db.DB
sources = map[string]source.Source{}
sinks = map[string]sink.Sink{}
commonObjs []common.Common
)
func main() {
var err error
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
flag.StringVar(&cfg.File, "config", "", "Path to configuration file")
flag.Parse()
if err = configLoad(); err != nil {
log.Fatal(err)
}
// Shutdown handling stuff
wg.Add(1)
go handleShutdown()
// Start the sources
for _, s := range sources {
s.Start()
}
if cfg.TickerInterval > 0 {
wg.Add(1)
go tickerWorker()
}
httpInit()
wg.Wait()
log.Println("Service shutdown complete")
}
// Wait for the SIGTERM or Ctrl+C and handle shutdown
func handleShutdown() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, os.Interrupt)
<-sig
log.Printf("Performing graceful shutdown")
httpShutdown()
close(chShutdown)
// Shutdown sources
for sn, s := range sources {
log.Printf("Closing source '%s' ...", sn)
if err := s.Close(); err != nil {
log.Printf("Unable to shutdown source '%s': %s", sn, err)
} else {
log.Printf("Source '%s' closed: %s", sn, s.Stats())
}
}
// Shutdown sinks
for sn, s := range sinks {
log.Printf("Closing sink '%s' ...", sn)
if err := s.Close(); err != nil {
log.Printf("Unable to shutdown sink '%s': %s", sn, err)
} else {
log.Printf("Sink '%s' closed: %s", sn, s.Stats())
}
}
if boltdb != nil {
if err := boltdb.Close(); err != nil {
log.Printf("Unable to close Bolt: %s", err)
}
}
wg.Done()
}