Skip to content

Commit 86b546c

Browse files
committed
refactor(main): refactoring main grafana server / startup code
1 parent 2b8177e commit 86b546c

File tree

3 files changed

+138
-63
lines changed

3 files changed

+138
-63
lines changed

pkg/cmd/grafana-server/main.go

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"flag"
65
"fmt"
76
"io/ioutil"
@@ -13,21 +12,11 @@ import (
1312
"syscall"
1413
"time"
1514

16-
"golang.org/x/sync/errgroup"
17-
1815
"github.com/grafana/grafana/pkg/log"
19-
"github.com/grafana/grafana/pkg/login"
20-
"github.com/grafana/grafana/pkg/metrics"
21-
"github.com/grafana/grafana/pkg/plugins"
22-
"github.com/grafana/grafana/pkg/services/cleanup"
23-
"github.com/grafana/grafana/pkg/services/eventpublisher"
24-
"github.com/grafana/grafana/pkg/services/notifications"
25-
"github.com/grafana/grafana/pkg/services/search"
16+
"github.com/grafana/grafana/pkg/models"
2617
"github.com/grafana/grafana/pkg/services/sqlstore"
2718
"github.com/grafana/grafana/pkg/setting"
28-
"github.com/grafana/grafana/pkg/social"
2919

30-
"github.com/grafana/grafana/pkg/services/alerting"
3120
_ "github.com/grafana/grafana/pkg/services/alerting/conditions"
3221
_ "github.com/grafana/grafana/pkg/services/alerting/notifiers"
3322
_ "github.com/grafana/grafana/pkg/tsdb/graphite"
@@ -66,41 +55,8 @@ func main() {
6655
setting.BuildCommit = commit
6756
setting.BuildStamp = buildstampInt64
6857

69-
appContext, shutdownFn := context.WithCancel(context.Background())
70-
grafanaGroup, appContext := errgroup.WithContext(appContext)
71-
72-
go listenToSystemSignals(shutdownFn, grafanaGroup)
73-
74-
flag.Parse()
75-
writePIDFile()
76-
77-
initRuntime()
78-
initSql()
79-
metrics.Init()
80-
search.Init()
81-
login.Init()
82-
social.NewOAuthService()
83-
eventpublisher.Init()
84-
plugins.Init()
85-
86-
// init alerting
87-
if setting.AlertingEnabled {
88-
engine := alerting.NewEngine()
89-
grafanaGroup.Go(func() error { return engine.Run(appContext) })
90-
}
91-
92-
// cleanup service
93-
cleanUpService := cleanup.NewCleanUpService()
94-
grafanaGroup.Go(func() error { return cleanUpService.Run(appContext) })
95-
96-
if err := notifications.Init(); err != nil {
97-
log.Fatal(3, "Notification service failed to initialize", err)
98-
}
99-
100-
exitCode := StartServer()
101-
102-
grafanaGroup.Wait()
103-
exitChan <- exitCode
58+
server := NewGrafanaServer()
59+
server.Start()
10460
}
10561

10662
func initRuntime() {
@@ -143,26 +99,16 @@ func writePIDFile() {
14399
}
144100
}
145101

146-
func listenToSystemSignals(cancel context.CancelFunc, grafanaGroup *errgroup.Group) {
102+
func listenToSystemSignals(server models.GrafanaServer) {
147103
signalChan := make(chan os.Signal, 1)
148104
code := 0
149105

150106
signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM)
151107

152108
select {
153109
case sig := <-signalChan:
154-
log.Info2("Received system signal. Shutting down", "signal", sig)
110+
server.Shutdown(0, fmt.Sprintf("system signal=%s", sig))
155111
case code = <-exitChan:
156-
switch code {
157-
case 0:
158-
log.Info("Shutting down")
159-
default:
160-
log.Warn("Shutting down")
161-
}
112+
server.Shutdown(code, "startup error")
162113
}
163-
164-
cancel()
165-
grafanaGroup.Wait()
166-
log.Close()
167-
os.Exit(code)
168114
}

pkg/cmd/grafana-server/server.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
"time"
9+
10+
"golang.org/x/sync/errgroup"
11+
12+
"github.com/grafana/grafana/pkg/api"
13+
"github.com/grafana/grafana/pkg/log"
14+
"github.com/grafana/grafana/pkg/login"
15+
"github.com/grafana/grafana/pkg/metrics"
16+
"github.com/grafana/grafana/pkg/models"
17+
"github.com/grafana/grafana/pkg/plugins"
18+
"github.com/grafana/grafana/pkg/services/alerting"
19+
"github.com/grafana/grafana/pkg/services/cleanup"
20+
"github.com/grafana/grafana/pkg/services/eventpublisher"
21+
"github.com/grafana/grafana/pkg/services/notifications"
22+
"github.com/grafana/grafana/pkg/services/search"
23+
"github.com/grafana/grafana/pkg/setting"
24+
"github.com/grafana/grafana/pkg/social"
25+
)
26+
27+
func NewGrafanaServer() models.GrafanaServer {
28+
rootCtx, shutdownFn := context.WithCancel(context.Background())
29+
childRoutines, childCtx := errgroup.WithContext(rootCtx)
30+
31+
return &GrafanaServerImpl{
32+
context: childCtx,
33+
shutdownFn: shutdownFn,
34+
childRoutines: childRoutines,
35+
log: log.New("server"),
36+
}
37+
}
38+
39+
type GrafanaServerImpl struct {
40+
context context.Context
41+
shutdownFn context.CancelFunc
42+
childRoutines *errgroup.Group
43+
log log.Logger
44+
}
45+
46+
func (g *GrafanaServerImpl) Start() {
47+
go listenToSystemSignals(g)
48+
49+
writePIDFile()
50+
initRuntime()
51+
initSql()
52+
metrics.Init()
53+
search.Init()
54+
login.Init()
55+
social.NewOAuthService()
56+
eventpublisher.Init()
57+
plugins.Init()
58+
59+
// init alerting
60+
if setting.AlertingEnabled {
61+
engine := alerting.NewEngine()
62+
g.childRoutines.Go(func() error { return engine.Run(g.context) })
63+
}
64+
65+
// cleanup service
66+
cleanUpService := cleanup.NewCleanUpService()
67+
g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
68+
69+
if err := notifications.Init(); err != nil {
70+
g.log.Error("Notification service failed to initialize", "erro", err)
71+
g.Shutdown(1, "Startup failed")
72+
return
73+
}
74+
75+
g.startHttpServer()
76+
}
77+
78+
func (g *GrafanaServerImpl) startHttpServer() {
79+
logger = log.New("http.server")
80+
81+
var err error
82+
m := newMacaron()
83+
api.Register(m)
84+
85+
listenAddr := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
86+
g.log.Info("Initializing HTTP Server", "address", listenAddr, "protocol", setting.Protocol, "subUrl", setting.AppSubUrl)
87+
88+
switch setting.Protocol {
89+
case setting.HTTP:
90+
err = http.ListenAndServe(listenAddr, m)
91+
case setting.HTTPS:
92+
err = http.ListenAndServeTLS(listenAddr, setting.CertFile, setting.KeyFile, m)
93+
default:
94+
g.log.Error("Invalid protocol", "protocol", setting.Protocol)
95+
g.Shutdown(1, "Startup failed")
96+
}
97+
98+
if err != nil {
99+
g.log.Error("Fail to start server", "error", err)
100+
g.Shutdown(1, "Startup failed")
101+
return
102+
}
103+
}
104+
105+
func (g *GrafanaServerImpl) Shutdown(code int, reason string) {
106+
log.Info("Shutting down", "code", code, "reason", reason)
107+
108+
g.shutdownFn()
109+
err := g.childRoutines.Wait()
110+
111+
log.Info("Shutting down completed", "error", err)
112+
113+
log.Close()
114+
os.Exit(code)
115+
}
116+
117+
// implement context.Context
118+
func (g *GrafanaServerImpl) Deadline() (deadline time.Time, ok bool) {
119+
return g.context.Deadline()
120+
}
121+
func (g *GrafanaServerImpl) Done() <-chan struct{} {
122+
return g.context.Done()
123+
}
124+
func (g *GrafanaServerImpl) Err() error {
125+
return g.context.Err()
126+
}
127+
func (g *GrafanaServerImpl) Value(key interface{}) interface{} {
128+
return g.context.Value(key)
129+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package core
1+
package models
22

33
import "context"
44

55
type GrafanaServer interface {
66
context.Context
7-
}
87

9-
type GrafanaServerImpl struct {
8+
Start()
9+
Shutdown(code int, reason string)
1010
}

0 commit comments

Comments
 (0)