-
Notifications
You must be signed in to change notification settings - Fork 50
/
module.go
48 lines (40 loc) · 1.03 KB
/
module.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
package status
import (
"context"
"os"
"os/signal"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/kernel"
"github.com/justtrackio/gosoline/pkg/log"
"golang.org/x/sys/unix"
)
type module struct {
kernel.BackgroundModule
kernel.ServiceStage
logger log.Logger
statusManager Manager
sigChan chan os.Signal
}
// NewModule creates a new module that reports the status from the status manager upon receiving SIGUSR1
func NewModule(statusManager Manager) kernel.ModuleFactory {
return func(ctx context.Context, config cfg.Config, logger log.Logger) (kernel.Module, error) {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, unix.SIGUSR1)
return &module{
logger: logger.WithChannel("status"),
statusManager: statusManager,
sigChan: sigChan,
}, nil
}
}
func (m *module) Run(ctx context.Context) error {
defer signal.Stop(m.sigChan)
for {
select {
case <-ctx.Done():
return nil
case <-m.sigChan:
m.statusManager.PrintReport(m.logger)
}
}
}