Skip to content
Merged
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
63 changes: 37 additions & 26 deletions cmd/internal/agent_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,33 @@ func (cmd *DaemonCmd) Run(ctx context.Context) error {
if err != nil {
return err
}
runtimeLockPath := daemonRuntimeLockPath
runtimeLockPath, locatorPath := daemonRuntimePaths()
runtimeLock, err := machinediagnostics.AcquireRuntimeLock(runtimeLockPath)
if err != nil {
return err
}
defer func() { _ = runtimeLock.Close() }()
cmd.startDiagnostics(location, locatorPath)
defer cmd.stopDiagnostics()

log.Infof("starting Devsy daemon patrol at %s", location.Root)
cmd.patrol(ctx)
return nil
}

func daemonRuntimePaths() (runtimeLockPath, locatorPath string) {
runtimeLockPath = daemonRuntimeLockPath
if fallbackPath := os.Getenv(machinediagnostics.RuntimeLockPathEnv); fallbackPath != "" {
runtimeLockPath = fallbackPath
}
locatorPath := daemonLocatorPath
locatorPath = daemonLocatorPath
if fallbackPath := os.Getenv(machinediagnostics.RuntimeLocatorPathEnv); fallbackPath != "" {
locatorPath = fallbackPath
}
runtimeLock, err := machinediagnostics.AcquireRuntimeLock(runtimeLockPath)
if err != nil {
return err
}
defer func() { _ = runtimeLock.Close() }()
return runtimeLockPath, locatorPath
}

func (cmd *DaemonCmd) startDiagnostics(location agentdaemon.StateLocation, locatorPath string) {
cmd.initDiagnostics(location, locatorPath)
cmd.recordEvent(
machinediagnostics.Event{
Expand All @@ -131,26 +145,23 @@ func (cmd *DaemonCmd) Run(ctx context.Context) error {
health: machinediagnostics.DaemonHealthy,
},
)
defer func() {
cmd.recordEvent(
machinediagnostics.Event{
Type: machinediagnostics.EventDaemonStopping,
Level: machinediagnostics.LevelInfo,
Message: "Devsy machine daemon stopping.",
},
)
cmd.updateDiagnostics(
diagnosticsUpdate{
state: machinediagnostics.DaemonStopping,
health: machinediagnostics.DaemonHealthy,
},
)
_ = cmd.recorder.Close()
}()
}

log.Infof("starting Devsy daemon patrol at %s", location.Root)
cmd.patrol(ctx)
return nil
func (cmd *DaemonCmd) stopDiagnostics() {
cmd.recordEvent(
machinediagnostics.Event{
Type: machinediagnostics.EventDaemonStopping,
Level: machinediagnostics.LevelInfo,
Message: "Devsy machine daemon stopping.",
},
)
cmd.updateDiagnostics(
diagnosticsUpdate{
state: machinediagnostics.DaemonStopping,
health: machinediagnostics.DaemonHealthy,
},
)
_ = cmd.recorder.Close()
}

func (cmd *DaemonCmd) initDiagnostics(location agentdaemon.StateLocation, locatorPath string) {
Expand Down
Loading