github.com/polarismesh/polaris@v1.17.8/admin/job/clean_deleted_instance.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  type CleanDeletedInstancesJobConfig struct {
    29  	InstanceCleanTimeout time.Duration `mapstructure:"instanceCleanTimeout"`
    30  }
    31  
    32  type cleanDeletedInstancesJob struct {
    33  	cfg     *CleanDeletedInstancesJobConfig
    34  	storage store.Store
    35  }
    36  
    37  func (job *cleanDeletedInstancesJob) init(raw map[string]interface{}) error {
    38  	cfg := &CleanDeletedInstancesJobConfig{
    39  		InstanceCleanTimeout: 10 * time.Minute,
    40  	}
    41  	decodeConfig := &mapstructure.DecoderConfig{
    42  		DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
    43  		Result:     cfg,
    44  	}
    45  	decoder, err := mapstructure.NewDecoder(decodeConfig)
    46  	if err != nil {
    47  		log.Errorf("[Maintain][Job][CleanDeletedInstances] new config decoder err: %v", err)
    48  		return err
    49  	}
    50  	if err = decoder.Decode(raw); err != nil {
    51  		log.Errorf("[Maintain][Job][CleanDeletedInstances] parse config err: %v", err)
    52  		return err
    53  	}
    54  	if cfg.InstanceCleanTimeout < 2*time.Minute {
    55  		cfg.InstanceCleanTimeout = 2 * time.Minute
    56  	}
    57  	job.cfg = cfg
    58  	return nil
    59  }
    60  
    61  func (job *cleanDeletedInstancesJob) execute() {
    62  	batchSize := uint32(100)
    63  	for {
    64  		count, err := job.storage.BatchCleanDeletedInstances(job.cfg.InstanceCleanTimeout, batchSize)
    65  		if err != nil {
    66  			log.Errorf("[Maintain][Job][CleanDeletedInstances] batch clean deleted instance, err: %v", err)
    67  			break
    68  		}
    69  
    70  		log.Infof("[Maintain][Job][CleanDeletedInstances] clean deleted instance count %d", count)
    71  		if count < batchSize {
    72  			break
    73  		}
    74  	}
    75  }
    76  
    77  func (job *cleanDeletedInstancesJob) interval() time.Duration {
    78  	return job.cfg.InstanceCleanTimeout
    79  }
    80  
    81  func (job *cleanDeletedInstancesJob) clear() {
    82  }