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
|
package serve
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/roadrunner-server/roadrunner/v2/roadrunner"
"github.com/roadrunner-server/errors"
"github.com/spf13/cobra"
)
const (
rrPrefix string = "rr"
)
// NewCommand creates `serve` command.
func NewCommand(override *[]string, cfgFile *string, silent *bool) *cobra.Command { //nolint:funlen
return &cobra.Command{
Use: "serve",
Short: "Start RoadRunner server",
RunE: func(*cobra.Command, []string) error {
const op = errors.Op("handle_serve_command")
rr, err := roadrunner.NewRR(*cfgFile, override, roadrunner.DefaultPluginsList())
if err != nil {
return errors.E(op, err)
}
errCh := make(chan error, 1)
go func() {
err = rr.Serve()
if err != nil {
errCh <- errors.E(op, err)
}
}()
oss, stop := make(chan os.Signal, 5), make(chan struct{}, 1) //nolint:gomnd
signal.Notify(oss, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
// first catch - stop the container
<-oss
// send signal to stop execution
stop <- struct{}{}
// after first hit we are waiting for the second
// second catch - exit from the process
<-oss
fmt.Println("exit forced")
os.Exit(1)
}()
for {
select {
case e := <-errCh:
return errors.E(op, e)
case <-stop: // stop the container after first signal
fmt.Printf("stop signal received\n")
if err = rr.Stop(); err != nil {
return fmt.Errorf("error: %w", err)
}
return nil
}
}
},
}
}
|