...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store/store.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/dashboard/store

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  	// Module includes the providers provided by store.
    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  // NewDBStore returns a new gorm.DB
    52  func NewDBStore(lc fx.Lifecycle, conf *config.ChaosDashboardConfig, logger logr.Logger) (*gorm.DB, error) {
    53  	ds := conf.Database.Datasource
    54  
    55  	// fix error `database is locked`, refer to https://github.com/mattn/go-sqlite3/blob/master/README.md#faq
    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  	// fix error `database is locked`, refer to https://github.com/mattn/go-sqlite3/blob/master/README.md#faq
    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