Skip to content

Commit 71e2c6f

Browse files
committed
refactor(backend): refactoring management of go routines in backend
1 parent bc634f2 commit 71e2c6f

File tree

10 files changed

+119
-123
lines changed

10 files changed

+119
-123
lines changed

pkg/cmd/grafana-server/main.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ import (
1919
"github.com/grafana/grafana/pkg/login"
2020
"github.com/grafana/grafana/pkg/metrics"
2121
"github.com/grafana/grafana/pkg/plugins"
22-
alertingInit "github.com/grafana/grafana/pkg/services/alerting/init"
23-
"github.com/grafana/grafana/pkg/services/backgroundtasks"
22+
"github.com/grafana/grafana/pkg/services/cleanup"
2423
"github.com/grafana/grafana/pkg/services/eventpublisher"
2524
"github.com/grafana/grafana/pkg/services/notifications"
2625
"github.com/grafana/grafana/pkg/services/search"
2726
"github.com/grafana/grafana/pkg/services/sqlstore"
2827
"github.com/grafana/grafana/pkg/setting"
2928
"github.com/grafana/grafana/pkg/social"
29+
30+
"github.com/grafana/grafana/pkg/services/alerting"
31+
_ "github.com/grafana/grafana/pkg/services/alerting/conditions"
32+
_ "github.com/grafana/grafana/pkg/services/alerting/notifiers"
33+
_ "github.com/grafana/grafana/pkg/tsdb/graphite"
34+
_ "github.com/grafana/grafana/pkg/tsdb/prometheus"
35+
_ "github.com/grafana/grafana/pkg/tsdb/testdata"
3036
)
3137

3238
var version = "3.1.0"
@@ -67,6 +73,7 @@ func main() {
6773

6874
flag.Parse()
6975
writePIDFile()
76+
7077
initRuntime()
7178
initSql()
7279
metrics.Init()
@@ -76,8 +83,15 @@ func main() {
7683
eventpublisher.Init()
7784
plugins.Init()
7885

79-
grafanaGroup.Go(func() error { return alertingInit.Init(appContext) })
80-
grafanaGroup.Go(func() error { return backgroundtasks.Init(appContext) })
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) })
8195

