...

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

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

     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 ttlcontroller provides a TTL (time to live) mechanism to clear old objects
    17  // in the database.
    18  package ttlcontroller
    19  
    20  import (
    21  	"context"
    22  
    23  	"github.com/go-logr/logr"
    24  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    25  	"k8s.io/apimachinery/pkg/util/wait"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/config"
    28  	"github.com/chaos-mesh/chaos-mesh/pkg/dashboard/core"
    29  )
    30  
    31  type Controller struct {
    32  	logger     logr.Logger
    33  	event      core.EventStore
    34  	experiment core.ExperimentStore
    35  	schedule   core.ScheduleStore
    36  	workflow   core.WorkflowStore
    37  	ttlconfig  *config.TTLConfig
    38  }
    39  
    40  // NewController returns a new database ttl controller
    41  func NewController(
    42  	event core.EventStore,
    43  	experiment core.ExperimentStore,
    44  	schedule core.ScheduleStore,
    45  	workflow core.WorkflowStore,
    46  	ttlconfig *config.TTLConfig,
    47  	logger logr.Logger,
    48  ) *Controller {
    49  	return &Controller{
    50  		experiment: experiment,
    51  		event:      event,
    52  		schedule:   schedule,
    53  		workflow:   workflow,
    54  		ttlconfig:  ttlconfig,
    55  		logger:     logger,
    56  	}
    57  }
    58  
    59  // Register periodically calls function runWorker to delete the data.
    60  func Register(ctx context.Context, c *Controller) {
    61  	defer utilruntime.HandleCrash()
    62  
    63  	c.logger.Info("Starting database TTL controller")
    64  
    65  	go wait.Until(c.runWorker, c.ttlconfig.ResyncPeriod, ctx.Done())
    66  }
    67  
    68  // runWorker is a long-running function that will be called in order to delete the events, archives, schedule, and workflow.
    69  func (c *Controller) runWorker() {
    70  	c.logger.Info("Deleting expired data from the database")
    71  
    72  	ctx := context.Background()
    73  
    74  	_ = c.event.DeleteByDuration(ctx, c.ttlconfig.EventTTL)
    75  	c.experiment.DeleteByFinishTime(ctx, c.ttlconfig.ExperimentTTL)
    76  	c.schedule.DeleteByFinishTime(ctx, c.ttlconfig.ScheduleTTL)
    77  	c.workflow.DeleteByFinishTime(ctx, c.ttlconfig.WorkflowTTL)
    78  }
    79