-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
186 lines (154 loc) · 3.44 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/yankeguo/minit/internal/menv"
"github.com/yankeguo/minit/internal/mexec"
"github.com/yankeguo/minit/internal/mlog"
"github.com/yankeguo/minit/internal/mrunners"
"github.com/yankeguo/minit/internal/msetups"
"github.com/yankeguo/minit/internal/munit"
"github.com/yankeguo/rg"
)
var (
AppVersion = "unknown"
)
func exit(err *error) {
if *err == nil {
return
}
_, _ = fmt.Fprintf(os.Stderr, "%s: exited with error: %s\n", "minit", (*err).Error())
os.Exit(1)
}
func envStr(key string, out *string) {
if val := strings.TrimSpace(os.Getenv(key)); val != "" {
*out = val
}
}
func envBool(key string, out *bool) {
if val := strings.TrimSpace(os.Getenv(key)); val != "" {
*out, _ = strconv.ParseBool(val)
}
}
func main() {
var err error
defer exit(&err)
defer rg.Guard(&err)
var (
optPprofPort = ""
optUnitDir = "/etc/minit.d"
optLogDir = ""
optQuickExit bool
)
// pprof
if envStr("MINIT_PPROF_PORT", &optPprofPort); optPprofPort != "" {
go func() {
_ = http.ListenAndServe(":"+optPprofPort, nil)
}()
}
envStr("MINIT_UNIT_DIR", &optUnitDir)
envStr("MINIT_LOG_DIR", &optLogDir)
envBool("MINIT_QUICK_EXIT", &optQuickExit)
log := rg.Must(mlog.CreateSimpleLogger(optLogDir, "minit", "minit: "))
exem := mexec.NewManager()
log.Print("starting (" + AppVersion + ")")
// run through setups
rg.Must0(msetups.Setup(log))
// load units
units, skips := rg.Must2(
munit.Load(
munit.LoadOptions{
Args: os.Args[1:],
Env: menv.Environ(),
Dirs: munit.ParseUnitDirPattern(optUnitDir),
},
),
)
for _, skip := range skips {
log.Print("unit skipped: " + skip.Name)
}
// load runners
var (
runnersS []mrunners.Runner
runnersL []mrunners.Runner
)
{
var runners []mrunners.Runner
// convert units to runners
for _, unit := range units {
runners = append(
runners,
rg.Must(mrunners.Create(mrunners.RunnerOptions{
Unit: unit,
Exec: exem,
Logger: rg.Must(mlog.CreateSimpleLogger(optLogDir, unit.Name, "")),
})),
)
}
// split short runners and long runners
for _, runner := range runners {
if runner.Long {
runnersL = append(runnersL, runner)
} else {
runnersS = append(runnersS, runner)
}
}
}
// execute short runners
for _, runner := range runnersS {
if err = runner.Action.Do(context.Background()); err != nil {
return
}
}
// quick exit
if len(runnersL) == 0 && optQuickExit {
log.Printf("no long runners and MINIT_QUICK_EXIT is set")
return
}
// run long runners
ctx, cancel := context.WithCancel(context.Background())
wg := &sync.WaitGroup{}
chErr := make(chan error, 1)
for _, runner := range runnersL {
wg.Add(1)
go func(runner mrunners.Runner) {
if err := runner.Action.Do(ctx); err != nil {
select {
case chErr <- err:
default:
}
}
wg.Done()
}(runner)
}
// wait for signals
chSig := make(chan os.Signal, 1)
signal.Notify(chSig, syscall.SIGINT, syscall.SIGTERM)
var sig os.Signal
select {
case sig = <-chSig:
log.Printf("signal caught: %s", sig.String())
case err = <-chErr:
log.Printf("critical error caught: %s", err.Error())
}
if sig == nil {
sig = syscall.SIGTERM
}
// shutdown context
cancel()
// delay 3 seconds
time.Sleep(time.Second * 3)
// broadcast signals
exem.Signal(sig)
// wait for long runners
wg.Wait()
}