-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
137 lines (132 loc) · 3.38 KB
/
example_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package supervisor_test
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/go-logr/stdr"
"go.einride.tech/supervisor"
)
func ExampleSupervisor() {
// Restart stopped services every 10ms.
cfg := supervisor.Config{
RestartInterval: 10 * time.Millisecond,
// No specified clock returns system clock
// No specified logger returns a nop-logger
}
// Create a context which can be canceled.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
// Create pingpong table
table := make(chan int)
roundsToPlay := 2
// Create player services.
pingService := supervisor.NewService("ping", func(ctx context.Context) error {
i := roundsToPlay
for {
select {
case <-ctx.Done():
return fmt.Errorf("timeout")
case table <- i:
fmt.Println("ping")
i = <-table
if i == 0 {
close(table)
cancel()
return nil
}
}
}
})
pongService := supervisor.NewService("pong", func(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return fmt.Errorf("timeout")
case i := <-table:
if i == 0 {
return nil
}
table <- i - 1
fmt.Println("pong")
}
}
})
// Add service to the supervised services.
cfg.Services = append(cfg.Services, pingService, pongService)
// Create the supervisor from the config.
s := supervisor.New(&cfg)
// Run the supervisor (blocking call).
if err := s.Run(ctx); err != nil {
// handle error
panic(err)
}
defer cancel()
// Output:
// ping
// pong
// ping
// pong
}
func ExampleNew() {
// Restart stopped services every 10ms.
cfg := supervisor.Config{
RestartInterval: 10 * time.Millisecond,
Logger: stdr.New(log.New(os.Stderr, "", log.LstdFlags)),
}
// Create a context that can be canceled inside the service.
ctx, cancel := context.WithCancel(context.Background())
starts := 0
svc := supervisor.NewService("example", func(_ context.Context) error {
if starts == 3 {
cancel()
return nil
}
starts++
return fmt.Errorf("oops")
})
// Add service to set of supervised services.
cfg.Services = append(cfg.Services, svc)
// Create supervisor from config.
s := supervisor.New(&cfg)
// Run supervisor (blocking).
_ = s.Run(ctx) // no error currently reported
fmt.Println("service restarted", starts, "times")
// Output:
// service restarted 3 times
}
func ExampleConfig_StatusUpdateListeners() {
// Restart stopped services every 10ms.
cfg := supervisor.Config{
RestartInterval: 10 * time.Millisecond,
Services: []supervisor.Service{
// Create a crashing service.
supervisor.NewService("example", func(_ context.Context) error {
return fmt.Errorf("oops")
}),
},
Logger: stdr.New(log.New(os.Stderr, "", log.LstdFlags)),
}
// Create a context that can be canceled.
ctx, cancel := context.WithCancel(context.Background())
stops := 0
// Create a statusupdate listener that cancels the context
// after the example service crashes 3 times.
cfg.StatusUpdateListeners = append(cfg.StatusUpdateListeners, func(updates []supervisor.StatusUpdate) {
for _, update := range updates {
if update.ServiceName == "example" &&
update.Status == supervisor.StatusError ||
update.Status == supervisor.StatusStopped {
stops++
}
}
if stops == 3 {
cancel()
}
})
s := supervisor.New(&cfg)
_ = s.Run(ctx) // no error currently reported
fmt.Println("service stopped", stops, "times")
// Output:
// service stopped 3 times
}