-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
206 lines (145 loc) · 4.41 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
199
200
201
202
203
204
205
206
package app
import (
"context"
"os"
"github.com/getsentry/sentry-go"
"github.com/grongor/panicwatch"
"go.uber.org/atomic"
"go.uber.org/zap"
)
var (
Release string
instance = &Application{}
)
type Application struct {
started atomic.Bool
terminationSignals []os.Signal
extensions []Extension
sentryConfig func(*sentry.ClientOptions)
zapConfig func(*zap.Config)
panicwatchConfig func(*panicwatch.Config, *zap.SugaredLogger)
config *genericConfig
}
func (app *Application) WithConfig(config interface{}) *Application {
app.checkStarted(false)
return app.WithExtensions(&appConfigExtension{config: config})
}
func (app *Application) WithTerminationSignals(signals ...os.Signal) *Application {
app.checkStarted(false)
app.terminationSignals = append(app.terminationSignals, signals...)
return app
}
func (app *Application) WithExtensions(extensions ...Extension) *Application {
app.checkStarted(false)
app.extensions = append(app.extensions, extensions...)
return app
}
func (app *Application) WithZapConfig(zapConfig func(*zap.Config)) *Application {
app.checkStarted(false)
app.zapConfig = zapConfig
return app
}
func (app *Application) WithSentryConfig(sentryConfig func(*sentry.ClientOptions)) *Application {
app.checkStarted(false)
app.sentryConfig = sentryConfig
return app
}
func (app *Application) WithPanicwatchConfig(
panicwatchConfig func(*panicwatch.Config, *zap.SugaredLogger),
) *Application {
app.checkStarted(false)
app.panicwatchConfig = panicwatchConfig
return app
}
func (app *Application) Run(appCallback func(appCtx context.Context, logger *zap.SugaredLogger)) {
app.checkStarted(true)
app.config = &genericConfig{}
app.config.Load(app.extensions, app.zapConfig, app.sentryConfig, app.panicwatchConfig)
logger := app.config.logger
defer logger.Sync()
appCtx := NewCliContext(app.terminationSignals...)
for _, extension := range app.extensions {
extension.Start(appCtx, logger)
}
appFinishedCh := make(chan struct{})
appCtx.StartWorker(func() {
logger.Info("running the application")
appCallback(appCtx.ctx, logger)
close(appFinishedCh)
appCtx.Shutdown()
})
signalLoggerCh := make(chan struct{})
go func() {
select {
case <-appFinishedCh:
case <-appCtx.Done():
select {
case <-appFinishedCh:
default:
logger.Info("received termination signal, application should finish soon")
}
}
close(signalLoggerCh)
}()
appCtx.WaitForWorkers()
<-signalLoggerCh
logger.Info("application finished, exiting now")
}
func (app *Application) Start(appCallback func(appCtx Context, logger *zap.SugaredLogger)) {
app.checkStarted(true)
app.config = &genericConfig{}
app.config.Load(app.extensions, app.zapConfig, app.sentryConfig, app.panicwatchConfig)
logger := app.config.logger
defer logger.Sync()
appCtx := NewCliContext(app.terminationSignals...)
for _, extension := range app.extensions {
extension.Start(appCtx, logger)
}
appCtx.StartWorker(func() {
appCallback(appCtx, logger)
logger.Info("application started")
})
shutdownLoggerCh := make(chan struct{})
appCtx.StartWorker(func() {
<-appCtx.Done()
logger.Info("application is shutting down")
close(shutdownLoggerCh)
})
appCtx.WaitForWorkers()
<-shutdownLoggerCh
logger.Info("application finished, exiting now")
}
func (app *Application) checkStarted(doStart bool) {
if doStart {
if app.started.CAS(false, true) {
return
}
} else if !app.started.Load() {
return
}
panic("application has already been started")
}
func WithConfig(config interface{}) *Application {
return instance.WithConfig(config)
}
func WithTerminationSignals(signals ...os.Signal) *Application {
return instance.WithTerminationSignals(signals...)
}
func WithExtensions(extensions ...Extension) *Application {
return instance.WithExtensions(extensions...)
}
func WithZapConfig(zapConfig func(*zap.Config)) *Application {
return instance.WithZapConfig(zapConfig)
}
func WithSentryConfig(sentryConfig func(*sentry.ClientOptions)) *Application {
return instance.WithSentryConfig(sentryConfig)
}
func WithPanicwatchConfig(panicwatchConfig func(*panicwatch.Config, *zap.SugaredLogger)) *Application {
return instance.WithPanicwatchConfig(panicwatchConfig)
}
func Run(appCallback func(appCtx context.Context, logger *zap.SugaredLogger)) {
instance.Run(appCallback)
}
func Start(appCallback func(appCtx Context, logger *zap.SugaredLogger)) {
instance.Start(appCallback)
}