-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
59 lines (50 loc) · 1.04 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
package easygo
import (
"context"
"github.com/newpurr/easy-go/boot"
"github.com/newpurr/easy-go/event"
"github.com/newpurr/easy-go/process"
"github.com/newpurr/easy-go/signalc"
"golang.org/x/sync/errgroup"
"log"
"syscall"
)
var (
EventBus = event.BusDefault
)
type Kernel struct {
eg errgroup.Group
b func() []boot.Bootloader
p func() []process.Processer
stopSignal chan struct{}
}
func NewKernel(b func() []boot.Bootloader, p func() []process.Processer) *Kernel {
return &Kernel{
eg: errgroup.Group{},
p: p,
b: b,
stopSignal: make(chan struct{}),
}
}
func (a *Kernel) Start(ctx context.Context) {
go func() {
select {
case <-signalc.Wait(syscall.SIGINT, syscall.SIGTERM):
close(a.stopSignal)
case <-ctx.Done():
close(a.stopSignal)
}
}()
boot.MustBoot(a.b())
for _, processer := range a.p() {
p := processer
a.eg.Go(func() error {
go p.MustStart()
<-a.stopSignal
p.Stop()
return nil
})
}
_ = a.eg.Wait()
log.Println("server has exited.")
}