-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
119 lines (90 loc) · 1.78 KB
/
engine.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
package loong
import (
"context"
"errors"
"log/slog"
)
type Engine struct {
Name string
Evaluator
TemplateGetter
Storer
IDGenerator
IoCaller
EventHandler
Txer
Logger *slog.Logger
ctx context.Context
liquid *liquid
bg Backgrounder
config *Config
isRunning bool
}
func NewEngine(name string, ops ...Option) *Engine {
config := &Config{
EventHandler: eh{},
Context: context.Background(),
IoCaller: IoSet{},
Logger: slog.Default(),
}
for _, op := range ops {
op(config)
}
e := &Engine{
Name: name,
Evaluator: NewExprEval(),
IDGenerator: uid{},
IoCaller: config.IoCaller,
TemplateGetter: config.templates,
Storer: config.Store,
Txer: config.Txer,
EventHandler: config.EventHandler,
Logger: config.Logger.With(slog.String("engine", name)),
ctx: config.Context,
config: config,
}
e.bg = bg{engine: e}
e.liquid = &liquid{
engine: e,
actCh: make(chan Activity, 1),
ech: make(chan Activity, 1),
logger: config.Logger.With(slog.String("driver", "liquid")),
size: 4,
}
return e
}
func (e *Engine) init() error {
return nil
}
func (e *Engine) Run() (err error) {
if e.isRunning {
return
}
if err = e.init(); err != nil {
return
}
if err = e.liquid.run(); err != nil {
return
}
logo()
e.isRunning = true
return
}
func (e *Engine) RunActivityCmd(ctx context.Context, activity ActivityCmd) error {
if !e.isRunning {
return errors.New("引擎未运行")
}
if err := activity.Bind(ctx, e); err != nil {
return err
}
return e.liquid.Emit(activity)
}
func (e *Engine) BackgroundCmd(ctx context.Context, cmd Cmd) error {
if !e.isRunning {
return errors.New("引擎未运行")
}
if err := cmd.Bind(ctx, e); err != nil {
return err
}
return e.bg.Background(cmd)
}