-
Notifications
You must be signed in to change notification settings - Fork 24
/
worker.go
74 lines (70 loc) · 1.96 KB
/
worker.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
package engine
import (
"github.com/pkg/errors"
"github.com/runabol/tork/conf"
"github.com/runabol/tork/internal/worker"
"github.com/runabol/tork/runtime"
"github.com/runabol/tork/runtime/docker"
"github.com/runabol/tork/runtime/shell"
)
func (e *Engine) initWorker() error {
// init the runtime
rt, err := e.initRuntime()
if err != nil {
return err
}
w, err := worker.NewWorker(worker.Config{
Broker: e.broker,
Runtime: rt,
Queues: conf.IntMap("worker.queues"),
Limits: worker.Limits{
DefaultCPUsLimit: conf.String("worker.limits.cpus"),
DefaultMemoryLimit: conf.String("worker.limits.memory"),
},
Address: conf.String("worker.address"),
Middleware: e.cfg.Middleware.Task,
})
if err != nil {
return errors.Wrapf(err, "error creating worker")
}
if err := w.Start(); err != nil {
return err
}
e.worker = w
return nil
}
func (e *Engine) initRuntime() (runtime.Runtime, error) {
if e.runtime != nil {
return e.runtime, nil
}
runtimeType := conf.StringDefault("runtime.type", runtime.Docker)
switch runtimeType {
case runtime.Docker:
mounter, ok := e.mounters[runtime.Docker]
if !ok {
mounter = runtime.NewMultiMounter()
}
// register bind mounter
bm := docker.NewBindMounter(docker.BindConfig{
Allowed: conf.Bool("mounts.bind.allowed"),
Allowlist: conf.Strings("mounts.bind.allowlist"),
Denylist: conf.Strings("mounts.bind.denylist"),
})
mounter.RegisterMounter("bind", bm)
// register volume mounter
vm, err := docker.NewVolumeMounter()
if err != nil {
return nil, err
}
mounter.RegisterMounter("volume", vm)
return docker.NewDockerRuntime(docker.WithMounter(mounter))
case runtime.Shell:
return shell.NewShellRuntime(shell.Config{
CMD: conf.Strings("runtime.shell.cmd"),
UID: conf.StringDefault("runtime.shell.uid", shell.DEFAULT_UID),
GID: conf.StringDefault("runtime.shell.gid", shell.DEFAULT_GID),
}), nil
default:
return nil, errors.Errorf("unknown runtime type: %s", runtimeType)
}
}