Skip to content

Commit

Permalink
Merge pull request #34 from pior/every
Browse files Browse the repository at this point in the history
Rewrite Periodic() into a nicer Every()
  • Loading branch information
pior authored May 16, 2022
2 parents 342f3fb + 23847bc commit b602d5c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 56 deletions.
38 changes: 38 additions & 0 deletions every.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package runnable

import (
"context"
"fmt"
"time"
)

// Every returns a runnable that will periodically run the runnable passed in argument.
func Every(runnable Runnable, period time.Duration) Runnable {
return &every{runnable, period}
}

type every struct {
runnable Runnable
period time.Duration
}

func (e *every) Run(ctx context.Context) (err error) {
ticker := time.NewTicker(e.period)

for {
select {
case <-ctx.Done():
ticker.Stop()
return ctx.Err()
case <-ticker.C:
err := e.runnable.Run(ctx)
if err != nil {
return err
}
}
}
}

func (e *every) name() string {
return composeName(fmt.Sprintf("every[%s]", e.period), e.runnable)
}
12 changes: 6 additions & 6 deletions periodic_test.go → every_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Periodic_Cancellation(t *testing.T) {
runner := Periodic(PeriodicOptions{Period: time.Second}, newDummyRunnable())
func Test_Every_Cancellation(t *testing.T) {
runner := Every(newDummyRunnable(), time.Second)

AssertRunnableRespectCancellation(t, runner, time.Second)
AssertRunnableRespectPreCancelledContext(t, runner)
}

func Test_Periodic(t *testing.T) {
func Test_Every(t *testing.T) {
counterRunnable := newCounterRunnable()

runner := Periodic(PeriodicOptions{Period: time.Millisecond * 10}, counterRunnable)
runner := Every(counterRunnable, time.Millisecond*10)

ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
Expand All @@ -30,6 +30,6 @@ func Test_Periodic(t *testing.T) {
require.LessOrEqual(t, counterRunnable.counter, 10)
}

func Test_Periodic_Name(t *testing.T) {
AssertName(t, "periodic(runnable.dummyRunnable)", Periodic(PeriodicOptions{Period: time.Second}, newDummyRunnable()))
func Test_Every_Name(t *testing.T) {
AssertName(t, "every[1s](runnable.dummyRunnable)", Every(newDummyRunnable(), time.Second))
}
7 changes: 2 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ func Example() {
})
g.Add(task)

periodicCleanup := runnable.Periodic(
runnable.PeriodicOptions{Period: time.Hour},
&CleanupTask{},
)
g.Add(periodicCleanup, jobs)
cleanup := runnable.Every(&CleanupTask{}, time.Hour)
g.Add(cleanup, jobs)

runnable.Run(g.Build())

Expand Down
4 changes: 1 addition & 3 deletions examples/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ func main() {
fmt.Printf("Task executed: %d\n", jobs.Executed())
return nil
})
monitor = runnable.Periodic(runnable.PeriodicOptions{
Period: 3 * time.Second,
}, monitor)
monitor = runnable.Every(monitor, 3*time.Second)

g := runnable.NewManager()
g.Add(jobs)
Expand Down
42 changes: 0 additions & 42 deletions periodic.go

This file was deleted.

0 comments on commit b602d5c

Please sign in to comment.