GitRoot
craft your forge, build your project, grow your community freely
1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
2//
3// SPDX-License-Identifier: EUPL-1.2
4
5package plugin
6
7import (
8 "context"
9 "fmt"
10 "path/filepath"
11 "sync"
12
13 "github.com/tetratelabs/wazero"
14 pluginLib "gitroot.dev/libs/golang/plugin/model"
15 "gitroot.dev/server/logger"
16)
17
18type runtimes struct {
19 lockRuntime sync.Mutex
20 runtime *runtime
21 log *logger.Logger
22 start chan runtimeInputs
23}
24
25func newRuntimes(manager *Manager, log *logger.Logger) *runtimes {
26 chanStart := make(chan runtimeInputs)
27 compileCache, _ := wazero.NewCompilationCacheWithDir(filepath.Join(manager.conf.PathCache(), "wazero"))
28 runtimeConfig := wazero.NewRuntimeConfig().WithCompilationCache(compileCache)
29 r, err := newRuntime(context.Background(), manager, log, runtimeConfig)
30 if err != nil {
31 log.Error("can't create runtime", err)
32 }
33 go func() {
34 r.listen(chanStart)
35 }()
36 return &runtimes{
37 runtime: r,
38 log: log,
39 start: chanStart,
40 }
41}
42
43func (r *runtimes) Compile(ctx context.Context, binary []byte) (wazero.CompiledModule, error) {
44 r.lockRuntime.Lock()
45 defer r.lockRuntime.Unlock()
46 return r.runtime.wazRun.CompileModule(ctx, binary)
47}
48
49func (r *runtimes) Start(ctx context.Context, repoName string, plugins []Plugin, kind runtimeInputsKind, commands []CommandForDiff) {
50 r.lockRuntime.Lock()
51 defer r.lockRuntime.Unlock()
52 r.start <- runtimeInputs{
53 ctx: ctx,
54 repoName: repoName,
55 kind: kind,
56 plugins: plugins,
57 commands: commands,
58 }
59}
60
61func (r *runtimes) Conf(ctx context.Context, plugin Plugin) (*pluginLib.ConfPlugin, error) {
62 timerStop := r.log.Time(fmt.Sprintf("Timer conf plugin %s", plugin.Name))
63 defer timerStop()
64 r.lockRuntime.Lock()
65 defer r.lockRuntime.Unlock()
66 return r.runtime.conf(ctx, plugin)
67}