-
Notifications
You must be signed in to change notification settings - Fork 0
/
procload-go.go
298 lines (271 loc) · 7.99 KB
/
procload-go.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package vorlage
import (
"ellem.so/vorlageproc"
"io/ioutil"
"os"
"plugin"
)
type goProc struct {
sourcefile string
plugin *plugin.Plugin
vorlageStartup func() (vorlageproc.ProcessorInfo, error)
vorlageOnRequest func(info vorlageproc.RequestInfo, i *interface{}) []vorlageproc.Action
vorlageDefineVariable func(info vorlageproc.DefineInfo, i interface{}) vorlageproc.Definition
vorlageOnFinish func(info vorlageproc.RequestInfo, i interface{})
vorlageShutdown func() error
// set in NewCompiler
indexincompiler int
}
func goProchandleerr(err error, ok bool, s string) error {
if err != nil {
return lmerrorNew(0x153b42,
"symbol not found",
err,
"ensure the processor was properly built and is up to date",
s)
}
if !ok {
return lmerrorNew(0x153b41,
"symbol not valid",
nil,
"ensure the processor was properly built and is up to date",
s)
}
return nil
}
// used for debugging
func GoAddToPreload(f func() []vorlageproc.VorlageGo) {
// v2 symbol is valid. Make the call.
v2procs := f()
gv := make([]*goProc, len(v2procs))
for i := range v2procs {
gv[i] = new(goProc)
gv[i].sourcefile = "__INTERNAL__"
gv[i].plugin = nil
gv[i].vorlageStartup = v2procs[i].VorlageStartup
gv[i].vorlageOnRequest = v2procs[i].VorlageOnRequest
gv[i].vorlageDefineVariable = v2procs[i].VorlageDefineVariable
gv[i].vorlageOnFinish = v2procs[i].VorlageOnFinish
gv[i].vorlageShutdown = v2procs[i].VorlageShutdown
}
// v2 symbol linked successfully.
preLoadGo = append(preLoadGo, gv...)
}
var preLoadGo []*goProc
// parses a file and returns 1 or many goProc to be used as a vorlageproc.Processor(s).
// does NOT run .VorlageStartup().
func loadGoProc(path string, fname string) (gv []*goProc, err error) {
plug, err := plugin.Open(path)
if err != nil {
return gv, lmerrorNew(3185,
"failed to open plugin file",
err,
"make sure the file is valid",
path)
}
var ok bool
var sym plugin.Symbol
var v2procs []vorlageproc.VorlageGo
// Lets look for the V2 interface...
var vorlagegov func() []vorlageproc.VorlageGo
sym, err = plug.Lookup("VorlageGoV")
if err != nil {
// symbol not found. Go to the v1 interface
goto v1
}
vorlagegov, ok = sym.(func() []vorlageproc.VorlageGo)
if e := goProchandleerr(err, ok, "VorlageGoV"); e != nil {
return gv, e
}
// v2 symbol is valid. Make the call.
v2procs = vorlagegov()
gv = make([]*goProc, len(v2procs))
for i := range v2procs {
gv[i] = new(goProc)
gv[i].sourcefile = fname
gv[i].plugin = plug
gv[i].vorlageStartup = v2procs[i].VorlageStartup
gv[i].vorlageOnRequest = v2procs[i].VorlageOnRequest
gv[i].vorlageDefineVariable = v2procs[i].VorlageDefineVariable
gv[i].vorlageOnFinish = v2procs[i].VorlageOnFinish
gv[i].vorlageShutdown = v2procs[i].VorlageShutdown
}
// v2 symbol linked successfully.
return gv, nil
v1:
g := goProc{}
g.plugin = plug
sym, err = g.plugin.Lookup("VorlageStartup")
if err == nil {
g.vorlageStartup, ok = sym.(func() (vorlageproc.ProcessorInfo, error))
}
if e := goProchandleerr(err, ok, "VorlageStartup"); e != nil {
return gv, e
}
sym, err = g.plugin.Lookup("VorlageOnRequest")
if err == nil {
g.vorlageOnRequest, ok = sym.(func(info vorlageproc.RequestInfo, i *interface{}) []vorlageproc.Action)
}
if e := goProchandleerr(err, ok, "VorlageOnRequest"); e != nil {
return gv, e
}
sym, err = g.plugin.Lookup("VorlageDefineVariable")
if err == nil {
g.vorlageDefineVariable, ok = sym.(func(info vorlageproc.DefineInfo, i interface{}) vorlageproc.Definition)
}
if e := goProchandleerr(err, ok, "VorlageDefineVariable"); e != nil {
return gv, e
}
sym, err = g.plugin.Lookup("VorlageOnFinish")
if err == nil {
g.vorlageOnFinish, ok = sym.(func(info vorlageproc.RequestInfo, i interface{}))
}
if e := goProchandleerr(err, ok, "VorlageOnFinish"); e != nil {
return gv, e
}
sym, err = g.plugin.Lookup("VorlageShutdown")
if err == nil {
g.vorlageShutdown, ok = sym.(func() error)
}
if e := goProchandleerr(err, ok, "VorlageShutdown"); e != nil {
return gv, e
}
// good link for v1
return []*goProc{&g}, nil
}
func (g goProc) Startup() (vorlageproc.ProcessorInfo, error) {
r, err := g.vorlageStartup()
if err != nil {
return r, err
}
return r, nil
}
func (g goProc) OnRequest(info vorlageproc.RequestInfo, i *interface{}) []vorlageproc.Action {
return g.vorlageOnRequest(info, i)
}
func (g goProc) DefineVariable(info vorlageproc.DefineInfo, i interface{}) vorlageproc.Definition {
return g.vorlageDefineVariable(info, i)
}
func (g goProc) OnFinish(info vorlageproc.RequestInfo, i interface{}) {
g.vorlageOnFinish(info, i)
}
func (g goProc) Shutdown() error {
return g.vorlageShutdown()
}
var _ vorlageproc.Processor = goProc{}
// reloadindex can be nil
// reloadindex will channel in indexes from this returned array that need to
// be reloaded because they were changed.
// If you want to shut the watcher down, just jam a -1 in that channel
func loadGoProcessors(dir string) ([]*goProc, error) {
var procs []*goProc
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, f := range files {
if f.IsDir() {
continue
}
if !validGoProcName(f.Name()) {
continue
}
path := dir + "/" + f.Name()
if dir == "" {
path = f.Name()
}
p, err := loadGoProc(path, f.Name())
if err != nil {
return procs, lmerrorNew(0x19945,
"failed to load go library",
err,
"",
path)
}
var pconv = make([]*goProc, len(p))
for i := range pconv {
pconv[i] = p[i]
}
procs = append(procs, pconv...)
Logger.Debugf("loaded golang elf %s from %s (%d processors)", f.Name(), path, len(p))
}
procs = append(procs, preLoadGo...)
return procs, nil
}
func validGoProcName(fname string) bool {
libnames := goLibraryFilenameSig.FindStringSubmatch(fname)
if libnames == nil {
Logger.Debugf("%s - not valid name format to be considered as golang processor", fname)
return false
}
return true
}
func (c *Compiler) watchGoPath(path string) {
w, err := newwatcher(path)
if err != nil {
Logger.Alertf("watcher failed: %s", err)
return
}
c.gowatcher = &w
defer w.close()
var filename string
for {
filename, err = w.waitForUpdate()
if err != nil {
Logger.Alertf("watcher failed to wait for update (will be closing): %s", err)
w.closederr = err
w.closed = true
return
}
// a file was just closed from being written too...
// get the file's info
fullpath := path + "/" + filename
stat, err := os.Stat(fullpath)
if err != nil {
Logger.Noticef("auto-reload detected file %s in %s but failed to get a stat on it, skipping.", filename, path)
continue
}
// is it a directory?
if stat.IsDir() {
Logger.Debugf("new file %s placed in %s is a directory, auto-reload doing nothing.", filename, path)
continue
}
// is it a valid name?
if !validGoProcName(filename) {
continue
}
// okay so at this point we know they just moved in / replaced a file
// that is attempting to be a valid processor.
newprocs, err := loadGoProc(fullpath, filename)
if err != nil {
Logger.Errorf("auto-detect failed to load new go processor: %s", err)
continue
}
// and now we know it IS a valid processor, go forth update it.
Logger.Infof("new valid processor detected (%s)", fullpath)
err = c.updategoproc(filename, newprocs)
if err != nil {
Logger.Alertf("watcher failed to wait for update (will be closing): %s", err)
w.closederr = err
w.closed = true
return
}
}
}
func (c *Compiler) updategoproc(filename string, newprocs []*goProc) (err error) {
// lets begin to stall new compiles until we get this thing loaded in.
c.makestall(4)
// and when everything is sorted out, remove the stall.
defer c.cont()
// was this new file replacing an old one that had previously gave us
// processors?
for i := range c.goprocessors {
if c.goprocessors[i].sourcefile == filename {
// set them to be deleted. they are outdated.
c.goprocessors[i] = nil
}
}
// add the new processors
c.goprocessors = append(c.goprocessors, newprocs...)
return c.rebuildProcessors()
}