Skip to content

Commit

Permalink
Handle agent lock file on abrupt failures (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ale7714 authored Mar 18, 2024
1 parent 7946a55 commit d1d87e3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/viam-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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"))
}
}

Expand Down
2 changes: 2 additions & 0 deletions subsystems/viamagent/viam-agent.service
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d1d87e3

Please sign in to comment.