github.com/kontera-technologies/go-supervisor/v2@v2.1.0/example_test.go (about)

     1  package supervisor_test
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"time"
     7  
     8  	"github.com/kontera-technologies/go-supervisor/v2"
     9  )
    10  
    11  func Example() {
    12  	testDir, _ := filepath.Abs("testdata")
    13  	events := make(chan supervisor.Event)
    14  	p := supervisor.NewProcess(supervisor.ProcessOptions{
    15  		Name:                 "./example.sh",
    16  		Dir:                  testDir,
    17  		Id:                   "example",
    18  		EventNotifier:        events,
    19  		OutputParser:         supervisor.MakeBytesParser,
    20  		ErrorParser:          supervisor.MakeBytesParser,
    21  		MaxSpawns:            4,
    22  		MaxSpawnAttempts:     2,
    23  		MaxInterruptAttempts: 3,
    24  		MaxTerminateAttempts: 5,
    25  		IdleTimeout:          10 * time.Second,
    26  		MaxSpawnBackOff:      time.Second,
    27  		MaxRespawnBackOff:    time.Second,
    28  	})
    29  
    30  	exit := make(chan bool)
    31  
    32  	go func() {
    33  		for {
    34  			select {
    35  			case msg := <-p.Stdout():
    36  				fmt.Printf("Received STDOUT message: %s\n", *msg)
    37  			case msg := <-p.Stderr():
    38  				fmt.Printf("Received STDERR message: %s\n", *msg)
    39  			case event := <-events:
    40  				if event.Code == "ProcessStart" || event.Message == "" {
    41  					fmt.Printf("Received event: %s\n", event.Code)
    42  				} else {
    43  					fmt.Printf("Received event: %s - %s\n", event.Code, event.Message)
    44  				}
    45  			case <-p.DoneNotifier():
    46  				fmt.Println("Closing loop we are done...")
    47  				close(exit)
    48  				return
    49  			}
    50  		}
    51  	}()
    52  
    53  	if err := p.Start(); err != nil {
    54  		panic(fmt.Sprintf("failed to start process: %s", err))
    55  	}
    56  
    57  	<-exit
    58  
    59  	// Output:
    60  	// Received event: ProcessStart
    61  	// Received STDOUT message: STDOUT MESSAGE
    62  	// Received STDERR message: STDERR MESSAGE
    63  	// Received event: ProcessDone - exit status 0
    64  	// Received event: StoppingHeartbeatMonitoring - Stop signal received.
    65  	// Received event: Sleep - Sleeping for 1s before respwaning instance.
    66  	// Received event: ProcessRespawn - Trying to respawn instance.
    67  	// Received event: ProcessStart
    68  	// Received STDOUT message: STDOUT MESSAGE
    69  	// Received STDERR message: STDERR MESSAGE
    70  	// Received event: ProcessDone - exit status 0
    71  	// Received event: StoppingHeartbeatMonitoring - Stop signal received.
    72  	// Received event: Sleep - Sleeping for 1s before respwaning instance.
    73  	// Received event: ProcessRespawn - Trying to respawn instance.
    74  	// Received event: ProcessStart
    75  	// Received STDOUT message: STDOUT MESSAGE
    76  	// Received STDERR message: STDERR MESSAGE
    77  	// Received event: ProcessDone - exit status 0
    78  	// Received event: StoppingHeartbeatMonitoring - Stop signal received.
    79  	// Received event: Sleep - Sleeping for 1s before respwaning instance.
    80  	// Received event: ProcessRespawn - Trying to respawn instance.
    81  	// Received event: ProcessStart
    82  	// Received STDOUT message: STDOUT MESSAGE
    83  	// Received STDERR message: STDERR MESSAGE
    84  	// Received event: ProcessDone - exit status 0
    85  	// Received event: StoppingHeartbeatMonitoring - Stop signal received.
    86  	// Received event: RespawnError - Max number of respawns reached.
    87  	// Closing loop we are done...
    88  }