github.com/polarismesh/polaris@v1.17.8/admin/job/history_clean.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package job
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/mitchellh/mapstructure"
    24  
    25  	"github.com/polarismesh/polaris/store"
    26  )
    27  
    28  // 默认保存配置发布天数
    29  const defaultHistoryRetentionDays = 7 * 24 * time.Hour
    30  
    31  type CleanConfigFileHistoryJobConfig struct {
    32  	RetentionDays time.Duration `mapstructure:"retentionDays"`
    33  	BatchSize     uint64        `mapstructure:"batchSize"`
    34  }
    35  
    36  type cleanConfigFileHistoryJob struct {
    37  	cfg     *CleanConfigFileHistoryJobConfig
    38  	storage store.Store
    39  }
    40  
    41  func (job *cleanConfigFileHistoryJob) init(raw map[string]interface{}) error {
    42  	cfg := &CleanConfigFileHistoryJobConfig{
    43  		RetentionDays: defaultHistoryRetentionDays,
    44  		BatchSize:     1000,
    45  	}
    46  	decodeConfig := &mapstructure.DecoderConfig{
    47  		DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
    48  		Result:     cfg,
    49  	}
    50  	decoder, err := mapstructure.NewDecoder(decodeConfig)
    51  	if err != nil {
    52  		log.Errorf("[Maintain][Job][cleanConfigFileHistoryJob] new config decoder err: %v", err)
    53  		return err
    54  	}
    55  	if err = decoder.Decode(raw); err != nil {
    56  		log.Errorf("[Maintain][Job][cleanConfigFileHistoryJob] parse config err: %v", err)
    57  		return err
    58  	}
    59  	if cfg.RetentionDays < time.Minute {
    60  		cfg.RetentionDays = time.Minute
    61  	}
    62  	job.cfg = cfg
    63  	return nil
    64  }
    65  
    66  func (job *cleanConfigFileHistoryJob) execute() {
    67  	endTime := time.Now().Add(-1 * job.cfg.RetentionDays)
    68  	if err := job.storage.CleanConfigFileReleaseHistory(endTime, job.cfg.BatchSize); err != nil {
    69  		log.Errorf("[Maintain][Job][cleanConfigFileHistoryJob] execute err: %v", err)
    70  	}
    71  }
    72  
    73  func (job *cleanConfigFileHistoryJob) interval() time.Duration {
    74  	return time.Minute
    75  }
    76  
    77  func (job *cleanConfigFileHistoryJob) clear() {
    78  }