-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (70 loc) · 1.42 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
package main
import (
"context"
"flag"
"os"
"os/signal"
"syscall"
"time"
"github.com/erkkah/letarette/pkg/logger"
sslr "github.com/erkkah/sslr/internal"
)
var args struct {
configFile string
continuous bool
}
func main() {
flag.StringVar(&args.configFile, "cfg", "sslr.json", "SSLR config file")
flag.BoolVar(&args.continuous, "c", false, "Run continuously")
flag.Parse()
config, err := sslr.LoadConfig(args.configFile)
if err != nil {
logger.Error.Printf("Failed to load config: %v\n", err)
os.Exit(1)
}
ctx, cancel := context.WithCancel(context.Background())
job, err := sslr.NewJob(ctx, config)
if err != nil {
logger.Error.Printf("Failed to create job: %v\n", err)
os.Exit(2)
}
done := make(chan struct{})
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
var jobError error
go func() {
runLoop:
for {
jobError = job.Run()
if jobError != nil {
break runLoop
}
if !args.continuous {
break runLoop
}
select {
case <-time.After(config.WaitBetweenJobs):
break
case <-ctx.Done():
break runLoop
}
}
cancel()
close(done)
}()
interrupted := false
select {
case s := <-signals:
logger.Info.Printf("Received signal %v\n", s)
interrupted = true
break
case <-ctx.Done():
break
}
if jobError != nil && !interrupted {
logger.Error.Printf("Job failed: %v\n", jobError)
os.Exit(3)
}
cancel()
<-done
}