From bc26766b84aacdd4fca4c1120bf049e84fcc9509 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 27 Nov 2022 20:23:13 -0500 Subject: [PATCH] refactor: use exit code 137 to signify a lock was unable to be retrieved when calling ps:retire Closes #5432 --- plugins/ps/ps.go | 22 ++++++++++++++++++++++ plugins/ps/subcommands.go | 4 ++-- plugins/scheduler-docker-local/install | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/plugins/ps/ps.go b/plugins/ps/ps.go index 5f7aac83f..9d86e297b 100644 --- a/plugins/ps/ps.go +++ b/plugins/ps/ps.go @@ -24,6 +24,28 @@ var ( } ) +// RetireLockFailed wraps error to distinguish between a normal error +// and an error where the retire lock could not be fetched +type RetireLockFailed struct { + Err *error +} + +// ExitCode returns an exit code to use in case this error bubbles +// up into an os.Exit() call +func (err *RetireLockFailed) ExitCode() int { + return 137 +} + +// Error returns a standard non-existent app error +func (err *RetireLockFailed) Error() string { + if err.Err != nil { + e := *err.Err + return fmt.Sprintf("Failed to acquire ps:retire lock: %s", e.Error()) + } + + return fmt.Sprintf("Failed to acquire ps:retire lock") +} + // Formation contains scaling information for a given process type type Formation struct { ProcessType string `json:"process_type"` diff --git a/plugins/ps/subcommands.go b/plugins/ps/subcommands.go index 2d00b87b4..442ffe062 100644 --- a/plugins/ps/subcommands.go +++ b/plugins/ps/subcommands.go @@ -111,12 +111,12 @@ func CommandRetire(appName string) error { fileLock := flock.New(lockFile) locked, err := fileLock.TryLock() if err != nil { - return fmt.Errorf("Failed to acquire ps:retire lock: %s", err) + return &RetireLockFailed{&err} } defer fileLock.Unlock() if !locked { - return fmt.Errorf("Failed to acquire ps:retire lock") + return &RetireLockFailed{} } common.LogInfo1("Retiring old containers and images") diff --git a/plugins/scheduler-docker-local/install b/plugins/scheduler-docker-local/install index 05e2e32dc..319838e2a 100755 --- a/plugins/scheduler-docker-local/install +++ b/plugins/scheduler-docker-local/install @@ -27,6 +27,7 @@ After=docker.service Type=oneshot User=$DOKKU_SYSTEM_USER ExecStart=$DOKKU_PATH ps:retire +SuccessExitStatus=137 [Install] WantedBy=docker.service