summaryrefslogtreecommitdiff
path: root/internal/cli/serve/command.go
blob: c508fafe79bfbde367f5d18f7564aadb8983200b (plain)
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
package serve

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/roadrunner-server/endure/v2"
	"github.com/roadrunner-server/roadrunner/v2/container"
	"github.com/roadrunner-server/roadrunner/v2/internal/meta"

	configImpl "github.com/roadrunner-server/config/v4"
	"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")
			// just to be safe
			if cfgFile == nil {
				return errors.E(op, errors.Str("no configuration file provided"))
			}

			// create endure container config
			containerCfg, err := container.NewConfig(*cfgFile)
			if err != nil {
				return errors.E(op, err)
			}

			cfg := &configImpl.Plugin{
				Path:    *cfgFile,
				Prefix:  rrPrefix,
				Timeout: containerCfg.GracePeriod,
				Flags:   *override,
				Version: meta.Version(),
			}

			endureOptions := []endure.Options{
				endure.GracefulShutdownTimeout(containerCfg.GracePeriod),
			}

			if containerCfg.PrintGraph {
				endureOptions = append(endureOptions, endure.Visualize())
			}

			// create endure container
			cont := endure.New(containerCfg.LogLevel, endureOptions...)

			// register plugins
			err = cont.RegisterAll(append(container.Plugins(), cfg)...)
			if err != nil {
				return errors.E(op, err)
			}

			// init container and all services
			err = cont.Init()
			if err != nil {
				return errors.E(op, err)
			}

			// start serving the graph
			errCh, err := cont.Serve()
			if err != nil {
				return 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)
			}()

			if !*silent {
				fmt.Printf("[INFO] RoadRunner server started; version: %s, buildtime: %s\n", meta.Version(), meta.BuildTime())
			}

			for {
				select {
				case e := <-errCh:
					return fmt.Errorf("error: %w\nplugin: %s", e.Error, e.VertexID)
				case <-stop: // stop the container after first signal
					fmt.Printf("stop signal received, grace timeout is: %0.f seconds\n", containerCfg.GracePeriod.Seconds())

					if err = cont.Stop(); err != nil {
						return fmt.Errorf("error: %w", err)
					}

					return nil
				}
			}
		},
	}
}