-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_start.go
44 lines (37 loc) · 886 Bytes
/
app_start.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
package mrserver
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/mondegor/go-webcore/mrlog"
)
const (
signalChanLen = 10
)
func PrepareAppToStart(ctx context.Context) (execute func() error, interrupt func(error)) {
ctx, cancel := context.WithCancel(ctx)
signalStop := make(chan os.Signal, signalChanLen)
signal.Notify(
signalStop,
syscall.SIGABRT,
syscall.SIGQUIT,
syscall.SIGHUP,
os.Interrupt,
syscall.SIGTERM,
)
return func() error {
logger := mrlog.Ctx(ctx)
logger.Info().Msg("Waiting for requests. To exit press CTRL+C")
select {
case signalApp := <-signalStop:
logger.Info().Msgf("Shutting down the application by signal: " + signalApp.String())
return nil
case <-ctx.Done():
logger.Info().Msgf("Shutting down the application by a child process")
return ctx.Err()
}
}, func(err error) {
cancel()
}
}