/
runner_service.go
261 lines (219 loc) · 6.1 KB
/
runner_service.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package daemon
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"github.com/Bitspark/go-funk"
"github.com/Bitspark/slang/pkg/api"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
type RunInstruction struct {
Id uuid.UUID `json:"id"`
Props core.Properties `json:"props"`
Gens core.Generics `json:"gens"`
Stream bool `json:"stream"`
}
type RunState struct {
Handle string `json:"handle,omitempty"`
URL string `json:"url,omitempty"`
Status string `json:"status"`
Error *Error `json:"error,omitempty"`
}
type runningOperator struct {
// JSON
Operator uuid.UUID `json:"operator"`
Handle string `json:"handle"`
URL string `json:"url"`
op *core.Operator
incoming chan interface{}
outgoing chan portOutput
inStop chan bool
outStop chan bool
}
type portOutput struct {
// JSON
Handle string `json:"handle"`
Port string `json:"port"`
Data interface{} `json:"data"`
IsEOS bool `json:"isEOS"`
IsBOS bool `json:"isBOS"`
port *core.Port
}
func (pm *portOutput) String() string {
j, _ := json.Marshal(pm)
return string(j)
}
type runningOperatorManager struct {
ops map[string]*runningOperator
}
var rnd = rand.New(rand.NewSource(99))
var runningOperators = &runningOperatorManager{make(map[string]*runningOperator)}
func (rom *runningOperatorManager) newRunningOperator(op *core.Operator) *runningOperator {
handle := strconv.FormatInt(rnd.Int63(), 16)
url := "/instance/" + handle + "/"
runningOp := &runningOperator{op.Id(), handle, url, op, make(chan interface{}, 0), make(chan portOutput, 0), make(chan bool, 0), make(chan bool, 0)}
rom.ops[handle] = runningOp
op.Main().Out().Bufferize()
op.Start()
log.Printf("operator %s (id: %s) started", op.Name(), handle)
return runningOp
}
func (rom *runningOperatorManager) Run(op *core.Operator) *runningOperator {
runningOp := rom.newRunningOperator(op)
go func() {
loop:
for {
select {
case incoming := <-runningOp.incoming:
op.Main().In().Push(incoming)
case <-runningOp.inStop:
break loop
}
}
}()
op.Main().Out().WalkPrimitivePorts(func(p *core.Port) {
go func() {
for {
if p.Closed() {
break
}
i := p.Pull()
po := portOutput{runningOp.Handle, p.String(), i, core.IsEOS(i), core.IsBOS(i), p}
runningOp.outgoing <- po
}
}()
})
return runningOp
}
func (rom *runningOperatorManager) Halt(handle string) error {
// `Halt` to me suggest that there is a way to resume operations
// which is not the case.
ro, err := runningOperators.Get(handle)
if err != nil {
return err
}
go ro.op.Stop()
ro.inStop <- true
ro.outStop <- true
delete(rom.ops, handle)
return nil
}
func (rom runningOperatorManager) Get(handle string) (*runningOperator, error) {
if runningOp, ok := rom.ops[handle]; ok {
return runningOp, nil
}
return nil, fmt.Errorf("unknown handle value: %s", handle)
}
var InstanceService = &Service{map[string]*Endpoint{
"/": {func(w http.ResponseWriter, r *http.Request) {
type outJSON struct {
Objects []runningOperator `json:"objects"`
Status string `json:"status"`
Error *Error `json:"error,omitempty"`
}
if r.Method == "GET" {
writeJSON(w, funk.Values(runningOperators.ops))
}
}},
}}
var RunningInstanceService = &Service{map[string]*Endpoint{
"/{handle:\\w+}/": {func(w http.ResponseWriter, r *http.Request) {
handle := mux.Vars(r)["handle"]
runningIns, err := runningOperators.Get(handle)
if err != nil {
w.WriteHeader(404)
return
}
var idat interface{}
if r.Method == "POST" {
r.ParseForm()
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
// An empty buffer would result into an error that is why we check the length
// and only than try to encode, because an empty POST is still valid and treated as trigger.
if buf.Len() > 0 {
err := json.Unmarshal(buf.Bytes(), &idat)
if err != nil {
w.WriteHeader(400)
return
}
}
runningIns.incoming <- idat
writeJSON(w, &runningIns)
}
}},
}}
var RunnerService = &Service{map[string]*Endpoint{
"/": {Handle: func(w http.ResponseWriter, r *http.Request) {
hub := GetHub(r)
st := GetStorage(r)
if r.Method == "POST" {
var data RunState
var ri RunInstruction
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&ri)
if err != nil {
data = RunState{Status: "error", Error: &Error{Msg: err.Error(), Code: "E000X"}}
writeJSON(w, &data)
return
}
opId := ri.Id
op, err := api.BuildAndCompile(opId, ri.Gens, ri.Props, st)
if err != nil {
data = RunState{Status: "error", Error: &Error{Msg: err.Error(), Code: "E000X"}}
writeJSON(w, &data)
return
}
runOp := runningOperators.Run(op)
// Move into the background and wait on message from the operator resp. ports
// and relay them through the `hub`
go func() {
loop:
for {
select {
case outgoing := <-runOp.outgoing:
// I don't know what happens when Root would be a dynamically changing variable.
// Is root's value bound to the scope or is the reference bound to the scope.
// I would suspect the latter, which means this is could turn into a race condition.
hub.broadCastTo(Root, Port, outgoing)
case <-runOp.outStop:
break loop
}
}
}()
data.Status = "success"
data.Handle = runOp.Handle
data.URL = runOp.URL
writeJSON(w, &data)
} else if r.Method == "DELETE" {
type stopInstructionJSON struct {
Handle string `json:"handle"`
}
type outJSON struct {
Status string `json:"status"`
Error *Error `json:"error,omitempty"`
}
var data outJSON
decoder := json.NewDecoder(r.Body)
var si stopInstructionJSON
err := decoder.Decode(&si)
if err != nil {
data = outJSON{Status: "error", Error: &Error{Msg: err.Error(), Code: "E000X"}}
writeJSON(w, &data)
return
}
if err := runningOperators.Halt(si.Handle); err == nil {
data.Status = "success"
} else {
data = outJSON{Status: "error", Error: &Error{Msg: "Unknown handle", Code: "E000X"}}
}
writeJSON(w, &data)
}
}},
}}