-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
94 lines (80 loc) · 2.75 KB
/
controller.go
File metadata and controls
94 lines (80 loc) · 2.75 KB
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
package launchcontrol
import (
"context"
"fmt"
"log/slog"
"github.com/spikesdivzero/launch-control/internal/controller"
)
// Controller is the primary interface into this package.
//
// The Controller tracks the lifecycle of all components spawned within it, and offers a way to
// block until all components have exited.
type Controller struct {
ic *controller.Controller
}
// New returns a new Controller, using the provided context as it's root context.
//
// Any contexts passed into a component will be children of this context.
func New(ctx context.Context) *Controller {
return &Controller{
ic: controller.NewController(ctx),
}
}
// Launch builds and then runs/starts a component, using the provided Options.
//
// At least one Options must be provided. The provided Options are all merged down into a
// single set of Options. The final Options provided ot this call must provide Run or Start
// function, along with a Stop function.
//
// In the that the provided Options fail validation, the Launch call will panic.
//
// If the controller is shutting down or dead, then the Launch request will be ignored.
func (c *Controller) Launch(name string, opts ...Options) {
if name == "" {
panic("Controller.Launch: must provide a name")
}
if len(opts) == 0 {
panic("Controller.Launch: must pass at least one Options")
}
merged, err := mergeOptions(opts)
if err != nil {
panic(fmt.Sprintf("Controller.Launch: option validation failed: %v", err))
}
if err := merged.validate(); err != nil {
panic(fmt.Sprintf("Controller.Launch: option validation failed: %v", err))
}
comp := merged.buildComponent(name)
c.ic.Launch(name, comp)
}
// RequestStop asks that the controller begin the shutdown process, with the provided reason.
//
// If the reason is not nil, then it's recorded as an error, which can returned by
// [Controller.Err] or [Controller.AllErrors].
func (c *Controller) RequestStop(reason error) {
c.ic.RequestStop(reason)
}
// Wait blocks until all components have finished shutting down.
//
// It returns the value from [Controller.Err] for convenience.
//
// If Wait is called before any components have been launched, then the call panics.
func (c *Controller) Wait() error {
c.ic.Wait()
return c.Err()
}
// Err returns the first non-nill error observed by the controller.
//
// In the event that no errors have been observed, this returns nil.
func (c *Controller) Err() error {
return c.ic.Err()
}
// AllErrors returns a slice containing all non-nill errors observed by the controller.
func (c *Controller) AllErrors() []error {
return c.ic.AllErrors()
}
// SetLogger allows you to enable logging for this package.
//
// The default logger used by this package discards all records.
func (c *Controller) SetLogger(log *slog.Logger) {
c.ic.SetLogger(log)
}