workers_watchdog.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package appsrv
  15. import (
  16. "time"
  17. "yunion.io/x/log"
  18. )
  19. const (
  20. WATCHDOG_SLEEP_SECONDS = 30
  21. )
  22. var (
  23. busyWorkers map[*SWorkerManager]int
  24. exitFlag bool
  25. )
  26. func init() {
  27. busyWorkers = make(map[*SWorkerManager]int)
  28. watchdog()
  29. }
  30. func watchdog() {
  31. do_worker_watchdog()
  32. time.AfterFunc(time.Second*WATCHDOG_SLEEP_SECONDS, watchdog)
  33. }
  34. func do_worker_watchdog() {
  35. for _, w := range workerManagers {
  36. stats := w.getState()
  37. busy := stats.IsBusy()
  38. if busy {
  39. if _, ok := busyWorkers[w]; ok {
  40. busyWorkers[w] += 1
  41. } else {
  42. busyWorkers[w] = 1
  43. }
  44. } else {
  45. if _, ok := busyWorkers[w]; ok {
  46. delete(busyWorkers, w)
  47. }
  48. }
  49. }
  50. if len(busyWorkers) > 0 {
  51. for w, k := range busyWorkers {
  52. if k > 1 {
  53. log.Warningf("WorkerManager %s has been busy for %d cycles...", w.name, k)
  54. }
  55. }
  56. } else {
  57. if exitFlag {
  58. log.Fatalln("System is idle, no worker is busy, exitFlag is set, to exit ...")
  59. }
  60. }
  61. }
  62. func SetExitFlag() {
  63. exitFlag = true
  64. }