This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.go
76 lines (60 loc) · 1.48 KB
/
plugin.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
package vm
import (
"context"
"errors"
"fmt"
"os"
"github.com/taubyte/go-interfaces/vm"
)
// TODO: Handle ma as multi-address
func Load(filename string, ctx context.Context) (vm.Plugin, error) {
if len(filename) < 1 {
return nil, errors.New("cannot load plugin from empty filename")
}
if _, err := os.Stat(filename); err != nil {
return nil, fmt.Errorf("stat `%s` failed with: %w", filename, err)
}
p := &vmPlugin{
origin: filename,
instances: make(map[*pluginInstance]interface{}),
}
if err := p.prepFile(); err != nil {
return nil, fmt.Errorf("prepping `%s` failed with: %w", p.origin, err)
}
if err := p.hashFile(); err != nil {
return nil, fmt.Errorf("hashing `%s` failed with: %w", p.origin, err)
}
p.ctx, p.ctxC = context.WithCancel(ctx)
p.connect()
if err := p.watch(); err != nil {
p.ctxC()
return nil, fmt.Errorf("watch on file `%s` failed with: %w", p.filename, err)
}
return p, nil
}
func (p *vmPlugin) Name() string {
return p.name
}
func (p *vmPlugin) New(instance vm.Instance) (vm.PluginInstance, error) {
pI, err := p.new(instance)
if err != nil {
return nil, fmt.Errorf("creating new plugin instance for plugin `%s` failed with: %w", p.name, err)
}
p.lock.Lock()
p.instances[pI] = nil
p.lock.Unlock()
return pI, nil
}
func (p *vmPlugin) Close() error {
p.lock.Lock()
defer p.lock.Unlock()
var err error
for pI := range p.instances {
if _err := pI.close(); _err != nil {
err = _err
}
}
p.ctxC()
p.proc.Kill()
return err
}