github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/common/task/task.go (about) 1 package task 2 3 import "time" 4 5 type StopFn func() 6 7 func RunTaskRepeateadly(task func(), period time.Duration) StopFn { 8 // Setup the ticker and the channel to signal 9 // the ending of the interval 10 ticker := time.NewTicker(period) 11 stop := make(chan struct{}) 12 13 go func() { 14 for { 15 select { 16 case <-ticker.C: 17 task() 18 case <-stop: 19 ticker.Stop() 20 return 21 } 22 } 23 }() 24 25 return func() { 26 stop <- struct{}{} 27 } 28 }