-
Notifications
You must be signed in to change notification settings - Fork 460
/
server_service.go
75 lines (60 loc) · 2 KB
/
server_service.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/cortex/server_service.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package mimir
import (
"context"
"fmt"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/server"
"github.com/grafana/dskit/services"
util_log "github.com/grafana/mimir/pkg/util/log"
)
// NewServerService constructs service from Server component.
// servicesToWaitFor is called when server is stopping, and should return all
// services that need to terminate before server actually stops.
// N.B.: this function is NOT Mimir specific, please let's keep it that way.
// Passed server should not react on signals. Early return from Run function is considered to be an error.
func NewServerService(serv *server.Server, servicesToWaitFor func() []services.Service) services.Service {
serverDone := make(chan error, 1)
runFn := func(ctx context.Context) error {
go func() {
defer close(serverDone)
serverDone <- serv.Run()
}()
select {
case <-ctx.Done():
return nil
case err := <-serverDone:
if err != nil {
return err
}
return fmt.Errorf("server stopped unexpectedly")
}
}
stoppingFn := func(_ error) error {
// wait until all modules are done, and then shutdown server.
for _, s := range servicesToWaitFor() {
_ = s.AwaitTerminated(context.Background())
}
// shutdown HTTP and gRPC servers (this also unblocks Run)
serv.Shutdown()
// if not closed yet, wait until server stops.
<-serverDone
level.Info(util_log.Logger).Log("msg", "server stopped")
return nil
}
return services.NewBasicService(nil, runFn, stoppingFn)
}
// DisableSignalHandling puts a dummy signal handler
func DisableSignalHandling(config *server.Config) {
config.SignalHandler = make(ignoreSignalHandler)
}
type ignoreSignalHandler chan struct{}
func (dh ignoreSignalHandler) Loop() {
<-dh
}
func (dh ignoreSignalHandler) Stop() {
close(dh)
}