-
Notifications
You must be signed in to change notification settings - Fork 9
/
runtime.go
63 lines (58 loc) · 1.34 KB
/
runtime.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
package runtime
import (
"context"
"github.com/PxyUp/fitter/pkg/builder"
"github.com/PxyUp/fitter/pkg/config"
"github.com/PxyUp/fitter/pkg/logger"
"github.com/PxyUp/fitter/pkg/registry"
"github.com/PxyUp/fitter/pkg/trigger"
)
type runtime struct {
ctx context.Context
cfg *config.Config
logger logger.Logger
}
func New(ctx context.Context, cfg *config.Config, logger logger.Logger) *runtime {
return &runtime{
ctx: ctx,
cfg: cfg,
logger: logger,
}
}
func (r *runtime) Start() {
updates := make(chan *trigger.Message)
triggers := trigger.CreateTriggers(r.ctx, r.cfg, r.logger)
r.createRunTime(updates)
for _, t := range triggers {
t.Run(updates)
}
<-r.ctx.Done()
close(updates)
for _, t := range triggers {
t.Stop()
}
}
func (r *runtime) createRunTime(updates <-chan *trigger.Message) {
reg := registry.NewFromConfig(r.cfg, r.logger.With("registry", "runtime"))
go func() {
for {
select {
case <-r.ctx.Done():
return
case n, ok := <-updates:
if !ok {
return
}
lName := n
go func(name string, value builder.Interfacable) {
fields := []string{"name", name}
if value != nil {
fields = append(fields, "input", value.ToJson())
}
r.logger.Infow("new trigger comes", fields...)
_, _ = reg.Get(name).Process(value)
}(lName.Name, lName.Value)
}
}
}()
}