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

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

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

	configImpl "github.com/roadrunner-server/config/v2"
	"github.com/roadrunner-server/errors"
	"github.com/spf13/cobra"
)

// NewCommand creates `serve` command.
func NewCommand(cfgPlugin *configImpl.Plugin) *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")

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

			// set the grace period which would be same for all the plugins
			cfgPlugin.Timeout = containerCfg.GracePeriod
			cfgPlugin.Version = meta.Version()

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

			// register config plugin
			if err = endureContainer.Register(cfgPlugin); err != nil {
				return errors.E(op, err)
			}

			// register another container plugins
			for i, plugins := 0, container.Plugins(); i < len(plugins); i++ {
				if err = endureContainer.Register(plugins[i]); err != nil {
					return errors.E(op, err)
				}
			}

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

			// start serving the graph
			errCh, err := endureContainer.Serve()
			if err != nil {
				return errors.E(op, err)
			}

			oss, stop := make(chan os.Signal, 2), 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)
			}()

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

			for {
				select {
				case e := <-errCh:
					fmt.Printf("error occurred: %v, plugin: %s\n", e.Error, e.VertexID)

					// return error, container already stopped internally
					if !containerCfg.RetryOnFail {
						return errors.E(op, e.Error)
					}

				case <-stop: // stop the container after first signal
					fmt.Printf("stop signal received, grace timeout is: %d seconds\n", uint64(containerCfg.GracePeriod.Seconds()))

					if err = endureContainer.Stop(); err != nil {
						fmt.Printf("error occurred during the stopping container: %v\n", err)
					}

					return nil
				}
			}
		},
	}
}