-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlooper_test.go
47 lines (44 loc) · 958 Bytes
/
looper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package plumber_test
import (
"context"
"fmt"
"time"
"github.com/getoutreach/plumber"
)
func ExampleLooper() {
ctx := context.Background()
err := plumber.Start(ctx,
plumber.Looper(func(ctx context.Context, l *plumber.Loop) error {
l.Ready()
tick := time.Tick(500 * time.Millisecond)
fmt.Println("Looper starting up")
for {
select {
case <-tick:
// Work
fmt.Println("Looper work")
case done := <-l.Closing():
fmt.Println("Looper requested to shutdown")
done.Success()
fmt.Println("Looper finished")
// Graceful shutdown
return nil
case <-ctx.Done():
fmt.Println("Looper canceled")
// Cancel / Timeout
return ctx.Err()
}
}
}),
plumber.TTL(600*time.Millisecond),
plumber.CloseTimeout(2*time.Second),
)
if err != nil {
fmt.Println("error: ", err)
}
// Output:
// Looper starting up
// Looper work
// Looper requested to shutdown
// Looper finished
}