summaryrefslogtreecommitdiff
path: root/roadrunner/roadrunner.go
blob: 320e48c404ab323d39c0ea9a75aed23a62c37a46 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package roadrunner

import (
	"fmt"
	"runtime/debug"

	configImpl "github.com/roadrunner-server/config/v2"
	endure "github.com/roadrunner-server/endure/pkg/container"
	"github.com/roadrunner-server/endure/pkg/fsm"
	"github.com/roadrunner-server/roadrunner/v2/internal/container"
	"github.com/roadrunner-server/roadrunner/v2/internal/meta"
)

const (
	rrPrefix string = "rr"
)

type RR struct {
	container *endure.Endure
	stop      chan struct{}
	Version   string
}

// NewRR creates a new RR instance that can then be started or stopped by the caller
func NewRR(cfgFile string, override *[]string, pluginList []interface{}) (*RR, error) {
	// create endure container config
	containerCfg, err := container.NewConfig(cfgFile)
	if err != nil {
		return nil, err
	}

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

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

	// register config plugin
	err = endureContainer.Register(cfg)
	if err != nil {
		return nil, err
	}

	// register another container plugins
	err = endureContainer.RegisterAll(pluginList...)
	if err != nil {
		return nil, err
	}

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

	rr := &RR{
		container: endureContainer,
		stop:      make(chan struct{}),
		Version:   cfg.Version,
	}

	return rr, nil
}

// Serve starts RR and starts listening for requests.
// This is a blocking call that will return an error if / when one occurs in a plugin
func (rr *RR) Serve() error {
	// start serving the graph
	errCh, err := rr.container.Serve()
	if err != nil {
		return err
	}

	select {
	case e := <-errCh:
		return fmt.Errorf("error: %w\nplugin: %s", e.Error, e.VertexID)
	case <-rr.stop:
		return nil
	}
}

func (rr *RR) CurrentState() fsm.State {
	return rr.container.CurrentState()
}

// Stop stops roadrunner
func (rr *RR) Stop() error {
	rr.stop <- struct{}{}

	return rr.container.Stop()
}

// DefaultPluginsList returns all the plugins that RR can run with and are included by default
func DefaultPluginsList() []interface{} {
	return container.Plugins()
}

// Tries to find the version info for a given module's path
// empty string if not found
func getModuleVersion(modulePath string) string {
	bi, ok := debug.ReadBuildInfo()
	if !ok {
		return ""
	}

	for _, d := range bi.Deps {
		if d.Path == modulePath {
			return d.Version
		}
	}

	return ""
}

// Grabs RR's module version if available, meta.Version() otherwise
func getRRVersion() string {
	v := getModuleVersion("github.com/roadrunner-server/roadrunner/v2")
	if v == "" {
		return meta.Version()
	}

	if len(v) > 1 && ((v[0] == 'v' || v[0] == 'V') && (v[1] >= '0' && v[1] <= '9')) {
		return v[1:]
	}

	return v
}