forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
118 lines (110 loc) · 2.4 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
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
package kratos
import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
)
// App is an application components lifecycle manager
type App struct {
opts options
ctx context.Context
cancel func()
instance *registry.ServiceInstance
log *log.Helper
}
// New create an application lifecycle manager.
func New(opts ...Option) *App {
options := options{
ctx: context.Background(),
logger: log.DefaultLogger,
sigs: []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT},
}
if id, err := uuid.NewUUID(); err == nil {
options.id = id.String()
}
for _, o := range opts {
o(&options)
}
ctx, cancel := context.WithCancel(options.ctx)
return &App{
opts: options,
ctx: ctx,
cancel: cancel,
instance: buildInstance(options),
log: log.NewHelper("app", options.logger),
}
}
// Run executes all OnStart hooks registered with the application's Lifecycle.
func (a *App) Run() error {
a.log.Infow(
"service_id", a.opts.id,
"service_name", a.opts.name,
"version", a.opts.version,
)
g, ctx := errgroup.WithContext(a.ctx)
for _, srv := range a.opts.servers {
srv := srv
g.Go(func() error {
<-ctx.Done() // wait for stop signal
return srv.Stop()
})
g.Go(func() error {
return srv.Start()
})
}
if a.opts.registrar != nil {
if err := a.opts.registrar.Register(a.opts.ctx, a.instance); err != nil {
return err
}
}
c := make(chan os.Signal, 1)
signal.Notify(c, a.opts.sigs...)
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-c:
a.Stop()
}
}
})
if err := g.Wait(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}
// Stop gracefully stops the application.
func (a *App) Stop() error {
if a.opts.registrar != nil {
if err := a.opts.registrar.Deregister(a.opts.ctx, a.instance); err != nil {
return err
}
}
if a.cancel != nil {
a.cancel()
}
return nil
}
func buildInstance(o options) *registry.ServiceInstance {
if len(o.endpoints) == 0 {
for _, srv := range o.servers {
if e, err := srv.Endpoint(); err == nil {
o.endpoints = append(o.endpoints, e)
}
}
}
return ®istry.ServiceInstance{
ID: o.id,
Name: o.name,
Version: o.version,
Metadata: o.metadata,
Endpoints: o.endpoints,
}
}