...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package store
17
18 import (
19 "context"
20
21 "github.com/go-logr/logr"
22 "github.com/jinzhu/gorm"
23 "go.uber.org/fx"
24 controllermetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
25
26 config "github.com/chaos-mesh/chaos-mesh/pkg/config"
27 "github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/event"
28 "github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/experiment"
29 "github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/metrics"
30 "github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/schedule"
31 "github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/workflow"
32 )
33
34 var (
35
36 Module = fx.Options(
37 fx.Provide(
38 experiment.NewStore,
39 event.NewStore,
40 schedule.NewStore,
41 workflow.NewStore,
42 ),
43 fx.Supply(controllermetrics.Registry),
44 fx.Invoke(metrics.Register),
45 fx.Invoke(experiment.DeleteIncompleteExperiments),
46 fx.Invoke(schedule.DeleteIncompleteSchedules),
47 )
48 sqliteDriver = "sqlite3"
49 )
50
51
52 func NewDBStore(lc fx.Lifecycle, conf *config.ChaosDashboardConfig, logger logr.Logger) (*gorm.DB, error) {
53 ds := conf.Database.Datasource
54
55
56 if conf.Database.Driver == sqliteDriver {
57 ds += "?cache=shared"
58 }
59
60 gormDB, err := gorm.Open(conf.Database.Driver, ds)
61 if err != nil {
62 logger.Error(err, "Failed to open DB: ", "driver => ", conf.Database.Driver)
63 return nil, err
64 }
65
66
67 if conf.Database.Driver == sqliteDriver {
68 gormDB.DB().SetMaxOpenConns(1)
69 }
70
71 lc.Append(fx.Hook{
72 OnStop: func(context.Context) error {
73 return gormDB.Close()
74 },
75 })
76
77 return gormDB, nil
78 }
79