github.com/polarismesh/polaris@v1.17.8/admin/job/clean_deleted_client.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 CleanDeletedClientsJobConfig struct { 29 ClientCleanTimeout time.Duration `mapstructure:"clientCleanTimeout"` 30 } 31 32 type cleanDeletedClientsJob struct { 33 cfg *CleanDeletedClientsJobConfig 34 storage store.Store 35 } 36 37 func (job *cleanDeletedClientsJob) init(raw map[string]interface{}) error { 38 cfg := &CleanDeletedClientsJobConfig{ 39 ClientCleanTimeout: 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][CleanDeletedClients] new config decoder err: %v", err) 48 return err 49 } 50 if err := decoder.Decode(raw); err != nil { 51 log.Errorf("[Maintain][Job][CleanDeletedClients] parse config err: %v", err) 52 return err 53 } 54 if cfg.ClientCleanTimeout < 2*time.Minute { 55 cfg.ClientCleanTimeout = 2 * time.Minute 56 } 57 job.cfg = cfg 58 return nil 59 } 60 61 func (job *cleanDeletedClientsJob) execute() { 62 batchSize := uint32(100) 63 for { 64 count, err := job.storage.BatchCleanDeletedClients(job.cfg.ClientCleanTimeout, batchSize) 65 if err != nil { 66 log.Errorf("[Maintain][Job][CleanDeletedClients] batch clean deleted client, err: %v", err) 67 break 68 } 69 70 log.Infof("[Maintain][Job][CleanDeletedClients] clean deleted client count %d", count) 71 72 if count < batchSize { 73 break 74 } 75 } 76 } 77 78 func (job *cleanDeletedClientsJob) clear() { 79 } 80 81 func (job *cleanDeletedClientsJob) interval() time.Duration { 82 return job.cfg.ClientCleanTimeout 83 }