Repository navigation
Expand file tree
/
Copy pathruntime_command.go
More file actions
54 lines (51 loc) · 1.6 KB
/
Copy pathruntime_command.go
File metadata and controls
54 lines (51 loc) · 1.6 KB
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
package main
import (
"context"
"errors"
"path/filepath"
"github.com/MMinasyan/lightcode/internal/plugins/builtin"
"github.com/MMinasyan/lightcode/runtime"
)
// runRuntime runs the shared managed Runtime owner in the foreground for the
// lifetime of the signal context. It opens the owner over the normalized
// paths with the shipped plugin set, attaches the protocol and publishes the
// discovery record once, then waits for the shutdown signal and joins the
// managed cleanup. Contention exits quietly without initialization; every
// startup, publication and cleanup error keeps its identity for exit 1.
func runRuntime(ctx context.Context, dataDir, configPath string) (err error) {
absDataDir, err := filepath.Abs(dataDir)
if err != nil {
return err
}
absConfigPath, err := filepath.Abs(configPath)
if err != nil {
return err
}
owner, err := runtime.Open(ctx, runtime.Options{
DataDir: absDataDir,
ConfigPath: absConfigPath,
Plugins: builtin.Plugins(),
})
if errors.Is(err, runtime.ErrOwned) {
// Another process owns the data directory; this contender
// initializes nothing and exits without a diagnostic.
return nil
}
if err != nil {
return err
}
defer func() {
// The required cleanup always runs on its own wait — the canceled
// signal context never bounds it — and its result joins the return.
err = errors.Join(err, owner.Close(context.Background()))
}()
server, err := owner.OpenProtocol(ctx)
if err != nil {
return err
}
if err := server.PublishDiscovery(filepath.Join(absDataDir, "runtime.json")); err != nil {
return err
}
<-ctx.Done()
return nil
}