Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions cmd/internal/agent_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,7 @@ func (cmd *DaemonCmd) Run(ctx context.Context) error {
return err
}
defer func() { _ = runtimeLock.Close() }()
cmd.startedAt = time.Now().UTC()
recorder, err := machinediagnostics.NewRecorder(machinediagnostics.Options{
Dir: machinediagnostics.DiagnosticsDir(location.Root),
Reader: cmd.diagnosticsReader(),
PatrolInterval: cmd.pollInterval(),
ErrorReporter: func(err error) {
log.Warnf("write machine diagnostics: %v", err)
},
})
if err != nil {
log.Warnf("initialize machine diagnostics: %v", err)
cmd.recorder = machinediagnostics.Nop()
} else {
cmd.recorder = recorder
if err := machinediagnostics.WriteLocator(
locatorPath,
machinediagnostics.Locator{
SessionID: recorder.SessionID(),
DiagnosticsDir: machinediagnostics.DiagnosticsDir(location.Root),
StateLayout: string(location.Layout),
StartedAt: cmd.startedAt,
},
); err != nil {
log.Warnf("write machine diagnostics locator: %v", err)
}
}
cmd.initDiagnostics(location, locatorPath)
cmd.recordEvent(
machinediagnostics.Event{
Type: machinediagnostics.EventDaemonStarted,
Expand Down Expand Up @@ -178,6 +153,35 @@ func (cmd *DaemonCmd) Run(ctx context.Context) error {
return nil
}

func (cmd *DaemonCmd) initDiagnostics(location agentdaemon.StateLocation, locatorPath string) {
cmd.startedAt = time.Now().UTC()
recorder, err := machinediagnostics.NewRecorder(machinediagnostics.Options{
Dir: machinediagnostics.DiagnosticsDir(location.Root),
Reader: cmd.diagnosticsReader(),
PatrolInterval: cmd.pollInterval(),
ErrorReporter: func(err error) {
log.Warnf("write machine diagnostics: %v", err)
},
})
if err != nil {
log.Warnf("initialize machine diagnostics: %v", err)
cmd.recorder = machinediagnostics.Nop()
return
}
cmd.recorder = recorder
if err := machinediagnostics.WriteLocator(
locatorPath,
machinediagnostics.Locator{
SessionID: recorder.SessionID(),
DiagnosticsDir: machinediagnostics.DiagnosticsDir(location.Root),
StateLayout: string(location.Layout),
StartedAt: cmd.startedAt,
},
); err != nil {
log.Warnf("write machine diagnostics locator: %v", err)
}
}

func (cmd *DaemonCmd) stateLocation() (agentdaemon.StateLocation, error) {
if cmd.StateRoot != "" || cmd.StateLayout != "" {
if cmd.StateRoot == "" || cmd.StateLayout == "" {
Expand Down
47 changes: 47 additions & 0 deletions cmd/internal/agent_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/devsy-org/devsy/cmd/flags"
"github.com/devsy-org/devsy/pkg/agent"
agentdaemon "github.com/devsy-org/devsy/pkg/daemon/agent"
"github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/devsy-org/devsy/pkg/machinediagnostics"
provider2 "github.com/devsy-org/devsy/pkg/provider"
Expand Down Expand Up @@ -455,3 +456,49 @@ func TestEvaluateMachineInactivityBlocksDisabledWorkspace(t *testing.T) {
assert.True(t, evaluation.statuses[0].BlocksMachineShutdown)
assert.Equal(t, "Auto-stop is not configured", evaluation.statuses[0].BlockerReason)
}

func TestInitDiagnostics(t *testing.T) {
t.Run("publishes a locator for the active recorder", func(t *testing.T) {
locatorPath := filepath.Join(t.TempDir(), "locator.json")
t.Setenv(machinediagnostics.RuntimeLocatorPathEnv, locatorPath)
root := t.TempDir()
cmd := &DaemonCmd{DiagnosticsReaderUID: os.Getuid(), DiagnosticsReaderGID: os.Getgid()}

cmd.initDiagnostics(
agentdaemon.StateLocation{Root: root, Layout: agentdaemon.StateLayoutCanonical},
locatorPath,
)
store, ok := cmd.recorder.(*machinediagnostics.Store)
require.True(t, ok)
t.Cleanup(func() { _ = store.Close() })

data, err := os.ReadFile(filepath.Clean(locatorPath))
require.NoError(t, err)
var locator machinediagnostics.Locator
require.NoError(t, json.Unmarshal(data, &locator))
assert.Equal(t, store.SessionID(), locator.SessionID)
assert.Equal(t, machinediagnostics.DiagnosticsDir(root), locator.DiagnosticsDir)
assert.Equal(t, string(agentdaemon.StateLayoutCanonical), locator.StateLayout)
assert.True(t, cmd.startedAt.Equal(locator.StartedAt))
})

t.Run("falls back to a nop recorder without a locator", func(t *testing.T) {
locatorPath := filepath.Join(t.TempDir(), "locator.json")
t.Setenv(machinediagnostics.RuntimeLocatorPathEnv, locatorPath)
cmd := &DaemonCmd{DiagnosticsReaderUID: os.Getuid(), DiagnosticsReaderGID: os.Getgid()}

cmd.initDiagnostics(
agentdaemon.StateLocation{
Root: "relative-state",
Layout: agentdaemon.StateLayoutCanonical,
},
locatorPath,
)

require.NotNil(t, cmd.recorder)
_, isStore := cmd.recorder.(*machinediagnostics.Store)
assert.False(t, isStore)
_, err := os.Stat(locatorPath)
assert.ErrorIs(t, err, os.ErrNotExist)
})
}
Loading