Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runmqserver: fix/improve verifySingleProcess #420

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 20 additions & 9 deletions cmd/runmqserver/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,35 @@ func verifySingleProcess() error {
}

// Verify that there is only one runmqserver
_, err = verifyOnlyOne(programName)
if err != nil {
one := verifyOnlyOne(programName)
if !one {
return fmt.Errorf("You cannot run more than one instance of this program")
}

return nil
}

// Verifies that there is only one instance running of the given program name.
func verifyOnlyOne(programName string) (int, error) {
func verifyOnlyOne(programName string) bool {
// #nosec G104
out, _, _ := command.Run("ps", "-e", "--format", "cmd")
//if this goes wrong then assume we are the only one
numOfProg := strings.Count(out, programName)
if numOfProg != 1 {
return numOfProg, fmt.Errorf("Expected there to be only 1 instance of %s but found %d", programName, numOfProg)
out, _, err := command.Run("pgrep", programName)
if err != nil {
// unable to verify: bad, but not fatal
log.Debug(err.Error())
return true
}
return numOfProg, nil
numOfProg := strings.Count(out, "\n")
switch numOfProg {
case 0:
// unable to verify: bad, but not fatal
log.Debugf("can't find %s using pgrep", programName)
return true
case 1:
return true
}

log.Errorf("Expected to have 1 instance of %s, got %d", programName, numOfProg)
return false
}

// Determines the name of the currently running executable.
Expand Down