-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
176 lines (136 loc) · 3.63 KB
/
app.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mikado
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
)
var ErrNotRunnable = errors.New("not runnable")
type typeKey struct {
Type reflect.Type
}
func (k typeKey) String() string {
return k.Type.String()
}
type App struct {
logger
providers []*provider
runnables []typeKey
values map[typeKey]reflect.Value
}
func New() *App {
return &App{
logger: defaultLogger,
values: make(map[typeKey]reflect.Value),
}
}
// AddProvider declares a provider for one or more components.
func (a *App) AddProvider(constructor interface{}) {
a.providers = append(a.providers, newProvider(constructor))
}
// AddRunnable declares a provider for one or more components that implements the
// github.com/pior/runnable.Runnable interface.
func (a *App) AddRunnable(constructor interface{}) {
p := newProvider(constructor)
a.providers = append(a.providers, p)
for _, output := range p.outputTypes {
if !_isErrorType(output) {
a.runnables = append(a.runnables, typeKey{output})
}
}
}
// Run instantiates all runnable components previously declared, and all their dependencies.
// Then, the runnable components are started (dependencies first).
// The running components are stopped when the context.Context is cancelled, or when a runnable prematurely exits.
func (a *App) Run(ctx context.Context) error {
// 1. instantiate all components
for _, runnable := range a.runnables {
a.logger("building: %s", runnable)
_, err := a.getValue(runnable)
if err != nil {
return fmt.Errorf("building %s: %w", runnable, err)
}
}
// 2. start all runnable
ctx, cancelCtx := context.WithCancel(ctx)
defer cancelCtx()
var wg sync.WaitGroup
for _, runnable := range a.runnables {
a.logger("starting: %s", runnable)
err := a.start(ctx, runnable, &wg)
if err != nil {
a.logger("runnable failed to start: %s", runnable)
cancelCtx()
break
}
}
<-ctx.Done()
a.logger("context cancelled, starting shutdown")
// 3. TODO: cancel component in reverse order
wg.Wait()
return nil
}
func (a *App) start(ctx context.Context, key typeKey, wg *sync.WaitGroup) error {
component, err := a.getValue(key)
if err != nil {
return err
}
if component.Kind() == reflect.Interface {
component = component.Elem() // the interface may not cover the Runnable interface, but the type may.
}
componentRunFunc := component.MethodByName("Run")
if !componentRunFunc.IsValid() {
return ErrNotRunnable
}
wg.Add(1)
go func() {
a.logger("starting runnable: %s", key)
args := []reflect.Value{reflect.ValueOf(ctx)}
results := componentRunFunc.Call(args)
a.logger("component %s returned: %s", component, results)
wg.Done()
}()
return nil
}
func (a *App) getValue(key typeKey) (reflect.Value, error) {
if v, ok := a.values[key]; ok {
return v, nil
}
p := findProvider(a, key)
if p == nil {
return reflect.Value{}, errors.New("not found")
}
a.logger("calling provider: %s", p.name)
err := a.callProvider(p)
if err != nil {
return reflect.Value{}, err
}
if v, ok := a.values[key]; ok {
return v, nil
}
panic("bug")
}
func (a *App) setValue(key typeKey, value reflect.Value) {
a.values[key] = value
}
func (a *App) callProvider(p *provider) error {
var inputs []reflect.Value
for _, inputType := range p.inputTypes {
arg, err := a.getValue(typeKey{inputType})
if err != nil {
return err
}
inputs = append(inputs, arg)
}
results := p.call(inputs)
for idx, output := range p.outputTypes {
value := results[idx]
if _isErrorType(output) && !value.IsNil() {
err := value.Interface().(error)
return fmt.Errorf("provider %s returned an error: %w", p.name, err)
}
a.setValue(typeKey{output}, value)
}
return nil
}