periodic.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package task
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // Periodic is a task that runs periodically.
  7. type Periodic struct {
  8. // Interval of the task being run
  9. Interval time.Duration
  10. // Execute is the task function
  11. Execute func() error
  12. access sync.Mutex
  13. timer *time.Timer
  14. running bool
  15. }
  16. func (t *Periodic) hasClosed() bool {
  17. t.access.Lock()
  18. defer t.access.Unlock()
  19. return !t.running
  20. }
  21. func (t *Periodic) checkedExecute() error {
  22. if t.hasClosed() {
  23. return nil
  24. }
  25. if err := t.Execute(); err != nil {
  26. t.access.Lock()
  27. t.running = false
  28. t.access.Unlock()
  29. return err
  30. }
  31. t.access.Lock()
  32. defer t.access.Unlock()
  33. if !t.running {
  34. return nil
  35. }
  36. t.timer = time.AfterFunc(t.Interval, func() {
  37. t.checkedExecute()
  38. })
  39. return nil
  40. }
  41. // Start implements common.Runnable.
  42. func (t *Periodic) Start() error {
  43. t.access.Lock()
  44. if t.running {
  45. t.access.Unlock()
  46. return nil
  47. }
  48. t.running = true
  49. t.access.Unlock()
  50. if err := t.checkedExecute(); err != nil {
  51. t.access.Lock()
  52. t.running = false
  53. t.access.Unlock()
  54. return err
  55. }
  56. return nil
  57. }
  58. // Close implements common.Closable.
  59. func (t *Periodic) Close() error {
  60. t.access.Lock()
  61. defer t.access.Unlock()
  62. t.running = false
  63. if t.timer != nil {
  64. t.timer.Stop()
  65. t.timer = nil
  66. }
  67. return nil
  68. }