forked from lucaslorentz/caddy-supervisor
-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.go
71 lines (54 loc) · 1.24 KB
/
app.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
package supervisor
import (
"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"
)
// Interface guards
var (
_ caddy.App = (*App)(nil)
_ caddy.Module = (*App)(nil)
_ caddy.Provisioner = (*App)(nil)
)
func init() {
caddy.RegisterModule(App{})
}
type App struct {
Supervise []Definition `json:"supervise,omitempty"`
log *zap.Logger
supervisors []*Supervisor
}
// CaddyModule implements caddy.Module
func (a App) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "supervisor",
New: func() caddy.Module { return new(App) },
}
}
// Provision implements caddy.Provisioner
func (a *App) Provision(context caddy.Context) error {
a.log = context.Logger(a)
for _, definition := range a.Supervise {
supervisors, err := definition.ToSupervisors(a.log)
if err != nil {
return err
}
a.supervisors = append(a.supervisors, supervisors...)
}
a.log.Debug("module provisioned", zap.Any("supervisors", a.supervisors))
return nil
}
// Start implements caddy.App
func (a *App) Start() error {
for _, s := range a.supervisors {
go s.Run()
}
a.log.Debug("module started")
return nil
}
// Stop implements caddy.App
func (a *App) Stop() error {
for _, s := range a.supervisors {
s.Stop()
}
return nil
}