Beispiel #1
0
// 检查状态,并在满足持续空间时间的条件时采取必要措施
func checkStatus(
	scheduler scheduler.MKScheduler,
	interval time.Duration,
	maxIdleCount uint,
	autoStop bool,
	checkCountChan chan<- uint64,
	record MKRecord,
	stopNotifier chan<- byte) {

	var checkCount uint64
	go func() {

		defer func() {
			stopNotifier <- 1
			stopNotifier <- 2
			checkCountChan <- checkCount
		}()

		// 等待调度器开启
		waitForSchedulerStart(scheduler)

		// 准备
		var idleCount uint
		var firstIdleTime time.Time
		for {
			// 检查调度器的空闲状态
			if scheduler.Idle() {
				idleCount++
				if idleCount == 1 {
					firstIdleTime = time.Now()
				}

				if idleCount >= maxIdleCount {
					msg := fmt.Sprintf(messageForReachMaxIdleCount, time.Since(firstIdleTime).String())
					record(0, msg)

					// 再次检查调度器的空闲状态,确保它已经可以被停止
					if scheduler.Idle() {

						if autoStop {
							var result string
							if scheduler.Stop() {
								result = "success"
							} else {
								result = "failing"
							}

							msg = fmt.Sprintf(messageForReachMaxIdleCount, result)
							record(0, msg)
						}

						break
					} else {
						if idleCount > 0 {
							idleCount = 0
						}
					}
				}
			} else {
				if idleCount > 0 {
					idleCount = 0
				}
			}

			checkCount++
			time.Sleep(interval)
		}
	}()
}