Skip to content

Commit

Permalink
run task at intervals
Browse files Browse the repository at this point in the history
wongoo committed Jan 12, 2022
1 parent 82df63b commit 5ab039e
Showing 4 changed files with 53 additions and 4 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
grunner - run/stop goroutines/tasks securely, recursively.

[![codecov](https://codecov.io/gh/vogo/grunner/branch/master/graph/badge.svg)](https://codecov.io/gh/vogo/grunner)
[![GoDoc](https://godoc.org/github.com/vogo/grunner?status.svg)](https://godoc.org/github.com/vogo/grunner)
![license](https://img.shields.io/badge/license-Apache--2.0-green.svg)

```go
s1 := grunner.New()

@@ -16,6 +20,11 @@ s1.Loop(func() {
time.Sleep(time.Millisecond*3)
})

// run interval task until s1 closed.
s1.Interval(func() {
t.Log("s1 run interval task")
}, time.Millisecond)

go func() {
ticker := time.NewTicker(time.Millisecond*2)

@@ -46,15 +55,25 @@ s1.Stop()
time.Sleep(time.Millisecond * 10)

// s1 run loop task
// s1 run interval task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run interval task
// s1 run loop task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run interval task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run loop task
// s1 run interval task
// s1 run interval task
// run ticker task until s1 stopped
// s1 run interval task
// s1 run loop task
// s1 run interval task
// s1 stopped 1
// s1 stopped 2
// s3 stopped
// s2 stopped
// s1 stopped 2
```
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/vogo/grunner

go 1.16

require github.com/stretchr/testify v1.7.0
23 changes: 23 additions & 0 deletions runner.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ package grunner
import (
"sync"
"sync/atomic"
"time"
)

type Task func()
@@ -98,6 +99,28 @@ func (s *Runner) Loop(task Task) {
}()
}

// Interval run task at intervals util the stopper is stopped.
func (s *Runner) Interval(task Task, interval time.Duration) {
go func() {
// run immediately for first time.
select {
case <-s.C:
return
default:
task()
}

for {
select {
case <-s.C:
return
case <-time.After(interval):
task()
}
}
}()
}

// New create a new Runner.
func New() *Runner {
return &Runner{
11 changes: 10 additions & 1 deletion runner_test.go
Original file line number Diff line number Diff line change
@@ -40,12 +40,17 @@ func TestRunnerStop(t *testing.T) {
t.Log("s1 stopped 1")
})

// loop run task until s1 closed.
// run loop task until s1 closed.
s1.Loop(func() {
t.Log("s1 run loop task")
time.Sleep(time.Millisecond * 3)
})

// run interval task until s1 closed.
s1.Interval(func() {
t.Log("s1 run interval task")
}, time.Millisecond)

go func() {
ticker := time.NewTicker(time.Millisecond * 2)

@@ -73,6 +78,10 @@ func TestRunnerStop(t *testing.T) {

s1.Stop()

s1.Interval(func() {
t.Fatal("should not run interval task after s1 stopped")
}, time.Millisecond)

time.Sleep(goroutineScheduleInterval)
}

0 comments on commit 5ab039e

Please sign in to comment.