-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (68 loc) · 1.72 KB
/
Copy pathmain.go
File metadata and controls
84 lines (68 loc) · 1.72 KB
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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/deepspace2/plugnpin/pkg/cli"
"github.com/deepspace2/plugnpin/pkg/clients"
"github.com/deepspace2/plugnpin/pkg/config"
"github.com/deepspace2/plugnpin/pkg/logging"
"github.com/deepspace2/plugnpin/pkg/metrics"
"github.com/deepspace2/plugnpin/pkg/processor"
)
var log = logging.GetLogger("main")
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
cliFlags := cli.ParseFlags()
config, err := config.Get()
if err != nil {
log.Error("Failed to load configuration", "error", err)
os.Exit(1)
}
if config.Debug {
logging.SetLevel(logging.DEBUG)
} else {
logging.SetLevel(logging.INFO)
}
if config.RunInterval > 0 {
log.Info(fmt.Sprintf("Will run every %v", config.RunInterval))
}
dockerClients, adguardHomeClient, piholeClient, npmClient, err := clients.GetClients(cliFlags, config)
if err != nil {
os.Exit(1)
}
proc := processor.New(dockerClients, adguardHomeClient, piholeClient, npmClient, cliFlags.DryRun)
defer proc.Shutdown()
if config.RunInterval == 0 {
log.Info("RUN_INTERVAL is 0, will run once")
proc.RunOnce(ctx)
return
}
var wg sync.WaitGroup
if config.Metrics {
errCh := make(chan error, 1)
wg.Go(func() {
metrics.Serve(ctx, config.MetricsServerPort, errCh)
})
if err := <-errCh; err != nil {
log.Error("Failed to start metrics server", "error", err)
stop()
wg.Wait()
os.Exit(1)
}
}
wg.Go(func() {
proc.ListenForEvents(ctx)
})
wg.Go(func() {
proc.RunScheduled(ctx, config.RunInterval)
})
<-ctx.Done()
log.Info("Shutdown signal received, exiting gracefully.")
wg.Wait()
log.Info("Shutdown complete.")
}