-
Notifications
You must be signed in to change notification settings - Fork 50
/
runners.go
88 lines (68 loc) · 2.38 KB
/
runners.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
package application
import (
"context"
"fmt"
"time"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/exec"
"github.com/justtrackio/gosoline/pkg/httpserver"
"github.com/justtrackio/gosoline/pkg/kernel"
"github.com/justtrackio/gosoline/pkg/log"
"github.com/justtrackio/gosoline/pkg/mdlsub"
"github.com/justtrackio/gosoline/pkg/stream"
)
func Run(options ...Option) {
app := Default(options...)
app.Run()
}
func RunFunc(f func(ctx context.Context, config cfg.Config, logger log.Logger) (kernel.ModuleRunFunc, error), options ...Option) {
options = append(options, WithModuleFactory("func", func(ctx context.Context, config cfg.Config, logger log.Logger) (kernel.Module, error) {
var err error
var run kernel.ModuleRunFunc
if run, err = f(ctx, config, logger); err != nil {
return nil, fmt.Errorf("can not build run function: %w", err)
}
return kernel.NewModuleFunc(run), nil
}))
Run(options...)
}
func RunModule(name string, moduleFactory kernel.ModuleFactory, options ...Option) {
options = append(options, WithModuleFactory(name, moduleFactory))
Run(options...)
}
func RunHttpDefaultServer(definer httpserver.Definer, options ...Option) {
RunHttpServers(
map[string]httpserver.Definer{
"default": definer,
},
options...,
)
}
func RunHttpServers(servers map[string]httpserver.Definer, options ...Option) {
options = append(options, WithExecBackoffSettings(&exec.BackoffSettings{
InitialInterval: time.Millisecond * 100,
MaxElapsedTime: time.Second * 10,
MaxInterval: time.Second,
}))
for name, definer := range servers {
options = append(options, WithModuleFactory("httpserver-"+name, httpserver.New(name, definer)))
}
Run(options...)
}
func RunConsumer(callback stream.ConsumerCallbackFactory, options ...Option) {
RunConsumers(stream.ConsumerCallbackMap{
"default": callback,
}, options...)
}
func RunConsumers(consumers stream.ConsumerCallbackMap, options ...Option) {
factory := stream.NewConsumerFactory(consumers)
options = append(options, WithModuleMultiFactory(factory))
options = append(options, WithExecBackoffInfinite)
Run(options...)
}
func RunMdlSubscriber(transformers mdlsub.TransformerMapTypeVersionFactories, options ...Option) {
subs := mdlsub.NewSubscriberFactory(transformers)
options = append(options, WithModuleMultiFactory(subs))
options = append(options, WithExecBackoffInfinite)
Run(options...)
}