-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
78 lines (63 loc) · 1.61 KB
/
Copy pathexample_test.go
File metadata and controls
78 lines (63 loc) · 1.61 KB
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
package controls_test
import (
"context"
"fmt"
"net/http"
"time"
"gitlab.com/phpboyscout/go/controls"
)
func ExampleNewController() {
ctx := context.Background()
// Create a controller. No OS signal handler is installed by default; a
// standalone daemon that owns signals adds controls.WithSignals().
controller := controls.NewController(ctx)
// Register an HTTP service
controller.Register("http-api",
controls.WithStart(func(ctx context.Context) error {
fmt.Println("HTTP server starting")
return nil
}),
controls.WithStop(func(ctx context.Context) {
fmt.Println("HTTP server stopping")
}),
controls.WithStatus(func() error {
return nil // healthy
}),
)
// Start all services
controller.Start()
// Graceful shutdown
time.Sleep(10 * time.Millisecond)
controller.Stop()
controller.Wait()
}
func ExampleWithRestartPolicy() {
controller := controls.NewController(context.Background())
controller.Register("worker",
controls.WithStart(func(ctx context.Context) error {
return nil
}),
controls.WithRestartPolicy(controls.RestartPolicy{
MaxRestarts: 3,
InitialBackoff: time.Second,
MaxBackoff: 30 * time.Second,
}),
)
_ = controller
}
func ExampleWithLiveness() {
controller := controls.NewController(context.Background())
controller.Register("api",
controls.WithStart(func(ctx context.Context) error { return nil }),
controls.WithLiveness(func() error {
// Check if the service can respond
resp, err := http.Get("http://localhost:8080/healthz")
if err != nil {
return err
}
_ = resp.Body.Close()
return nil
}),
)
_ = controller
}