-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
88 lines (80 loc) · 2.84 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
package supervisor_test
import (
"fmt"
"path/filepath"
"time"
"github.com/kontera-technologies/go-supervisor/v2"
)
func Example() {
testDir, _ := filepath.Abs("testdata")
events := make(chan supervisor.Event)
p := supervisor.NewProcess(supervisor.ProcessOptions{
Name: "./example.sh",
Dir: testDir,
Id: "example",
EventNotifier: events,
OutputParser: supervisor.MakeBytesParser,
ErrorParser: supervisor.MakeBytesParser,
MaxSpawns: 4,
MaxSpawnAttempts: 2,
MaxInterruptAttempts: 3,
MaxTerminateAttempts: 5,
IdleTimeout: 10 * time.Second,
MaxSpawnBackOff: time.Second,
MaxRespawnBackOff: time.Second,
})
exit := make(chan bool)
go func() {
for {
select {
case msg := <-p.Stdout():
fmt.Printf("Received STDOUT message: %s\n", *msg)
case msg := <-p.Stderr():
fmt.Printf("Received STDERR message: %s\n", *msg)
case event := <-events:
if event.Code == "ProcessStart" || event.Message == "" {
fmt.Printf("Received event: %s\n", event.Code)
} else {
fmt.Printf("Received event: %s - %s\n", event.Code, event.Message)
}
case <-p.DoneNotifier():
fmt.Println("Closing loop we are done...")
close(exit)
return
}
}
}()
if err := p.Start(); err != nil {
panic(fmt.Sprintf("failed to start process: %s", err))
}
<-exit
// Output:
// Received event: ProcessStart
// Received STDOUT message: STDOUT MESSAGE
// Received STDERR message: STDERR MESSAGE
// Received event: ProcessDone - exit status 0
// Received event: StoppingHeartbeatMonitoring - Stop signal received.
// Received event: Sleep - Sleeping for 1s before respwaning instance.
// Received event: ProcessRespawn - Trying to respawn instance.
// Received event: ProcessStart
// Received STDOUT message: STDOUT MESSAGE
// Received STDERR message: STDERR MESSAGE
// Received event: ProcessDone - exit status 0
// Received event: StoppingHeartbeatMonitoring - Stop signal received.
// Received event: Sleep - Sleeping for 1s before respwaning instance.
// Received event: ProcessRespawn - Trying to respawn instance.
// Received event: ProcessStart
// Received STDOUT message: STDOUT MESSAGE
// Received STDERR message: STDERR MESSAGE
// Received event: ProcessDone - exit status 0
// Received event: StoppingHeartbeatMonitoring - Stop signal received.
// Received event: Sleep - Sleeping for 1s before respwaning instance.
// Received event: ProcessRespawn - Trying to respawn instance.
// Received event: ProcessStart
// Received STDOUT message: STDOUT MESSAGE
// Received STDERR message: STDERR MESSAGE
// Received event: ProcessDone - exit status 0
// Received event: StoppingHeartbeatMonitoring - Stop signal received.
// Received event: RespawnError - Max number of respawns reached.
// Closing loop we are done...
}