periodic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Package task
  2. // Reused from https://github.com/v2fly/v2ray-core/blob/784775f68922f07d40c9eead63015b2026af2ade/common/task/periodic.go
  3. /*
  4. The MIT License (MIT)
  5. Copyright (c) 2015-2021 V2Ray & V2Fly Community
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. package task
  23. import (
  24. "sync"
  25. "time"
  26. )
  27. // Periodic is a task that runs periodically.
  28. type Periodic struct {
  29. // Interval of the task being run
  30. Interval time.Duration
  31. // Execute is the task function
  32. Execute func() error
  33. // OnError handles the error of the task
  34. OnError func(error)
  35. access sync.Mutex
  36. timer *time.Timer
  37. running bool
  38. }
  39. func (t *Periodic) hasClosed() bool {
  40. t.access.Lock()
  41. defer t.access.Unlock()
  42. return !t.running
  43. }
  44. func (t *Periodic) checkedExecute() error {
  45. if t.hasClosed() {
  46. return nil
  47. }
  48. if err := t.Execute(); err != nil {
  49. if t.OnError != nil {
  50. t.OnError(err)
  51. } else {
  52. // default error handling is to shut down the task
  53. t.access.Lock()
  54. t.running = false
  55. t.access.Unlock()
  56. return err
  57. }
  58. }
  59. t.access.Lock()
  60. defer t.access.Unlock()
  61. if !t.running {
  62. return nil
  63. }
  64. t.timer = time.AfterFunc(t.Interval, func() {
  65. t.checkedExecute()
  66. })
  67. return nil
  68. }
  69. // Start implements common.Runnable.
  70. func (t *Periodic) Start() error {
  71. t.access.Lock()
  72. if t.running {
  73. t.access.Unlock()
  74. return nil
  75. }
  76. t.running = true
  77. t.access.Unlock()
  78. if err := t.checkedExecute(); err != nil {
  79. t.access.Lock()
  80. t.running = false
  81. t.access.Unlock()
  82. return err
  83. }
  84. return nil
  85. }
  86. func (t *Periodic) WaitThenStart() {
  87. time.AfterFunc(t.Interval, func() {
  88. t.Start()
  89. })
  90. }
  91. // Close implements common.Closable.
  92. func (t *Periodic) Close() error {
  93. t.access.Lock()
  94. defer t.access.Unlock()
  95. t.running = false
  96. if t.timer != nil {
  97. t.timer.Stop()
  98. t.timer = nil
  99. }
  100. return nil
  101. }