-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
156 lines (136 loc) · 3.54 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"flag"
"os"
"os/signal"
"sync"
"syscall"
"github.com/dz0ny/wrap2/version"
"github.com/hashicorp/go-reap"
"github.com/robfig/cron"
"github.com/jinzhu/copier"
"go.uber.org/zap"
)
var configLocation string
var loggerLocation string
var showVersion bool
var showDebug bool
var wg sync.WaitGroup
var log *zap.Logger
func init() {
log, _ = zap.NewProduction()
flag.StringVar(&configLocation, "config", "/provision/init.toml", "Location of the init file")
flag.StringVar(&loggerLocation, "logger", "/var/www/mu-plugins/logger.sock", "Location of logger socket")
flag.BoolVar(&showVersion, "version", false, "Show build time and version")
flag.BoolVar(&showDebug, "debug", false, "Show detailed process traces")
}
func main() {
flag.Parse()
if showDebug {
log, _ = zap.NewDevelopment()
}
log.Info(version.String())
if showVersion {
os.Exit(0)
}
cronRunner := cron.New()
ctx, cancel := context.WithCancel(context.Background())
pids := make(reap.PidCh, 1)
errors := make(reap.ErrorCh, 1)
done := make(chan struct{}, 1)
mainHandler := make(chan os.Signal)
signal.Notify(mainHandler, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
sig := <-mainHandler
log.Info("Main interrupt done, canceling workers", zap.String("received", sig.String()))
cancel()
cronRunner.Stop()
done <- struct{}{}
}()
go func() {
for {
select {
case pid := <-pids:
log.Debug("Reaped child process", zap.Int("pid", pid))
case err := <-errors:
log.Error("Error reaping child process", zap.Error(err))
}
}
}()
go reap.ReapChildren(pids, errors, done, nil)
config := NewConfig(configLocation)
if config.PreStart.Command != "" {
config.PreStart.RunBlocking(false, false, ctx)
}
for _, job := range config.Cron {
cj := Cron{}
err := copier.Copy(&cj, &job)
if err != nil {
log.Fatal("Failed", zap.Error(err))
}
if cj.Enabled.IsActive() && !cj.Enabled.IsTrue() {
log.Info(
"Skipping cron",
zap.String("cmd", cj.Command.Command),
zap.Bool("enabled", cj.Enabled.IsTrue()),
zap.String("schedule", cj.Schedule),
)
continue
}
log.Info(
"Scheduling cron",
zap.String("cmd", cj.Command.Command),
zap.String("schedule", cj.Schedule),
zap.Bool("SafeEnv", cj.SafeEnv),
)
if cj.SafeEnv {
if err := cronRunner.AddFunc(cj.Schedule, func() { cj.RunBlockingNonFatalSafeEnv(ctx) }); err != nil {
log.Fatal("Adding cron entry failed", zap.Error(err))
}
} else {
if err := cronRunner.AddFunc(cj.Schedule, func() { cj.RunBlockingNonFatal(ctx) }); err != nil {
log.Fatal("Adding cron entry failed", zap.Error(err))
}
}
}
cronRunner.Start()
for _, proc := range config.Process {
if err := proc.Template.Process(); err != nil {
log.Fatal(
"process config failed",
zap.String("cmd", proc.Command),
zap.Error(err),
)
}
}
for idx := range config.Process {
proc := config.Process[idx]
log.Info(
"Command enabler",
zap.Bool("isActive", proc.Enabled.IsActive()),
zap.String("operator", proc.Enabled.Operator),
)
if proc.Enabled.IsActive() && !proc.Enabled.IsTrue() {
log.Info(
"Command not enabled",
zap.String("cmd", proc.Command),
)
continue
}
proc.Run(ctx, false)
}
if config.PostStart.Command != "" {
wg.Add(1)
go func() {
defer wg.Done()
config.PostStart.RunBlocking(false, false, ctx)
}()
}
log.Info("Staring logger socket", zap.String("location", loggerLocation))
unixlog := NewUnixLogger(loggerLocation)
go unixlog.Serve()
log.Info("Event loop ready")
wg.Wait()
os.Exit(0)
}