-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
98 lines (78 loc) · 1.6 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
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"time"
nats "github.com/nats-io/go-nats"
"github.com/robfig/cron"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
log.Fatal("Please specify a config file.")
}
config, err := ReadConfig(args[0])
if err != nil {
log.Fatal(err)
}
queries, err := config.ReadQueries()
if err != nil {
log.Fatal(err)
}
if len(queries) == 0 {
log.Print("No queries found.")
return
}
var nc *nats.Conn
if config.NATS.URL != "" {
nc, err = nats.Connect(config.NATS.URL)
if err != nil {
log.Fatalf("error connecting to nats: %s", err)
}
}
log.Printf("Found %d queries.", len(queries))
cxt, done := context.WithCancel(context.Background())
// Handle signals.
sig := make(chan os.Signal)
signal.Notify(sig, os.Kill, os.Interrupt)
wg := &sync.WaitGroup{}
wg.Add(1)
// Separate thread to stop main thread.
go func() {
defer wg.Done()
// Cancel if signal is received.
select {
case <-sig:
done()
case <-cxt.Done():
}
}()
// No schedule, one-off invocation.
if config.Schedule.Cron == "" {
log.Printf("No schedule specified, running one extract")
if err := Schedule(cxt, nc, config, queries); err != nil {
log.Print(err)
}
done()
} else {
log.Printf("Schedule found: %s", config.Schedule.Cron)
wg.Add(1)
// Create and schedule job.
go func() {
defer wg.Done()
c := cron.NewWithLocation(time.Local)
c.AddFunc(config.Schedule.Cron, func() {
Schedule(cxt, nc, config, queries)
})
c.Start()
// Block
<-cxt.Done()
c.Stop()
}()
}
// Wait to cleanup threads.
wg.Wait()
}