func refreshStateConfigs()

in agent/statemanager/statemanager.go [86:158]


func refreshStateConfigs() {
	defer func() {
		if panicPayload := recover(); panicPayload != nil {
			stacktrace := debug.Stack()
			fmt.Println(string(stacktrace))
			clientreport.ReportPanic(panicPayload, stacktrace, false)
		}
	}()
	log.GetLogger().Info("refresh state configurations")
	cachedResult, err := LoadConfigCache()
	var lastCheckpoint string
	var lastCheckTime time.Time
	if err != nil {
		log.GetLogger().WithError(err).Error("load local state configuration fail")
	}
	if cachedResult != nil {
		lastCheckpoint = cachedResult.Checkpoint
		log.GetLogger().Debugf("last state configuration checkpoint: %s", lastCheckpoint)
		lastCheckTime, err = timetool.ParseApiTime(lastCheckpoint)
	} else {
		lastCheckpoint = ""
	}

	result := cachedResult
	// 如果是刚刚拉取过则使用缓存
	if time.Now().Sub(lastCheckTime).Minutes() > 1 {
		info, err := instance.GetInstanceInfo()
		log.GetLogger().Debugf("instance information: %s", info)
		if err != nil {
			log.GetLogger().WithError(err).Error("get instance info failed")
			return
		}
		resp, err := ListInstanceStateConfigurations(lastCheckpoint, info.AgentName, info.AgentVersion,
			info.ComputerName, info.PlatformName, info.PlatformType, info.PlatformVersion,
			info.IpAddress, info.RamRole)
		if err != nil {
			if resp != nil && resp.ErrCode == "ServiceNotSupported" {
				log.GetLogger().Warn("state manager feature is not supported in current region")
				oneDay := 24 * 60 * 60
				if refreshIntervalSeconds != oneDay {
					refreshIntervalSeconds = oneDay
					CancelStateManagerTimer()
					InitStateManagerTimer()
				}
				return
			}
			log.GetLogger().WithError(err).Error("fail to list state configurations")
			return
		}
		if resp.Result != nil && resp.Result.Changed {
			// 未变更的情况下,服务端没有完整返回配置,使用缓存
			result = resp.Result
			WriteConfigCache(result)
		}
	}
	var targetInterval = result.Interval
	if targetInterval == 0 {
		// interval can recover to default once api call is successful and no interval is returned from server
		targetInterval = DefaultRefreshIntervalSeconds
	}
	if targetInterval != refreshIntervalSeconds {
		// 拉取配置的间隔变更,立即重新调度,会触发再次执行本函数,本次执行可以直接返回
		log.GetLogger().Infof("state manager refresh interval changes from %d to %d seconds", refreshIntervalSeconds, targetInterval)
		refreshIntervalSeconds = targetInterval
		CancelStateManagerTimer()
		InitStateManagerTimer()
		return
	}
	log.GetLogger().Infof("use state configurations: %v", result)
	updateStateConfigs(result.StateConfigurations)
	refreshStateConfigTimers(result.StateConfigurations)
	runtime.GC()
}