8296
if err := notifications.Init(); err != nil {
8397
log.Fatal(3, "Notification service failed to initialize", err)

pkg/models/dashboard_snapshot.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type DeleteDashboardSnapshotCommand struct {
6363
DeleteKey string `json:"-"`
6464
}
6565

66+
type DeleteExpiredSnapshotsCommand struct {
67+
}
68+
6669
type GetDashboardSnapshotQuery struct {
6770
Key string
6871

pkg/models/timer.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/services/alerting/engine.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ func NewEngine() *Engine {
3636
return e
3737
}
3838

39-
func (e *Engine) Start(grafanaCtx context.Context) error {
40-
e.log.Info("Starting Alerting Engine")
39+
func (e *Engine) Run(ctx context.Context) error {
40+
e.log.Info("Initializing Alerting")
4141

42-
g, grafanaCtx := errgroup.WithContext(grafanaCtx)
42+
g, ctx := errgroup.WithContext(ctx)
4343

44-
g.Go(func() error { return e.alertingTicker(grafanaCtx) })
45-
g.Go(func() error { return e.execDispatcher(grafanaCtx) })
46-
g.Go(func() error { return e.resultDispatcher(grafanaCtx) })
44+
g.Go(func() error { return e.alertingTicker(ctx) })
45+
g.Go(func() error { return e.execDispatcher(ctx) })
46+
g.Go(func() error { return e.resultDispatcher(ctx) })
4747

48-
return g.Wait()
48+
err := g.Wait()
49+
50+
e.log.Info("Stopped Alerting", "reason", err)
51+
return err
4952
}
5053

5154
func (e *Engine) Stop() {

pkg/services/alerting/init/init.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
_ "github.com/grafana/grafana/pkg/tsdb/testdata"
1313
)
1414

15-
var engine *alerting.Engine
16-
1715
func Init(ctx context.Context) error {
1816
if !setting.AlertingEnabled {
1917
return nil

pkg/services/backgroundtasks/background_tasks.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

pkg/services/backgroundtasks/remove_tmp_images.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

pkg/services/cleanup/cleanup.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cleanup
2+
3+
import (
4+
"context"
5+
"io/ioutil"
6+
"os"
7+
"path"
8+
"time"
9+
10+
"golang.org/x/sync/errgroup"
11+
12+
"github.com/grafana/grafana/pkg/bus"
13+
"github.com/grafana/grafana/pkg/log"
14+
m "github.com/grafana/grafana/pkg/models"
15+
"github.com/grafana/grafana/pkg/setting"
16+
)
17+
18+
type CleanUpService struct {
19+
log log.Logger
20+
}
21+
22+
func NewCleanUpService() *CleanUpService {
23+
return &CleanUpService{
24+
log: log.New("cleanup"),
25+
}
26+
}
27+
28+
func (service *CleanUpService) Run(ctx context.Context) error {
29+
service.log.Info("Initializing CleanUpService")
30+
31+
g, _ := errgroup.WithContext(ctx)
32+
g.Go(func() error { return service.start(ctx) })
33+
34+
err := g.Wait()
35+
service.log.Info("Stopped CleanUpService", "reason", err)
36+
return err
37+
}
38+
39+
func (service *CleanUpService) start(ctx context.Context) error {
40+
service.cleanUpTmpFiles()
41+
42+
ticker := time.NewTicker(time.Hour * 1)
43+
for {
44+
select {
45+
case <-ticker.C:
46+
service.cleanUpTmpFiles()
47+
service.deleteExpiredSnapshots()
48+
case <-ctx.Done():
49+
return ctx.Err()
50+
}
51+
}
52+
}
53+
54+
func (service *CleanUpService) cleanUpTmpFiles() {
55+
files, err := ioutil.ReadDir(setting.ImagesDir)
56+
if err != nil {
57+
service.log.Error("Problem reading image dir", "error", err)
58+
return
59+
}
60+
61+
var toDelete []os.FileInfo
62+
for _, file := range files {
63+
if file.ModTime().AddDate(0, 0, 1).Before(time.Now()) {
64+
toDelete = append(toDelete, file)
65+
}
66+
}
67+
68+
for _, file := range toDelete {
69+
fullPath := path.Join(setting.ImagesDir, file.Name())
70+
err := os.Remove(fullPath)
71+
if err != nil {
72+
service.log.Error("Failed to delete temp file", "file", file.Name(), "error", err)
73+
}
74+
}
75+
76+
service.log.Debug("Found old rendered image to delete", "deleted", len(toDelete), "keept", len(files))
77+
}
78+
79+
func (service *CleanUpService) deleteExpiredSnapshots() {
80+
bus.Dispatch(&m.DeleteExpiredSnapshotsCommand{})
81+
}

pkg/services/sqlstore/dashboard_snapshot.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/go-xorm/xorm"
77
"github.com/grafana/grafana/pkg/bus"
8-
"github.com/grafana/grafana/pkg/log"
98
m "github.com/grafana/grafana/pkg/models"
109
"github.com/grafana/grafana/pkg/setting"
1110
)
@@ -15,30 +14,24 @@ func init() {
1514
bus.AddHandler("sql", GetDashboardSnapshot)
1615
bus.AddHandler("sql", DeleteDashboardSnapshot)
1716
bus.AddHandler("sql", SearchDashboardSnapshots)
18-
bus.AddEventListener(DeleteExpiredSnapshots)
17+
bus.AddHandler("sql", DeleteExpiredSnapshots)
1918
}
2019

21-
func DeleteExpiredSnapshots(cmd *m.HourCommand) error {
20+
func DeleteExpiredSnapshots(cmd *m.DeleteExpiredSnapshotsCommand) error {
2221
return inTransaction(func(sess *xorm.Session) error {
2322
var expiredCount int64 = 0
24-
var oldCount int64 = 0
2523

2624
if setting.SnapShotRemoveExpired {
2725
deleteExpiredSql := "DELETE FROM dashboard_snapshot WHERE expires < ?"
28-
expiredResponse, err := x.Exec(deleteExpiredSql, cmd.Time)
26+
expiredResponse, err := x.Exec(deleteExpiredSql, time.Now)
2927
if err != nil {
3028
return err
3129
}
3230
expiredCount, _ = expiredResponse.RowsAffected()
3331
}
3432

35-
oldSnapshotsSql := "DELETE FROM dashboard_snapshot WHERE created < ?"
36-
oldResponse, err := x.Exec(oldSnapshotsSql, cmd.Time.AddDate(0, 0, setting.SnapShotTTLDays*-1))
37-
oldCount, _ = oldResponse.RowsAffected()
38-
39-
log.Debug2("Deleted old/expired snaphots", "to old", oldCount, "expired", expiredCount)
40-
41-
return err
33+
sqlog.Debug("Deleted old/expired snaphots", "expired", expiredCount)
34+
return nil
4235
})
4336
}
4437

pkg/setting/setting.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ var (
120120
IsWindows bool
121121

122122
// PhantomJs Rendering
123-
ImagesDir string
124-
PhantomDir string
125-
RenderedImageTTLDays int
123+
ImagesDir string
124+
PhantomDir string
126125

127126
// for logging purposes
128127
configFiles []string
@@ -540,9 +539,6 @@ func NewConfigContext(args *CommandLineArgs) error {
540539
ImagesDir = filepath.Join(DataPath, "png")
541540
PhantomDir = filepath.Join(HomePath, "vendor/phantomjs")
542541

543-
tmpFilesSection := Cfg.Section("tmp.files")
544-
RenderedImageTTLDays = tmpFilesSection.Key("rendered_image_ttl_days").MustInt(14)
545-
546542
analytics := Cfg.Section("analytics")
547543
ReportingEnabled = analytics.Key("reporting_enabled").MustBool(true)
548544
CheckForUpdates = analytics.Key("check_for_updates").MustBool(true)

0 commit comments

Comments
 (0)