-
Notifications
You must be signed in to change notification settings - Fork 82
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
fix: mark the work as failed if exact command runner does not exist after receptor restart #716
base: devel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import ( | |
"github.com/ansible/receptor/pkg/logger" | ||
"github.com/ghjm/cmdline" | ||
"github.com/google/shlex" | ||
"github.com/shirou/gopsutil/v3/process" | ||
) | ||
|
||
// commandUnit implements the WorkUnit interface for the Receptor command worker plugin. | ||
|
@@ -239,6 +240,35 @@ func (cw *commandUnit) Start() error { | |
return cw.runCommand(cmd) | ||
} | ||
|
||
// isOrphaned checks if command runner for this unit exists | ||
func (cw *commandUnit) isOrphaned() (bool, error) { | ||
pid := cw.Status().ExtraData.(*commandExtraData).Pid | ||
p, err := process.NewProcess(int32(pid)) | ||
if err != nil { | ||
// process does not exist | ||
return true, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im finding the name of the function a little confusing here. If the process dosent exist, how can it also be an orphaned process? Perhaps changing the name to something like "isPidValid()" and reversing the bools returned will make a little more sense? What do you think? |
||
} | ||
|
||
cmd, err := p.CmdlineSlice() | ||
if err != nil { | ||
return true, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im worried here that receptor may not have privilege to view the cmd line slice, do you know if their are any restrictions on which type of user can see this data? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e.g. do we need sudo? do we need to be in a user group etc. |
||
} | ||
|
||
if len(cmd) == 0 || cmd[0] != os.Args[0] { | ||
// process is not receptor | ||
return true, fmt.Errorf("pid %v is not command runner", pid) | ||
} | ||
unitdirArg := fmt.Sprintf("unitdir=%s", cw.UnitDir()) | ||
for _, arg := range cmd { | ||
if arg == unitdirArg { | ||
// process is receptor and is using expected unitdir | ||
return false, nil | ||
} | ||
} | ||
// process is receptor but is using different unitdir | ||
return true, fmt.Errorf("pid %v is command runner for different unit", pid) | ||
} | ||
|
||
// Restart resumes monitoring a job after a Receptor restart. | ||
func (cw *commandUnit) Restart() error { | ||
if err := cw.Load(); err != nil { | ||
|
@@ -253,6 +283,15 @@ func (cw *commandUnit) Restart() error { | |
// Job never started - mark it failed | ||
cw.UpdateBasicStatus(WorkStateFailed, "Pending at restart", stdoutSize(cw.UnitDir())) | ||
} | ||
if isOrphaned, err := cw.isOrphaned(); isOrphaned { | ||
// Job never completed - mark it failed and wipe pid | ||
cw.UpdateFullStatus(func(status *StatusFileData) { | ||
status.State = WorkStateFailed | ||
status.Detail = fmt.Sprintf("Orphaned: %s", err) | ||
status.StdoutSize = stdoutSize(cw.UnitDir()) | ||
status.ExtraData.(*commandExtraData).Pid = 0 | ||
}) | ||
} | ||
go cw.monitorLocalStatus() | ||
|
||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know what user elevation is needed for this library to work as expected?