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 "sync"
11
12 pluginLib "gitroot.dev/libs/golang/plugin"
13 "gitroot.dev/server/logger"
14)
15
16type runtimes struct {
17 lockRuntime sync.Mutex
18 runtime *runtime
19 log *logger.Logger
20 start chan runtimeInputs
21}
22
23func newRuntimes(manager *Manager, log *logger.Logger) *runtimes {
24 chanStart := make(chan runtimeInputs)
25 r, err := newRuntime(context.Background(), manager, log)
26 if err != nil {
27 log.Error("can't create runtime", err)
28 }
29 go func() {
30 r.listen(chanStart)
31 }()
32 return &runtimes{
33 runtime: r,
34 log: log,
35 start: chanStart,
36 }
37}
38
39func (r *runtimes) Start(ctx context.Context, repoName string, plugins []Plugin, kind runtimeInputsKind, commands []CommandForDiff) {
40 r.lockRuntime.Lock()
41 defer r.lockRuntime.Unlock()
42 r.start <- runtimeInputs{
43 ctx: ctx,
44 repoName: repoName,
45 kind: kind,
46 plugins: plugins,
47 commands: commands,
48 }
49}
50
51func (r *runtimes) Conf(ctx context.Context, plugin Plugin) ([]pluginLib.PluginRun, error) {
52 timerStop := r.log.Time(fmt.Sprintf("Timer conf plugin %s", plugin.Name))
53 defer timerStop()
54 r.lockRuntime.Lock()
55 defer r.lockRuntime.Unlock()
56 return r.runtime.conf(ctx, plugin)
57}