-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
198 lines (160 loc) · 3.7 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
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
187
188
189
190
191
192
193
194
195
196
197
198
package toho
import (
"context"
"errors"
"fmt"
"log/slog"
"reflect"
"sync"
"time"
"github.com/go-toho/toho/app"
"github.com/go-toho/toho/pkg/xos"
)
// DefaultTimeout is the default timeout for starting or stopping an
// application. It can be configured with the StartTimeout and StopTimeout
// options.
const DefaultTimeout = 15 * time.Second
var (
errAlreadyStarted = errors.New("already started")
errNotStarted = errors.New("not started")
)
// TohoApp defines a application components lifecycle manager.
// [C] defines type of the configuration struct for the application.
// [L] defines type of the logger for the application.
type TohoApp[C, L any] struct {
opts options
ctx context.Context
cancel func()
appInfo *app.App
mu sync.Mutex
core Core
bootstrap bool
config C
log L
}
func New(opts ...Option) *TohoApp[struct{}, *slog.Logger] {
return NewCL[struct{}, *slog.Logger](opts...)
}
func NewC[C any](opts ...Option) *TohoApp[C, *slog.Logger] {
return NewCL[C, *slog.Logger](opts...)
}
func NewL[L any](opts ...Option) *TohoApp[struct{}, L] {
return NewCL[struct{}, L](opts...)
}
func NewCL[C, L any](opts ...Option) *TohoApp[C, L] {
o := options{
ctx: context.Background(),
core: &defaultCore{},
startTimeout: DefaultTimeout,
stopTimeout: DefaultTimeout,
}
for _, opt := range opts {
opt(&o)
}
ctx, cancel := context.WithCancel(o.ctx)
return &TohoApp[C, L]{
opts: o,
ctx: ctx,
cancel: cancel,
appInfo: app.New(o.appInfoOpts...),
core: o.core,
}
}
func (a *TohoApp[C, L]) AppInfo() app.Info {
return a.appInfo
}
func (a *TohoApp[C, L]) Config() C {
return a.config
}
func (a *TohoApp[C, L]) Logger() L {
return a.log
}
// Start executes all OnStart hooks registered with the application's Lifecycle.
func (a *TohoApp[C, L]) Start() error {
a.mu.Lock()
if a.bootstrap {
return errAlreadyStarted
}
if a.opts.logger != nil {
// handle manually configured logger
if log, ok := a.opts.logger.(L); ok {
a.log = log
}
}
coreOpts := &CoreOptions{
App: *a.appInfo,
ConfigPointer: &a.config,
LogPointer: &a.log,
Options: a.opts.options,
StartTimeout: a.opts.startTimeout,
StopTimeout: a.opts.stopTimeout,
}
if err := a.core.Init(coreOpts); err != nil {
return fmt.Errorf("%s: %w", reflect.TypeOf(a.core), err)
}
// fallback logger
if reflect.ValueOf(a.log).IsNil() {
if _, ok := any(a.log).(*slog.Logger); ok {
a.log = any(slog.Default()).(L)
}
}
a.bootstrap = true
a.mu.Unlock()
if err := callLifecycleFn(a.ctx, a.opts.beforeStart); err != nil {
return err
}
if err := a.core.Start(a.ctx); err != nil {
return err
}
if err := callLifecycleFn(a.ctx, a.opts.afterStart); err != nil {
return err
}
return nil
}
// Stop gracefully stops the application.
func (a *TohoApp[C, L]) Stop() error {
a.mu.Lock()
if !a.bootstrap {
return errNotStarted
}
a.mu.Unlock()
if err := callLifecycleFn(a.ctx, a.opts.beforeStop); err != nil {
return err
}
if err := a.core.Stop(a.ctx); err != nil {
return err
}
if a.cancel != nil {
a.cancel()
}
if err := callLifecycleFn(a.ctx, a.opts.afterStop); err != nil {
return err
}
return nil
}
// Wait blocks application until termination.
func (a *TohoApp[C, L]) Wait() <-chan error {
a.mu.Lock()
defer a.mu.Unlock()
ch := make(chan error, 1)
if !a.bootstrap {
ch <- errNotStarted
return ch
}
go func() {
signal := <-a.core.Wait()
ch <- xos.SignalError{Signal: signal}
}()
return ch
}
func callLifecycleFn(ctx context.Context, fns []func(context.Context) error) error {
for _, fn := range fns {
if fn == nil {
continue
}
if err := fn(ctx); err != nil {
return err
}
}
return nil
}