From d1d87e3ce1657335b6f96729cab8478b3d79dfaf Mon Sep 17 00:00:00 2001 From: Ale Paredes <1709578+ale7714@users.noreply.github.com> Date: Mon, 18 Mar 2024 12:55:46 -0400 Subject: [PATCH] Handle agent lock file on abrupt failures (#19) --- cmd/viam-agent/main.go | 12 +++++++++--- subsystems/viamagent/viam-agent.service | 2 ++ utils.go | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/viam-agent/main.go b/cmd/viam-agent/main.go index c877b12..8b7bb67 100644 --- a/cmd/viam-agent/main.go +++ b/cmd/viam-agent/main.go @@ -104,12 +104,18 @@ func main() { exitIfError(agent.InitPaths()) // use a lockfile to prevent running two agents on the same machine - pidFile, err := lockfile.New(filepath.Join(agent.ViamDirs["tmp"], "viam-agent.pid")) + pidFile, err := lockfile.New(filepath.Join(agent.ViamDirs["run"], "viam-agent.pid")) exitIfError(errors.Wrap(err, "cannot init lock file")) if err = pidFile.TryLock(); err != nil { globalLogger.Error(errors.Wrapf(err, "cannot lock %s", pidFile)) if errors.Is(err, lockfile.ErrBusy) { - globalLogger.Fatal("Please terminate any other copies of viam-agent and try again.") + globalLogger.Debug("Retrying to lock file") + + time.Sleep(2 * time.Second) + + if err = pidFile.TryLock(); err != nil { + globalLogger.Fatal("Please terminate any other copies of viam-agent and try again.") + } } } defer func() { @@ -153,7 +159,7 @@ func main() { if errors.Is(err, agent.ErrSubsystemDisabled) { globalLogger.Warn("provisioning subsystem disabled, please manually update /etc/viam.json and connect to internet") } else { - globalLogger.Error("could not start provisioning subsystem, please manually update /etc/viam.json and connect to internet") + globalLogger.Error(errors.Wrapf(err, "could not start provisioning subsystem, please manually update /etc/viam.json and connect to internet")) } } diff --git a/subsystems/viamagent/viam-agent.service b/subsystems/viamagent/viam-agent.service index 94d1a78..33d45e2 100644 --- a/subsystems/viamagent/viam-agent.service +++ b/subsystems/viamagent/viam-agent.service @@ -9,6 +9,8 @@ Restart=always RestartSec=5 User=root TimeoutSec=120 +RuntimeDirectory=viam +RuntimeDirectoryMode=0755 ExecStart=/opt/viam/bin/viam-agent --config /etc/viam.json FinalKillSignal=SIGQUIT diff --git a/utils.go b/utils.go index 09e9291..1af18e7 100644 --- a/utils.go +++ b/utils.go @@ -29,6 +29,7 @@ func init() { ViamDirs["cache"] = filepath.Join(ViamDirs["viam"], "cache") ViamDirs["tmp"] = filepath.Join(ViamDirs["viam"], "tmp") ViamDirs["etc"] = filepath.Join(ViamDirs["viam"], "etc") + ViamDirs["run"] = "/run/viam" } func InitPaths() error {